POJ 1061 Date of the Frog (extended Euclidean algorithm)

Source: Internet
Author: User
Tags gcd greatest common divisor

Topic links

Description

Two of the frogs met on the internet, and they chatted very happily, so they felt it was necessary to see one side. They were pleased to find that they lived on the same latitude line, so they agreed to jump westward until they met. But they forget a very important thing before they set out, neither to ask clearly the characteristics of each other, nor to meet the specific location. But the frogs are very optimistic, they feel as long as they have to jump in some direction, always meet each other. But unless the two frogs jump to the same point at the same time, they will never be able to meet. To help these two optimistic frogs, you are asked to write a procedure to determine if the two frogs will meet and when.
We have these two frogs are called Frog A and Frog B, and the latitude line on the longitude of 0 degrees at the origin, from east to West for the positive direction, the unit length of 1 meters, so that we get a first-to-last line. The starting point of setting Frog A is x, and Frog B's starting point coordinates are Y. Frog A can jump M m at a time, Frog B can jump n meters at a time, and two frogs will spend the same time jumping once. Latitude line total length l m. Now you have to find out how many times they have jumped before they meet.

Input

The input includes only one line of 5 integer 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, and output a line of "impossible" if it is never possible to meet

Sample Input

1 2 3) 4 5

Sample Output

4

Analysis:

began to use a violent enumeration, and then a look at the data so large, estimated definitely timed out, helpless to search the Internet, the study is extended Euclidean algorithm, there are relatively large integer _int64 processing.

After S-step, the two frogs meet, they will satisfy the equation: (x+ms)-(Y+ns) =kL (k=0,1,2 ...)
To deform the equation: (n-m)
s+kl=x-y
Make N-m=a,k=b,x-y=c, that is, the primitive can be converted to: a
s+b*l=c
If there is an integer solution, then two frogs can meet, or not.

The first thought of a method is to use two times for the loop to enumerate the value of the s,l, see if there is a s,l integer solution, if there is a minimum of s, but obviously this method is not advisable, who does not know the smallest s is how big, if the smallest s is large, the timeout is obvious.

In fact, the problem with the Euclidean expansion of the principle can be quickly resolved, first to see what is the Euclidean expansion principle:
Euclidean algorithm, also known as the greatest common divisor method, is used to calculate two integers, a, b, and so on. Its computational principle relies on the following theorem: Gcd (A, b) = gcd (b,a MoD)
Proof: A can be expressed as A = kb + R, then r = a mod b
Suppose D is a number of conventions for a, B, then there are D|a, d|b, and r = a-kb, so d|r, so D is the number of conventions (B,a mod b)
Assuming D is the number of conventions (B,a mod b), then D | B, D |r, but a = KB +r, so D is also the number of conventions (A, B)
therefore (b) and (b,a mod b) The number of conventions is the same, and their greatest common divisor are necessarily equal, to be certified

Euclidean algorithm is based on this principle, its algorithm is described in C + + language as:

int Gcd(intint b){    0)          return a;    return Gcd(b, a % b);  }

It can also be written as an iterative form:

int Gcd(intint b) {    while0)    {        int r = b;        b = a % b;        a = r;    }    return a;}

Supplement: The extended Euclidean algorithm is used to solve a set of X, Y, andx+bY=GCD (A, A, b) in known A, a (a, a) (the solution must exist, according to the correlation theorem in number theory). Extended Euclidean is commonly used in solving linear equations and equations. The following is an implementation that uses C + +:

int  exgcd (int  A, int  B, int  &x, int  &y) {
    
     if  (b = = 
     0 ) {x = 
     1 ;        y = 
     0 ;              
     return  A;    } 
     int  r = exgcd (b, a% B, x, y);    
     int  t = x;    x = y;    y = t-a/b * y;  
     return  R; }
    

Comparing this implementation with the recursive implementation of GCD, we find that the following X, Y assignment process is found, which is the essence of extending Euclid's algorithm.
Can think like this:
For a ' = B, b ' = a% B, we get x, Y makes a ' x + b ' y = Gcd (a ', B ')
Because b ' = a% b = a-a/b * B (Note: This is a division in the programming language)
Then you can get: a ' x + b ' y = Gcd (a ', B ') ===>
BX + (A-A/b * b) y = GCD (a ', b ') = GCD (A, b) ===>
Ay +b (x-a/by) = GCD (A, B)
Therefore, for A and B, their relative p,q are Y and (x-a/b
y) respectively.
  
Read a lot on the internet on the problem of solving the equations of indefinite equation, can not say that all, all only said a part, after looking at a lot of real to understand the indefinite equation of the whole process, the steps are as follows:
An integer solution that asks A * x + b * y = n.
1, first calculate gcd (A, b), if n can not be divisible by gcd (A, B), then the equation has no integer solution; otherwise, the equation is divided by gcd (A, a), and the new indefinite equation a ' * x + b ' * y = n ', at this time gcd (a ', ' c ') = 1
2, using the Euclidean algorithm described above to find the equation a ' * x + b ' * y = 1 of a set of integer solution X0,y0, then n ' * x0,n ' * y0 is the equation a ' * x + b ' * y = n ' A set of integer solutions;
3, according to the correlation theorem in number theory, can get the equation a ' * x + b ' * y = n ' of all the integer solution is:
x = N ' * x0 + b ' * t
y = N ' * y0-a ' * t
(t is an integer)
The above solution is the whole integer solution of a * x + b * y = n.

Code:

#include <stdio.h>#include <iostream>using namespaceStd#define LL Long Longll EXGCD (ll a,ll b,ll &x,ll &y) {if(!B) {x=1; y=0;returnA }Else{LL t=exgcd (b,a%b,y,x); y-= (A/b) *x;returnT }}ll x,y,m,n,l; LL A,b,c;intMain () {scanf ("%lld%lld%lld%lld%lld", &x,&y,&m,&n,&l);    A=m-n;    B=l;    C=y-x; LL GCD=EXGCD (a,b,x,y);if(C%GCD) printf ("Impossible\ n");Else{x*=1LL*C/GCD; printf"%lld", (x%b+b)%b); }return 0;}

POJ 1061 Date of the Frog (extended Euclidean algorithm)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.