Example 1 frog appointments

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

The number of hops required to meet the output, if never meet the output line "Impossible"

Sample Input

1 2 3 4 5

Sample Output

4 
Solution: This is an extension of the Euclidean algorithm to solve the linear equations of Indefinite examples using
i.e., ax + by = c problems of this kind or that a * x == c (mod b ) the relationship between the two is equivalent to
Solutions ax by = + __ gcd (a, b) is determined by first solving this kind of problem template, and then determines whether c is a multiple of __gcd (a, b), if it is, then a solution, if not no solution
next step is to update xx = x * c / __ gcd (a, b) can be imagined, at the same time both sides of the equation multiplied by c / __ gcd (a, b ). y Similarly
the next step is again updated as x X may be negative, and we require is positive then while (x <= 0) x + = b / __ gcd (a, b) if it is y, then y + = a / __ gcd ( a, b);
there is a way but some is the wrong time (x = (x% b / __ gcd + b / __ gcd)% b / __ gcd) y Similarly
//    if c% gcd == 0 then the equation has a solution, or if there is no solution while solutions of multiplying the equation on both sides
 //    C / GCD (A, B) to give (a * c / gcd (a , b)) * + X0 (B * C / GCD (a, B)) = C * yO;
 //    Then a solution derived equations x1 = x0 * c / gcd ( a, b) y1 = y0 * c / gcd (a, B) 
#include <the iostream> 
#include <cstdio>
 the using  namespace STD; 
typedef Long  Long LL; 
LL X, Y, m, n-, L; 
LL X0, yO; 
LL exgcd (A LL, LL B) { 
    IF (B == 0 ) { 
        X0 = . 1 ; 
        yO = 0 ;
         return A; 
    } 
    LL R & lt = exgcd (B, A% B); 
    LL T=y0;
    y0=x0-(a/b)*y0;
    x0=t;
    return r;
}
int main(){
    cin>>x>>y>>m>>n>>l;
    ll a=n-m;
    ll b=l;
    ll c=x-y;
    if(a<0){
        a=-a;
        c=-c;
    }
    ll d=exgcd(a,b);
    
    if(c%d!=0){
        puts("Impossible");
    }
    
    else {
        x0=x0*c/d;
        b=b/d;
        x0=(x0%b+b)%b;
        cout<<x0<<endl;
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Accepting/p/11355233.html