Frog dating poj1061 [extended Euclidean]

Two frogs on the Internet acquaintance, and they talked happily, so that it is necessary to see the side. They are pleased to find that they live in the same line of latitude, so they agreed to their west-hop until you meet so far. But before they start forgetting a very important thing, neither ask other features, there is no agreement to meet the specific location. But the frogs are very optimistic, as long as they feel has been moving in a certain direction jump, always run into each other's. But unless these two frogs jump to the same point at the same time, otherwise it is impossible to never meet the. To help these two optimistic frog, you are asked to write a program to determine whether it can meet two frogs, will meet at what time.
We call two frogs were called A frog and frog B, a predetermined longitude and latitude 0 degrees at the line of origin in the positive direction from east to west, a unit length 1 m, so that we get a butt axes . A frog is provided is the starting point coordinates x, the coordinates of the starting point frog B is y. A frog m m a jump, a jump frog n B m, a two frog jump takes the same time. Latitude line length L meters. Now you will meet several times after the jump find them.
Input
input line 5 includes only integers x, y, m, n, L, where x ≠ y <2000000000,0 <m, n <2000000000,0 <L <2100000000.
Output
Output the number of hops required to meet, if the output line never meet "Impossible"
the Sample the Input
. 1 2. 5. 4. 3
the Sample Output
. 4
** Solution: ** title meaning two frogs jumping around a planet, whether we can meet each other, two different jumping frog point distance is not the same, two frogs jump in the same direction, a bit like this topic the chase and physical problems, but there are different, because the problem is only integer solution, and this is to jump on a ring, speed is not necessarily who is great who is small, but we can look at the column formula, be sure to meet two frogs in the same place, but they traveled is not the same, although the distance is not the same, but they add a starting point and the origin of the distance, the difference between the two is sure to satisfy x + nt- (y + mt) = pL, tidy can be obtained (mn) t + kL = yx; cases this is clearly a demand extended Euclidean equation general solution, so that no solution of (yx)% gcd (mn, L) = 0;! otherwise, there must be solution, we can use Euclid's theorem to calculate a set of particular solution through the solution and then calculate the smallest integer of the calculated final solution.

#include<iostream>
#include<queue>
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long ll;
ll exgcd(ll a,ll b,ll &x,ll &y)//扩展欧几里得算法
{
    if(b==0)
    {
        x=1;y=0;
        return a;  //到达递归边界开始向上一层返回
    }
    ll r=exgcd(b,a%b,x,y);
    ll temp=y;    //把x y变成上一层的
    y=x-(a/b)*y;
    x=temp;
    return r;     //得到a b的最大公因数
}
int main()
{
    ll  x,y,m,n,L;
    cin>>x>>y>>m>>n>>L;
    ll a=m-n;
    ll c=y-x;
    if(a<0){
        a=-a;
        c=-c;
    }
    ll gcd=exgcd(a,L,x,y);
    if(c%gcd!=0){
        printf("Impossible\n");
    }
    else{
        x=x*c/gcd;
        int t=L/gcd;
        if(x<0) x=x%t+t;
        else x=x%t;
       cout<<x<<endl; 
    }
 //  system("pause");
}

Published 156 original articles · won praise 9 · views 6514

Guess you like

Origin blog.csdn.net/weixin_43438700/article/details/103438919