每组数据有两个数n(0 <= n < 9973)和B(1 <= B <= 10^9)。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 using namespace std; 7 typedef long long ll; 8 ll exgcd(ll a,ll b,ll &x,ll &y) 9 { 10 if(!b) 11 { 12 x=1;y=0; 13 return a; 14 } 15 ll r=exgcd(b,a%b,x,y); 16 ll t=x; 17 x=y; 18 y=t-a/b*y; 19 return r; 20 } 21 void work(ll a,ll b,ll c) 22 { 23 ll x,y; 24 ll r=exgcd(a,b,x,y); x*=c/r; 25 ll t=b/r; 26 if(t<0)t=-t; 27 x=(x%t+t)%t; 28 printf("%lld\n",x); 29 } 30 int main() 31 { 32 ll a,b;int T; 33 scanf("%d",&T); 34 while(T--) 35 { 36 scanf("%lld%lld",&a,&b); 37 work(b,9973,a); 38 } 39 return 0; 40 }