Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1576
Problem description (a/B) % 9973, but since a is very large, we only give n (n = A % 9973) (the given a must be divisible by B, and gcd (B, 9973) = 1 ). The first line of input data is a T, indicating that T groups of data exist.
Each data set has two data sets: n (0 <= n <9973) and B (1 <= B <= 10 ^ 9 ).
Output corresponds to the data output (a/B) of each group % 9973. Sample Input
21000 5387 123456789
Sample output
79226060
Authorxhdsourcehdu 2007-1 Programming Contest
PS:
N = A % 9973, set a/9973 = y;
A/B = x --- >>> A = B * X;
Then there is: B * X-9973 * Y = N;
The Code is as follows:
#include <cstdio>#include <cstring>#include <cmath>typedef __int64 LL;LL exgcd(LL a,LL b,LL &x,LL &y){ if(b == 0) { x = 1; y = 0; return a; } LL r = exgcd(b,a%b,x,y); LL t = x; x = y; y = t-a/b*y; return r;}LL cal(LL a, LL b, LL c){ LL x, y; LL tt = exgcd(a, b, x, y); if(c%tt) return -1; x *= c/tt; b/=tt; if(b < 0) b = -b; LL ans = x%b; if(ans < 0) ans += b; return ans;}int main(){ LL n, b; LL t; scanf("%I64d",&t); while(t--) { scanf("%I64d%I64d",&n,&b); LL ans = cal(b,9973,n); if(ans == -1) printf("Impossible\n"); else printf("%I64d\n",ans); } return 0;}
HDU 1576 A/B (Extended Euclidean deformation)