标签:
思路:设(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,记得取模。
void Ex_gcd(int a,int b,int &x,int &y) { if(b==0) { x=1; y=0; return ; } else { Ex_gcd(b,a%b,y,x); y-=x*(a/b); } } int main() { #ifndef ONLINE_JUDGE freopen("in.cpp","r",stdin); #endif // ONLINE_JUDGE int t; cin>>t; while(t--) { int n,B; int x,y; scanf("%d%d",&n,&B); Ex_gcd(B,MOD,x,y); x=(x%MOD+MOD)%MOD; printf("%d\n",(x*n)%MOD); } return 0; }
标签:
原文地址:http://blog.csdn.net/u013514722/article/details/45647369