HDU - 1576
要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1)。
每组数据有两个数n(0 <= n < 9973)和B(1 <= B <= 10^9)。
2 1000 53 87 123456789
7922 6060
思路:
求:(A/B)%9973
首先,依题可得:
(1)n=A%9973;
(2)A|B
(3)gcd(B,9973)==1
现在我们根据gcd(B,9973)这条件我们可以反推
x*B+y*9973==gcd(B,9973);
因为A|B--> A/B=x -->A=B*x;
因为A%9973=n,根据同余可得:A=n+9973*y -->n+9973*y = B*x -->(x/n)*B-(y/n)*9973 = 1
最终推出 (x/n)*B-(y/n)*9973 = gcd(B,9973) = 1 -->得到 要求的x1 = x/n;
所以 我们只要求出 (B,9973的)逆元x1 那么x=x1*n=A/B;
#include <iostream>
#include <cstring>
#include <map>
using namespace std;
typedef long long ll;
ll exgcd(ll a,ll b,ll &x,ll &y){
if(a==0&&b==0){
return -1;
}
if(b==0){
x=1;
y=0;
return a;
}
ll d=exgcd(b,a%b,y,x);
y = y-a/b*x;
return d;
}
ll solve(ll b,ll m){
ll x,y;
ll d = exgcd(b,m,x,y);
if(d==1){
return (x%m+m)%m;
}
else{
return -1;
}
}
int main(){
int t;
ll n,b;
cin>>t;
while(t--){
cin>>n>>b;
ll m=9973;
ll x1=solve(b,m);
ll ans=((x1*n)%m+m)%m;
cout<<ans<<endl;
}
return 0;
}