思路:设(A/B)%9973 = K, 则A/B = k + 9973x (x未知), 因此A = kB + 9973xB,
又A%9973 = n, 所以kB%9973 = n, 故kB = n + 9973y (y未知)
故(k/n)B +(-y/n)*9973 = gcd(B,9973) = 1
扩展欧几里得 求出k/n, 再乘以个n,记得取模,就是answer了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define m 9973
using namespace std;
typedef long long LL;
void gcd(LL a,LL b,LL &x,LL &y)
{
if(!b) {x=1;y=0;}
else {gcd(b,a%b,y,x);y-=x*(a/b);}
}
int main()
{
int t;
LL n,b,x,y;
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld",&n,&b);
gcd(b,m,x,y);
x=(x%m+m)%m;
printf("%lld\n",(x*n)%9973);
}
return 0;
}