- 原题链接:Here!
- 思路:设两只青蛙跳t步后相遇,可得到一个式子 x + t*m - y - t*n = p*L (P是正整数),整理一下可得:(n-m)*t + L*p = (x-y) ,明显的用拓展欧几里德。
- 代码:
#include<iostream> using namespace std; typedef long long LL; LL exgcd(LL a,LL b,LL &x,LL &y){ if(b==0){ x=1; y=0; return a; } LL d = exgcd(b,a%b,x,y); LL tmp = x; x = y; y = tmp - a/b*y; return d; } int main(){ LL x,y,m,n,l,tx,ty; while(cin>>x>>y>>m>>n>>l){ LL d = exgcd(n-m,l,tx,ty); if( (x-y)%d || m==n) // 因为x不等于y,m==n时必不相遇 cout<<"Impossible"<<endl; else{ LL ans = tx*(x-y)/d; LL tmp = l/d; ans = (ans%tmp+tmp)%tmp; cout<<ans<<endl; } } return 0; }