标签:
1 #include<iostream> 2 using namespace std; 3 4 long long int exgcd(long long int a,long long int b,long long int &x,long long int &y) 5 { 6 if(b==0) 7 { 8 x=1; 9 y=0; 10 return a; 11 } 12 long long int gcd=exgcd(b,a%b,x,y); 13 long long int x2=x,y2=y; 14 x=y2; 15 y=x2-(a/b)*y2; 16 return gcd; 17 } 18 19 int main() 20 { 21 long long int x,y,n,m,l,a,b,c,gcd,t; 22 while(cin>>x>>y>>m>>n>>l) 23 { 24 a=n-m; 25 b=l; 26 c=x-y; 27 gcd=exgcd(a,b,x,y); 28 if(c%gcd!=0) 29 cout<<"Impossible"<<endl; 30 else 31 { 32 c/=gcd; 33 x*=c; 34 x=(x%b+b)%b; 35 cout<<x<<endl; 36 } 37 } 38 return 0; 39 }
注:设输出结果为 s,则(m*s+x)-(n*s+y)= kl (k∈Z),即(n-m)*s + kl = x-y,设 a=n-m,b=l,c=x-y,即ax+by=c,求最小正整数x,这里要用到扩展欧几里得算法(Extended Euclidean Algorithm)。
标签:
原文地址:http://www.cnblogs.com/haveyoueverbeen/p/4483218.html