poj 1061 青蛙的约会(扩展欧几里得)

Description

两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面。它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止。可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特征,也没有约定见面的具体位置。不过青蛙们都是很乐观的,它们觉得只要一直朝着某个方向跳下去,总能碰到对方的。但是除非这两只青蛙在同一时间跳到同一点上,不然是永远都不可能碰面的。为了帮助这两只乐观的青蛙,你被要求写一个程序来判断这两只青蛙是否能够碰面,会在什么时候碰面。 
我们把这两只青蛙分别叫做青蛙A和青蛙B,并且规定纬度线上东经0度处为原点,由东往西为正方向,单位长度1米,这样我们就得到了一条首尾相接的数轴。设青蛙A的出发点坐标是x,青蛙B的出发点坐标是y。青蛙A一次能跳m米,青蛙B一次能跳n米,两只青蛙跳一次所花费的时间相同。纬度线总长L米。现在要你求出它们跳了几次以后才会碰面。
 

Input

输入只包括一行5个整数x,y,m,n,L,其中x≠y < 20000000000 < m、n < 20000000000 < L < 2100000000

Output

输出碰面所需要的跳跃次数,如果永远不可能碰面则输出一行"Impossible"

Sample Input

1 2 3 4 5

Sample Output

4

Source

 
 
 1 #pragma comment(linker, "/STACK:1024000000,1024000000")
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<math.h>
 7 #include<algorithm>
 8 #include<queue>
 9 #include<set>
10 #include<bitset>
11 #include<map>
12 #include<vector>
13 #include<stdlib.h>
14 using namespace std;
15 #define max(a,b) (a) > (b) ? (a) : (b)  
16 #define min(a,b) (a) < (b) ? (a) : (b)
17 #define ll long long
18 #define eps 1e-10
19 #define MOD 1000000007
20 #define N 1000000
21 #define inf 1e12
22 ll a,b,m,n,L;
23 ll e_gcd(ll a,ll b,ll &x,ll &y){
24      if(b==0)
25       {
26           x=1;
27           y=0;
28           return a;
29       }
30       ll r=e_gcd(b,a%b,x,y);
31       ll t=x;
32       x=y;
33       y=t-a/b*y;
34       return r;
35  }
36 int main()
37 {
38     while(scanf("%I64d%I64d%I64d%I64d%I64d",&a,&b,&m,&n,&L)==5){
39         ll x,y,r;
40         ll d=e_gcd((n-m),L,x,y);
41         //printf("---%d %d %d
",d,x,y);
42         if((a-b)%d!=0){
43             printf("Impossible
");
44             continue;
45         }
46         else{
47             x=x*(a-b)/d;
48             r=L/d;
49             x=(x%r+r)%r;
50             printf("%I64d
",x);
51         }
52     }
53     return 0;
54 } 
View Code