题意:有两只青蛙,一只在坐标x,另一直在坐标y,青蛙x一次跳跃可以前进m单位距离,青蛙y一次跳跃可以前进n单位的距离,两青蛙都在同一纬度,该纬度长度为L。两只青蛙同方向同时跳啊跳,问你最少跳多少次,它们才可以相遇,如果不能相遇,输出impossble。
思路:初识扩展欧几里得~
题目链接:http://poj.org/problem?id=1061
View Code
1 #include <cstdio> 2 #include <cmath> 3 #include <cstdlib> 4 #include <cstring> 5 #include <string> 6 #include <algorithm> 7 #include <iostream> 8 using namespace std; 9 #define LL long long 10 11 LL x,y,m,n,L; 12 13 LL exgcd(LL a,LL b){ 14 LL t,d; 15 if(b==0){ 16 x=1; 17 y=0; 18 return a; 19 } 20 else{ 21 d=exgcd(b,a%b); 22 t=x; 23 x=y; 24 y=t-(a/b)*y; 25 return d; 26 } 27 } 28 29 int main(){ 30 31 freopen("data.in","r",stdin); 32 freopen("data.out","w",stdout); 33 34 while(scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&L)!=EOF){ 35 LL a=n-m; 36 LL b=L; 37 LL c=x-y; 38 LL d=exgcd(a,b); 39 if(c%d!=0) {puts("Impossible"); continue;} 40 x=x*(c/d); 41 y=y*(c/d); //x1=x+b/d*t y1=y-a/d*t; 42 LL k=x*d/b; 43 k=x-k*b/d; 44 if(k<0) k+=b/d; 45 printf("%lld\n",k); 46 } 47 return 0; 48 }