代碼如下:
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
const LL m=9973;
LL exgcd(LL a,LL b,LL &x,LL &y)
{
if(b==0)
{
x=1;
y=0;
return a;
}
LL ans=exgcd(b,a%b,x,y);
LL t=y;
y=x-(a/b)*y;
x=t;
return ans;
}
int main()
{
int kase;
scanf("%d",&kase);
while(kase--)
{
LL B,n,x,y;
scanf("%lld %lld",&n,&B);
LL g=exgcd(B,m,x,y);
x*=n;
x=(x%m+m)%m;
cout << x << endl;
}
return 0;
}