Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 108469 | Accepted: 21745 |
Description
Input
Output
Sample Input
1 2 3 4 5
Sample Output
4
题意:
根据题意,两个青蛙跳到同一个点上才算是遇到了,所以有 (x+m*s) - (y+n*s) = t * l; ( s 是跳的次数,l 是a青蛙跳的圈数跟b青蛙的圈数之差。整个就是路程差等于纬度线周长的整数倍),
转化一下: (n-m) * s + l* t = x – y;
令 a = n-m, b = l c = gcd(a, b), d = x-y;
有 a * s + b * t = d;
要求的是 s 的最小整数解。
代码:
#include
#include
#include
#include
#include
using namespace std;
typedef long long int ll;
ll ext_gcd(ll a,ll b,ll& s,ll& t){
if(b==0){
s = 1;
t = 0;
return a;
}
ll ans = ext_gcd(b,a%b,s,t);
ll tmp = s;
s = t;
t = tmp-a/b*s;
return ans;
}
int main(){
ll x,y,m,n,l;
scanf("%I64d %I64d %I64d %I64d %I64d",&x,&y,&m,&n,&l);
ll s=0,t=0;
ll gcd = ext_gcd(n-m,l,s,t);
if((x-y)%gcd!=0){
printf("Impossible\n");
return 0;
}
s *= (x-y)/gcd;
t *= (x-y)/gcd;
s = s%(l/gcd);
ll tmp = fabs(l/gcd);
printf("%I64d\n",(s+tmp)%tmp);
return 0;
}