拓展gcd是一种求两个数a、b的gcd以及ax+by=gcd(a,b)的算法,和gcd的递归类似
传送门:拓展GCD详解
这道题就用到了其解模线性方程的这样一个性质
想要求 A/B%9973,n=A%9973。
而且gcd(b,9973)=1
可以化简为bx=n(mod9973)求x
这就变成了求模线性方程的裸题
答案为x=x' *(n/d)%9973,这里x'就是拓展gcd中求出的x,而这道题保证了d=1,答案就刻化简为n*x'%9973
因为x算出来有可能是小于0的,所以将其变形为 (n*x%9973+9973)%9973,这样能保证n*x在0到9972区间内;
#include<iostream>
#include<cstdio>
#include<vector>
#include<set>
#include<map>
#include<string.h>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#define LL long long
#define mod 1000000007
#define inf 0x3f3f3f3f
#define sqr(a) (a)*(a)
#define lan(a,b) memset(a,b,sizeof(a))
using namespace std;
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=y;y=x-a/b*y;x=t;
return r;
}
int main()
{
int t;
scanf("%d",&t);
LL n,b;
while(t--)
{
scanf("%lld%lld",&n,&b);
LL x,y;
LL d=exGcd(b,9973,x,y);
//printf("%lld %lld %lld\n",d,y,b);
printf("%lld\n",(n*x%9973+9973)%9973);
}
return 0;
}