POJ 1061 青蛙的约会
/* 扩展欧几里德: 求解:ax+by=GCD(a,b)=d */ #include <iostream> #include <algorithm> #include <cmath> #include <cstdio> typedef long long LL; const int N=1000100; using namespace std; void gcd ( LL a, LL b , LL &d , LL &x , LL &y ) { //a,b分别代表方程的系数,d返回a和b的最大公约数,x,y返回对应的解 if ( ! b )//当b等于0,方程变成ax=gcd(a,0)=a,所以方程解为x=1,y=0,d为a d = a , x = 1 , y = 0 ; else { gcd ( b , a % b , d , y , x ) ; y -= ( a / b ) * x ; } } int main() { LL x,y,m,n,L,d; cin>>x>>y>>m>>n>>L; LL a=m-n,ans=y-x; if ( a < 0 ) a*= -1, ans*= -1 ; gcd(L,a,d,x,y); if(ans%d) cout<<"Impossible"<<endl; else { LL t=L/d; ans=(ans/d*y)%t; if(ans<0) ans+=t; cout<<ans<<endl; } return 0; }
时间: 2024-09-29 00:48:14