1 #include<cstdio> 2 #include<iostream> 3 #include<queue> 4 #include<string> 5 #include<math.h> 6 #include<stack> 7 #include<cstdlib> 8 #include<set> 9 #include<map> 10 #include<cstring> 11 #include<vector> 12 #include<algorithm> 13 #include<cctype> 14 #include<sstream> 15 16 const double PI = acos(-1.0); 17 typedef long long ll; 18 typedef long long LL; 19 const int MOD = 9973; 20 using namespace std; 21 22 // ax+by = gcd(a, b) = d 已知a,b. 解 x,y,d; 23 // b = 0 时: ax = gcd(a,0) = d; --> d = a , x = 1 ,y = 0 24 void ext_gcd(ll a,ll b,ll &x,ll &y,ll &d){ 25 if(!b){ 26 d = a, x = 1 , y = 0; 27 }else{ 28 ext_gcd(b,a%b,y,x,d); // y,x交换位置 29 y = y - x*(a/b); // y = y - x*(a/b); 30 } 31 } 32 int main(){ 33 // (k/n)B +(-y/n)*9973 = gcd(B,9973) = 1 34 // x=k/n 35 ll n,b,T,x,y,tmp; 36 scanf("%lld",&T); 37 while(T--){ 38 scanf("%lld%lld",&n,&b); 39 ext_gcd(b,MOD,x,y,tmp); 40 x=(x%MOD+MOD)%MOD; 41 printf("%d\n",(x*n)%MOD); 42 } 43 return 0; 44 } 45 /* 46 令(A/B)%9973=k 47 A/B = k + 9973x 48 --> A = kB + 9973*x*B, 49 50 A%9973 = n 51 --->k*B%9973 = n 52 --->kB = n + 9973*y 53 54 故(k/n)B +(-y/n)*9973 = gcd(B,9973) = 1 55 */
/*
令(A/B)%9973=k
A/B = k + 9973x
--> A = kB + 9973*x*B,
A%9973 = n
--->k*B%9973 = n
--->kB = n + 9973*y
故(k/n)B +(-y/n)*9973 = gcd(B,9973) = 1
*/
时间: 2024-10-09 05:23:49