POJ1061 青蛙的约会

Description

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

Input

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

Output

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

Sample Input

1 2 3 4 5

Sample Output

4

Source

 
 
正解:扩展欧几里得
解题报告:
  这是一道扩展欧几里得裸题,然而我到现在还不会完整的推导过程。
  我根据这几篇博客写的这道题:
  http://blog.chinaunix.net/uid-22263887-id-1778922.html
  http://blog.csdn.net/zhjchengfeng5/article/details/7786595
  具体的,偷懒的我就不写了...
 
 
 1 //It is made by jump~
 2 #include <iostream>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <algorithm>
 8 #include <ctime>
 9 #include <vector>
10 #include <queue>
11 #include <map>
12 #include <set>
13 using namespace std;
14 typedef long long LL;
15 const int inf = (1<<30);
16 LL n,m,X,Y,L;
17 LL a,b;
18 LL GCD,ans;
19 
20 inline int getint()
21 {
22     int w=0,q=0; char c=getchar();
23     while((c<'0' || c>'9') && c!='-') c=getchar(); if(c=='-') q=1,c=getchar(); 
24     while (c>='0' && c<='9') w=w*10+c-'0', c=getchar(); return q ? -w : w;
25 }
26 
27 inline LL extend_gcd(LL aa,LL bb,LL &x,LL &y){
28     if(bb==0) {
29     x=1; y=0;
30     return aa;
31     }
32     LL cun=extend_gcd(bb,aa%bb,x,y),lin=x;
33     x=y; y=lin-(aa/bb)*y;
34     return cun;
35 }
36 
37 inline void work(){
38     X=getint(); Y=getint(); m=getint(); n=getint(); L=getint();
39     if(n==m) { printf("Impossible"); return ; }//不可能相遇
40     GCD=extend_gcd(n-m,L,a,b);
41     LL you=X-Y;
42     if(you%GCD!=0) { printf("Impossible"); return ; }//无解
43     L/=GCD; you/=GCD;//我们希望L的系数最小
44     ans=you*a; ans%=L; ans+=L; ans%=L;
45     printf("%lld",ans);
46 }
47 
48 int main()
49 {
50     work();
51     return 0;
52 }

 

posted @ 2016-09-25 18:53  ljh_2000  阅读(200)  评论(0编辑  收藏  举报