Poj1061青蛙的约会
来源:互联网 发布:网络内容提供商有哪些 编辑:程序博客网 时间:2024/04/29 08:51
这是一个中文题,两只青蛙在相同的纬度向相同的方向跳,问何时才能相遇。
#include<stdio.h>long long gcd(long long a,long long b){ if(b==0) return a; else return gcd(b,a%b);}void exgcd(long long a,long long b,long long& x,long long& y){ if(b==0) { x=1; y=0; return ; } else { exgcd(b,a%b,y,x); y-=x*(a/b); }}int main(){ long long x,y,m,n,l,a,b,c,r,t,k1,k2; while(scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&l)!=EOF) { a=n-m; b=l; c=x-y; r=gcd(a,b); if(c%r||m==n) { printf("Impossible\n"); } else { a=a/r; b=b/r; c=c/r; exgcd(a,b,k1,k2); t=k1*c/b; t=c*k1-b*t; if(t<0) t+=b; printf("%lld\n",t); } } return 0;}