要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1)。
Input
数据的第一行是一个T,表示有T组数据。
每组数据有两个数n(0 <= n < 9973)和B(1 <= B <= 10^9)。
Output
对应每组数据输出(A/B)%9973。
Sample Input
2
1000 53
87 123456789
Sample Output
7922
6060
问题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576
问题分析:由扩展欧几里得可得gcd(A,B)=xA+yB ,gcd(B,9973)=xB+9973y=1
题目给出A/B%=0 ,所以设X=A/B,A=XB。n=A%9973=A+A/99739973=XB+A/99739973。 xnB+9973ny=n
所以 nx=X,由欧几里得可以求出X。
把求出来的X做(x%9973+9973)%9973即可
AC通过的C++语言程序如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
int exgcd(int a, int b, int &x, int &y)
{
if (b == 0)
{
x = 1;
y = 0;
return a;
}
int r = exgcd(b, a%b, x, y);
int t = x; x = y;
y = t - a / b * y;
return r;
}
int main()
{
int T;
cin >> T;
for (int i = 0; i < T; i++)
{
int n, B;
int x = 0, y = 0;
cin >> n >> B;
exgcd(B, 9973, x, y);
int a = n * x;
int res = (a % 9973 + 9973) % 9973;
cout << res << endl;
}
return 0;
}