poj 1061 青蛙的约会
扩展欧几里德算法的练习题,直接调用exgcd函数就可以了
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<algorithm> 6 #define inf 0x7fffffff 7 using namespace std; 8 9 typedef long long LL; 10 LL x,y,m,n,L; 11 LL x1,y1; 12 LL q; 13 void exgcd(LL a,LL b) 14 { 15 //cout<<a<<endl; 16 if (!b) {x1=1 ;y1=0 ;q=a ; } 17 else 18 { 19 exgcd(b,a%b); 20 LL temp=x1 ;x1=y1 ;y1=temp-a/b*y1; 21 } 22 } 23 LL gcd(LL a,LL b) {return b==0 ? a : gcd(b,a%b); } 24 int main() 25 { 26 while (cin>>x>>y>>m>>n>>L) { 27 LL d=y-x; 28 LL gcd=1; 29 exgcd(n-m,L); 30 gcd=q; 31 if (d%gcd) cout<<"Impossible"<<endl; 32 else 33 { 34 x1 *= (x-y)/gcd; 35 x1=(x1%(L/gcd)+(L/gcd))%(L/gcd); 36 cout<<x1<<endl; 37 } 38 } 39 return 0; 40 }