Description
Input
每組數據有兩個數n(0 <= n < 9973)和B(1 <= B <= 10^9)。
Output
Sample Input
Sample Output
解題思路
這道題主要是要找出等式關係:9973 * x + n = A = B * y,整理得:- 9973 * x + B * y = n;
再用拓展歐幾里得,求出正解
AC代碼
#include <cstdio>
#define LL __int64 //記住了,以後用 LL就行了^_^
LL extend_gcd(LL a, LL b, LL &x, LL &y)
{
if(b == 0)
{
x = 1;
y = 0;
return a;
}
LL d = extend_gcd(b, a % b, x, y);
LL t = x;
x = y;
y = t - a / b * y;
return d;
}
int main()
{
LL T, a, b, x, y, c, d, ans;
scanf("%I64d", &T);
while(T--)
{
a = 9973;
scanf("%I64d%I64d", &c, &b);
d = extend_gcd(a, b, x, y);
x = - x * (c / d);
while(x < 0)
x += b / d;
ans = ( (c + 9973 * x) / b ) % 9973;
printf("%I64d\n", ans);
}
return 0;
}