Poj 1061 frog appointment, poj1061 frog

Source: Internet
Author: User

Poj 1061 frog appointment, poj1061 frog
Frog appointment

Time Limit:1000 MS   Memory Limit:10000 K
Total Submissions:90083   Accepted:16257

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

Outputs the number of jumps required for the meeting. If the meeting is never possible, an "Impossible" line is output"

Sample Input

1 2 3 4 5

Sample Output

4
Question:
First, a small analysis: we can achieve it in step s. Then the equation is: (x + m * s)-(y + n * s) = k * L (k = 0, 1, 2 ....);
It can be resolved to (n-m) * s + k * L = x-y; equivalent to A * x + B * y = C;
Now we can solve the problem. This question requires a binary one-time equation and an integer x solution of the same remainder: ax + by = c. First, set d = gcd (a, B ), divide the two sides of the equation by d to get a/d * x + B/d * y = c/d. Obviously, a is the division of d, and B is the division of d, x and y are Integer Solutions, So c/d is also an integer. If c does not divide d, of course it is Impossible. Otherwise, if we can find the solutions x0 and y0 for ax0 + by0 = d, multiply the two sides by c/d, that is, a (c/d * x0) + B (c/d * y0) = c, you can get the solution of the original equation x = (c/d * x0), y = (c/d * y0.
Theorem involved:
<Span style = "color: # ff6666;"> Theorem 1 gcd (a, B) is the smallest positive integer in the linear combination of ax + by, x, y, z; <span style = "font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 12.800000190734863px; line-height: 18.399999618530273px; background-color: rgb (238,238,238); "> if d = gcd (a, B), the positive or negative integers k and l must be found, make d = a * k + B * l </span>
<Span style = "color: # ff0000;"> Theorem 2: If ax + by = c, x, y, z, c % gcd = 0; <span style = "font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 12.800000190734863px; line-height: 18.399999618530273px; background-color: rgb (238,238,238); "> If gcd (a, B) = 1, the equation ax bought c (mod B) has a unique solution on [0, b-1. </Span>
<Span style = "color: # ff0000;"> Theorem 3 If a <span style = "font-family: ;">, </span> B <span style = "font-family: ;"> is a positive integer of the mutual quality. </span> c <span style = "font-family:; "> is an integer and the equation </span>
ax+by=c(1)
There is a group of Integers to solve x0, and y0, All Integer Solutions of this equation can be expressed
x=x0+bt;y=y0-at;t∈z;(2)
The at + bp = c equation is divided by gcd (a, B) on both sides of the equation. a1t + b1p = c1 is obtained, and then extended_euclid is used to solve the smallest positive integer linear combination to obtain a group of solutions x1, y1, then, the group of the equation is T = x1 * c1, P = y1 * c1, and the minimum positive integer of t is obtained according to the (2) formula.(T % b1 + b1) % b1, b1 = B/gcd (a, B). If gcd (a, B) is d, the equation ax ≡ c (mod B) has a unique solution on [0, B/d-1. Pay attention to the number to be solved.
(T % b1 + b1) % b1 can be changed. If y is required, replace T with P.
Well, I can solve the problem based on the above knowledge. I will give three different code groups with the same posture, but the code style is different.
1. cpp
#include<iostream>#include<cstdio>using namespace std;long long X,Y;long long exgcd(long a,long b){    if(b==0){X=1;Y=0;return a;}    long long d=exgcd(b,a%b);    long long t=X-a/b*Y;X=Y;Y=t;    return d;}int main(){    long x,y,m,n,L,a,b,c;    cin>>x>>y>>m>>n>>L;    long long d=exgcd(n-m,L);    long long r=L/d;    if((x-y)%d) puts("Impossible");    else    {        cout<<((x-y)/d*X%r+r)%r<<endl;    }    return 0;}

///////////////////
#include <iostream>using namespace std;long long extgcd(long long a, long long b, long long &x, long long &y){    long long d, t;    if (b == 0) { x = 1; y = 0; return a; }    d = extgcd(b, a % b, x, y);    t = x - a / b * y; x = y; y = t;    return d;}int main(){    long long x, y, m, n, L, X, Y, d, r;    while (cin >> x >> y >> m >> n >> L)    {        d = extgcd(n - m, L, X, Y); r = L / d;        if ((x - y) % d) cout << "Impossible" << endl;        else cout << ((x - y) / d * X % r + r) % r << endl;    }    return 0;}

////////////////
#include <iostream>using namespace std;__int64 t,p;__int64 euclid(__int64 a,__int64 b){if(b==0)return a;elsereturn euclid(b,a%b);}void extended_euclid(__int64 a,__int64 b){if(b==0){t=1;p=0;}else{__int64 temp;extended_euclid(b,a%b);temp=t-a/b*p;t=p;p=temp;}}int main(){__int64 x,y,n,m,L,gcd;cin>>x>>y>>m>>n>>L;if (m==n){cout<<"Impossible"<<endl;return 0;}__int64 a,b,c,c1;a=n-m;b=L;c=x-y;gcd=euclid(a,b);c1=c%gcd;if (c1!=0){cout<<"Impossible"<<endl;return 0;}c/=gcd;a/=gcd;b/=gcd;extended_euclid(a,b);t*=c;p*=c;t=(t%b+b)%b;cout<<t<<endl;return 0;}

/////////////////
# Include <iostream> # include <cstdio> using namespace std ;__ int64 gcd (_ int64 a ,__ int64 B) {if (B = 0) return; else gcd (B, a % B);} void exgcd (_ int64 a ,__ int64 B ,__ int64 & xx ,__ int64 & yy) {if (B = 0) {xx = 1; yy = 0; return;} exgcd (B, a % B, xx, yy); _ int64 r = xx; xx = yy; yy = r-a/B * yy;} int main () {_ int64 x, y, m, n, l, a, B, c, xx, yy, rr, k, t; while (scanf ("% I64d % I64d % I64d % I64d % I64d", & x, & y, & m, & n, & l )! = EOF) {a = n-m; B = l; c = x-y; rr = gcd (a, B); // obtain, maximum common divisor of B if (c % rr! = 0) // If c cannot be divisible by r, the correlation Theorem in number theory indicates that the integer solution must not have printf ("Impossible \ n"); else {a = a/rr; B = B/rr; c = c/rr; exgcd (a, B, xx, yy); // calculate a * xx + B * yy = Gcd (a, B) in this case, Gcd (a, B) = 1 t = c * xx/B; // all solutions of the equation at this time are: x = c * k1 + B * t, // set x = 0 to obtain the value of t at the hour of x. In this way, the value of x may be less than 0. when x is less than 0, add B. K = c * xx-t * B; if (k <0) k = k + B; printf ("% I64d \ n", k) ;}} return 0 ;}

At the end, I will extract the summary from others:
 

This is actually an Extended Euclidean algorithm-solving an indefinite equation, linear homogeneous equation.

Analysis 1: Get the equation and establish the model

When two frogs meet each other after step s, the following equations must be met:

(X + m * s)-(y + n * s) = k * l (k = 0, 1, 2 ....)

 Slightly changed:

(N-m) * s + k * l = x-y

     N-m = a, l = B, x-y = c, that is

A * s + B * k = c// This equation is critical.

       Here, a, B, and c are constants, and s and k are unknown.

As long as the above formula has an integer solution, the two frogs can meet each other; otherwise, they cannot.

The first method is to use two for loops to enumerate the values of s and k to check whether there is an integer solution of s and k. If so, the minimum value of s is output, but obviously this method is not feasible, and no one knows how big the smallest s is. If the smallest s is large, the timeout is obvious.

 

 Learning: how to solve this equation? Extended by Euclidean

   Let's take a look at the Euclidean Algorithm:

Euclidean algorithm, also known as the moving phase division, is used to calculate the maximum approximate number of two integers a and B.

Theorem: gcd (a, B) = gcd (B, a mod B) // Gcd indicates the maximum common divisor.

The proof is as follows:

A can be expressed as a = kb + r, then r = a mod B

Assume that d is a common divisor of a and B.

D | a, d | B, and r = a-kb, so d | r

Therefore, d is (B,Mod B)

Assume that d is the common divisor of (B, a mod B ),

D | B, d | r, but a = kb + r

Therefore, d is also the common divisor of (a, B ).

Therefore, (a, B) and (B, a mod B) share the same common number, and the maximum common number must be equal.

Euclidean algorithm is based on this principle. Its algorithm is described in C language as follows:

Int Gcd (int a, int B)

{

   If (B = 0)

       Return;

   Return Gcd (B, a % B );

}

Of course, you can also write it as an iteration:

Int Gcd (int a, int B)

{

   While (B! = 0)

   {

       Int r = B;

      B = a % B;

     A = r;

   }

   Return;

}

Supplement: The Extended Euclidean algorithm is used to solve the solution of a * x + B * y = Gcd (a, B) (which must exist according to the related concepts in number theory ).The proof is as follows:

Ax +

= Gcd (a, B)         

= Gcd (B, a % B)

= Bx '+ (a % B) y'   

= Bx '+ (a-(a/B) * B) y'

= Bx '+ ay'-(a/B) * B * y'

= Ay '+ B (x'-(a/B) y ')

So ax + by = ay '+ B (x'-(a/B) y ')

So x = y', y = x'-(a/B) y'

The following is an implementation using C ++:

Int exGcd (int a, int B, int & x, int & y)

{

   If (B = 0)

   {

       X = 1;

       Y = 0;

      Return;

   }

   Int r = exGcd (B, a % B, x, y );

   Int t = x;

   X = y;

   Y = t-a/B * y;

  Return r;

}

The whole process of solving the integer solution of the uncertainty equation a x + B * y = n using the Extended Euclidean algorithm is as follows:

1. calculate Gcd (a, B) first. If n cannot be divisible by Gcd (a, B), the equation has no integer solution. Otherwise, the two sides of the equation are divided by Gcd (, b), obtain the new uncertainty equation a' * x + B '* y = n'. In this case, Gcd (A', B') = 1;

   2. Use the Extended Euclidean Algorithm to obtain a group of integers in equation a' * x + B '* y = 1 for solutions x0 and y0, then n' * x0, n '* y0 is a group of Integer Solutions for equation a' * x + B' * y = n;

3. According to the related Theorem in number theory, All integers of the equation a' * x + B '* y = n' can be obtained and interpreted:

       X = n' * x0 + B '* t

Y = n' * y0-a' * t  (T = 0, 1, 2 ,......)

The above solution is All Integer Solutions of a * x + B * y = n.

The above articles extract others.
 

Questions about ACM related to Extended Euclidean Algorithms

To obtain the smallest positive integer. X0 is a special solution, so (X0% B) is also a specific solution.

What are the characteristics and Solutions of linear homogeneous equations?

In number theory, linear homogeneous equations are the most basic homogeneous equations. "linear" indicates that the number of unknowns of an equation is one time, that is, an equation in the shape. This equation has solutions when and only when B can be divisible by the maximum common divisor of a and n (recorded as gcd (a, n) | B ). In this case, if x0 is a solution of the equation, all the solutions can be expressed as: Where d is the maximum approximate number of a and n. In the completely residual series {0, 1,… of the modulo n ,..., There are exactly d solutions in n-1. Directory 1 Example 2 finding special solutions 3 linear homogeneous equations 4 See example in Equation 3x limit 2 (mod 6) Where d = gcd () = 3, 3 without Division 2, therefore, there is no solution to the equation. In equation 5x limit 2 (mod 6), d = gcd () = divisible by 2. Therefore, the equation has exactly one solution in: x = 4. In the 4x limit 2 (mod 6) equation, d = gcd () = Division 2. Therefore, the equation has exactly two solutions in: x = 2 and x = 5. Find a special solution for the linear homogeneous equation ax ≡ B (mod n) (1) if d = gcd (a, n divisible by B, then it is an integer. The existence of an integer pair (r, s) (which can be obtained by the moving phase division) makes ar + sn = d, so it is a solution of equation (1. Other solutions are about the same as x. For example, d = gcd () = 4 in equation 12x (mod 28. Note that this is a solution. For model 28, all solutions are {4, 11, 18, 25 }. The solutions of linear homogeneous equations can be divided into several linear homogeneous equations. For example, for linear homogeneous equations: 2x limit 2 (mod 6) 3x limit 2 (mod 7) 2x limit 4 (mod 8), the first equation is first solved, after we get the value of x forward 1 (mod 3), so that x = 3 k + 1, the second equation becomes: 9 k forward 1 (mod 7) obtain k limit 3 (mod 7 ). Then, k = 7l + 3, the third equation can be converted into: 42l limit 16 (mod 8) solution: l limit 0 (mod 4), that is, l = 4 m. The original expression x = 21 (4 m) + 10 = 84 m + 10, that is, the solution is: x limit 10 (mod 84, and the result of the solution, we need to use the Chinese Remainder Theorem in number theory. For more information, see the quadratic surplus China residue theorem to describe linear homogeneous equations. Because ACM/ICPC has some questions about number theory, especially linear homogeneous equations, it is necessary to prepare this knowledge. I have read a lot of information about this part, including Chen jingrun's "Elementary number theory", the solution of the Program Design Competition examples, the "Black Book", and a lot of online materials, I personally think that the best and most thorough explanation is the relevant chapter in introduction to algorithms. I suddenly realized it. After several days of self-study, I felt that I had mastered the "Mysteries ". Write it as an article. So what is a linear homogeneous equation? For the equation: ax ≡ B (mod m), a, B, and m are all integers, and the value of x is obtained. Problem-solving routine: pku1061 frog appointment problem-solving report symbol Description: mod: modulo operation ax 127B (mod m): (ax-B) mod m = 0, that is, the same remainder gcd (a, B) indicates the principle of solving the maximum common divisor of a and B for ax limit B (mod n): For the equation ax limit B (mod n ), the existence of ax + by = gcd (a, B), x, y is an integer. The solution of ax branch B (mod n) can be stacked by x and y. For details, refer to the MLES algorithm below. The first problem: Solving gcd (a, B) Theorem 1: gcd (a, B) =... the rest of the full text>

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.