Poj 1061 frog dating extension Euclidean

Source: Internet
Author: User
Frog appointment
Time limit:1000 ms   Memory limit:10000 K
Total submissions:67617   Accepted:10832

Description

The two frogs met each other on the Internet. They had a good chat, so they thought it was necessary to meet each other. They are happy to find that they live on the same latitude line, so they agreed to jump westward until they met each other. However, before they set out, they forgot a very important thing. They did not know the characteristics of the other party, nor agreed on the specific location of the meeting. However, frogs are optimistic. They think that as long as they keep jumping in a certain direction, they will always meet each other. However, unless the two frogs jump to the same point at the same time, they will never be able to meet each other. To help the two optimistic frogs, you are asked to write a program to determine whether the two frogs can meet and when.
The two frogs are called Frog A and frog B respectively, and The 0th degree of the latitude line is the origin, from East to West is the positive direction, the unit length is 1 meters, in this way, we get a number axis that is connected at the beginning and end. Set the starting coordinate of frog a to X, and that of frog B to y. Frog A can jump M meters at a time, and frog B can jump n meters at a time. It takes the same time for the two frogs to jump at a time. The total length of the latitude line is l meters. Now you need to find out how many times they will be met.

Input

The input contains only five integers x, y, M, N, and l in a row. x = Y <2000000000,0 <m, n <2000000000,0 <L <2100000000.

Output

Output The number of jumps required for meeting, if it is never possible to meet each other, output a line "impossible" ============================ ========================================== the analysis is well written. From: http://qa4862741.blog.163.com/blog/static/56587549201112765242423/
The following describes the specific ideas (after finishing online and providing detailed processes, some problem-solving reports directly draw conclusions ):

1) First, let's consider that the coordinates of step a's jump to step t are x + MT, and B is Y + nt.

2). (x + M * t) % L = (Y + N * t) % L;
Therefore, it can be written as (x + M * t)-(Y + N * t) = p * l; (P is an arbitrary integer)
That is, (m-N) * t + p * l = Y-X;

3). Make a = m-N, B = Y-X;
Abbreviated as a * t + p * l = B
Then the problem is solved as a solution that satisfies the minimum T value of equation A * t + p * l = B (obviously T> = 0)

4 ). there is a theorem in the polynomial that contains u (x), V (x), so that f (x) u (x) + g (x) V (X) = gcd (f (x), g (x ));
Similarly, there is a similar Theorem in number theory, that is, given a, B, there are u, v, so that Au + bv = gcd (A, B );
In addition, for any U, V, set n = Au + bv, there is gcd (a, B) | n. Because gcd (a, B) | A, gcd (a, B) | B,
After division of gcd (a, B) on both sides, the N/gcd (A, B) is an integer. Therefore, gcd (a, B) | n.
From this we can see that the equation A * t + p * l = B is similar to the above form. At this moment, B is not necessarily satisfied with the solution.

5). How can we solve a * t + p * l = B? Inspired by the above, we can immediately come up with no solution,
That is, if gcd (A, L) is not divided into B, then this formula must be unsolvable. What is the solution?

6). Analyze as follows: Convert the problem to: for known a, B, N, evaluate X, Y to satisfy AX + by = N, (gcd (a, B) | N)
(At this time, a corresponds to the above a, B Corresponds to L, X, Y respectively corresponding to T, P, N corresponds to B, so that the write is to separate the problem from the study)

The following is a solution (the following seems to be a bit difficult. You may wish to calculate it with a pen or paper)
If D = gcd (a, B), we will find a special solution that satisfies the equation.
Suppose X' and y' meet the requirements of ax '+ by' = D (Here we assume that how to find this solution is described later)
Then both sides are multiplied by N/D at the same time;
Available: A * n * x'/d + B * n * y'/D = N;
Compare AX + by = N;
X0 = N * x'/d, Y0 = N * y'/d; (x0, y0 is a special solution of AX + by = N)

7). The next step is to find a general solution:
So we divide AX + by = n on both sides of the same d, and then make a' = A/D, B '= B/d, n' = N/d;
Obviously, gcd (A', B ') = 1;
Then there is a 'x + B 'y = n ';
From a' x + B 'y = n' to a' * x0 + B' * Y0 = n ';
A '(x-x0) + B' (y-y0) = 0;
Because gcd (A', B ') = 1, there is a' | (y-y0)
Then you can make y = y0-a '* T (t is an integer), then you can get x = x0 + B' * t
So y = y0-a '* t, x = x0 + B' * t is AX + by = n.
Y = (N * y '/D)-(A/D) * t;
X = (N * x'/D) + (B/d) * t;

8). At this moment, the t we want is the X
While Minx = x % B '= (x0 + B' * t) % B '= x0 % B' = (N * x'/d) % B'
NOTE: If Minx is a negative number, the number after modulo is smaller than 0 and greater than-B,
Therefore, as long as Minx = Minx + B ', Minx can take the smallest positive integer, which is our solution.
Let's go back to the previous assumption: X' and y' meet the requirements of ax '+ by' = d
It is to convert the problem into the x and y values of the solution AX + by = D.

9) You must be familiar with the division of the moving phase: For integers A and B, gcd (a, B) = gcd (B, A % B );
Here we can get some information
We push ax + by = D down one layer: There is X', y', so that B * x' + (a % B) y' = d
And a % B = A-B * (A/B );
So B * x' + (a-B * (a/B) y' = D;
Expand to a * y' + B * (x'-(A/B) * y') = D;
So there are x = y', y = x'-(A/B) * y ';
Then we can use recursion to obtain the values of X and Y corresponding to a and B (at this time, the values of X and Y are unique)
Also, determine the boundary condition. A * 1 + 0*0 = gcd (A, 0) =;
This is the Extended Euclidean.

#include<cstdio>long long ect_gcd ( long long a, long long b, long long &x, long long &y ){    long long tmp, ret;    if ( b == 0 )    {        x = 1, y = 0;        return a;    }    ret = ect_gcd ( b, a%b, x, y );    tmp = x; x = y; y = tmp - a / b * y;    return ret;}int main(){    long long x, y, m, n, l;    while ( scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&l) != EOF )    {        long long a = n - m, b = l, c = x - y;        long long d = ect_gcd ( a, b, x, y );        if ( c % d != 0 )        {            printf("Impossible\n");            continue;        }        x = ( x * c / d ) % ( b / d );        if ( x < 0 ) x += b / d;        printf("%lld\n",x);    }    return 0;}

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.