Topic:
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
Exercises
Here to apply the Ben Comeon4mydream of God, Orz
Life second ACM problem, I do not know to say God horse, Chinese problem really is not to mess with, a few days ago a recursive and the group of Chinese questions took me three days, this problem took me a night time!
Do not do at the beginning, the Internet to read the report of the problem is number theory. Then learn AH learning Ah, after writing found only 21 lines of code, but it took me a night time ah! Is it too hard or am I too water? 55555~~~
To do this problem, we must first understand a few theorems, in order to protect the rigor of the one by one I have to prove it (heart disease, do not prove that the words used is not practical ah!). Or I won't have to run to GDCC! )
Alas, the proof is too dull, first talk about the test instructions bar.
Male frog at the beginning of the X position, the female frog in the y position. Every time the male frog jumps M m, the female frog jumps n meters at a time, and all jumps to the right. The earth meridian length is L, then the earth is round, that is, jump to L, l+1, l+2 ... Actually jump to 0, 1, 2. Male frog want to chase mother Frog, ask how many times after they can jump together. If they can never meet, then output impossible (poor Ah!) )
Obviously, is to ask for a k, so x + k*m≡y + k*n (mod L), wood is wrong? At least I think so! Then to the equation simplification, it becomes (n-m) * K≡X-Y (mod L). Then the equation is actually equivalent to (n-m) *k + l*s = x-y. This is the model of ax + by = C for Integer x.
The integer x solution that requires ax + by = C is not so simple, otherwise I won't have to spend a whole night! First of all, set d = gcd (A, b), the equation is divided by D to get A/d * x + b/d * y = C/D, it is clear that a is divisible by D, B is divisible by D, and X, Y are integer solutions, so the requirement of C/D is also an integer. If c is not divisible by D, of course it is impossible. Otherwise, if we can find the Ax0+by0=d solution x0 and y0, then the two sides multiplied by C/D namely a (C/D * x0) + b (C/D * y0) = c, you can get the original equation of the solution x = (C/D * x0), y = (C/D * y0) slightly.
Hey, hey, wait a minute, how do you know ax0 + By0 = d must have a solution? This has to pass rigorous proof:
Theorem One: if d = gcd (A, b), you must be able to find positive or negative integers k and l, making d = a*k + b*l.
Proof : because gcd (A, 0) = A, we can assume that b≠0, so that by even we can write
A = b*q1 + r1
b = r1*q2 + r2
R1 = r2*q3 + R3
......
By the first type there is R1 = a-q1*b, so R1 can be written in K1*a + l1*b form (at this time K1 = 1, L1 =-q1). By the second type there is r2 = B-R1*Q2 = B-(k1*a + l1*b) *q2 =-q2*k1*a + (1-q2*l1) *b = K2*a + l2*b.
Obviously, this process can be repeated through this string remainder until an expression of Rn = K*a + L*b, which is d = k*a + L*b, is obtained, which is what we want to prove.
So say, ax0 + by0 = d must have a solution! So how do we find x0 and y0? To use a kind of algorithm called Extended Okiri (NND, the first time I heard, I was too weak ah ~ ~ ~)!
The method is as follows: Because GCD (A, b) = gcd (b, a%b) (this does not have to prove it??? All the people of the Earth know! ), there is ax0 + by0 = gcd (A, b) = gcd (b, a%b) = Bx1 + (a%b) y1, and a%b can be written a-a/b*b (A/b*b is not equal to a ah!). Remember for this SB problem is also scolded ~), so =bx1 + (a-a/b*b) y1 = ay1 + b(x1-a/b*y1), so if we find gcd (b, a%b) = Bx1 + (a%b) Y1 of X1 and Y1, then x0 = Y1,y0 = (x1-a/b*y1) can be calculated by observing. So how do we ask X1 and y1? Of course it is x2 and Y2, the same way. Always ask GCD (AN, 0) = an*xn + 0 * yn, this season xn=1,yn=0 is done, you can ask Xn-1 and yn-1, then xn-2 and yn-2, and then always find x0 and y0.
So get ax0 + by0 = gcd (A, B) to find an integer x0, y0 the extension Okiri algorithm (to be bored here to die!) I'm almost about to give up this step! Weak, weak, 55555~~~)
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;
}
Long long? Why a long long? Because I gave wrong answer with INT! Then Baidu check to know there is a long long this thing, that is, int is 32 bits, long long is 64 bits, can represent a larger integer. This is disgusting, why bully me??!! But then you can find x0 and y0.
Then ax + by = c, judging c if not divisible by D is impossible, otherwise the x = C/D * X0 can get an X solution.
But there are countless solutions that meet ax + by = c. For God's horse? Ax + by = c is actually equivalent to Ax≡c (mod b), right? If I get a special solution x*, then add several times B or the solution of this equation, because a (x*+k*b) = ax* + a*k*b≡c + 0≡c (mod b). Thus the equation in [0, B-1] must have an integer solution (if less than 0, you add several times B ah, you can keep it in the 0~b-1, if it is greater than b-1, you subtract several times B ah, it also remains 0~b-1).
So how do you find the smallest nonnegative integer x? There are also two theorems:
Theorem Two: If gcd (A, b) = 1, then the equation ax≡c (mod b) has a unique solution on [0, B-1].
Proof: by theorem One knows, the total can be found or positive or negative integers k and L make A*k + b*l = gcd (A, b) = 1, that is, we can find out the solution x0 of ax≡1 (mod b). Of course, both sides are multiplied by C with a (cx0) ≡c (mod b), so there is x = cx0 is the solution of Ax≡c (mod b). Since the addition or subtraction of several times B is the solution of the equation, X has a solution on [0, B-1]. So how to determine its uniqueness? It took me one hours to finally prove it, and the way to prove it is that if X1 and X2 are both [0, B-1] on the solution, then there is ax1≡c (mod b), ax2≡c (mod b), the subtraction of a (x1-x2) ≡0 (mod b), that a (X1-X2) can be divisible by B. But gcd (A, b) = 1 Ah! So there is no common language between a and B can communicate, so can only say (x1-x2) by B whole except. But X1 and x2 are all in [0, b-1], so x1-x2 also in [0, b-1], so can only say x1-x2=0, so x1=x2. This proves the uniqueness of understanding!
This theorem is just to prove the theorem three convenient, theorem three is kingly:
Theorem Three: If gcd (A, b) = d, then the equation ax≡c (mod b) has a unique solution on [0, b/d-1].
Proof: As stated above, this damned equation is equivalent to ax + by = c, if there is a solution, the two sides are divided by D, there is a/d * x + b/d * y = C/D, i.e. A/d * X≡C/D (mod b/d), obviously gcd (A/d, b/d) = 1, so by the theorem The two know that X has a unique solution on [0, b/d-1]. So the x ax + by = C has a unique solution on [0, b/d-1], that is, ax≡c (mod b) has a unique solution on [0, b/d-1], proof!
With a couple of damn theorems, I finally solved the problem of least nonnegative integers. If I get a special solution x for ax≡c (mod b), then I make r = B/GCD (A, B), I know that X has a unique solution on [0, r-1], so I use x = (x% r + R)% r to find the smallest nonnegative integer solution x! (X% r may be negative, at which point it remains within [-(R-1), 0], positive values remain in [0, R-1]. Plus R is kept in [1, 2r-1], so the model R is in [0, R-1].
Experience:
In summary, the AX+BY=C (ax≡c (mod b)) x minimum solution is obtained:
The GCD (A, B) =d, and X, Y are determined by the method of the Euclidean division, and the first is whether C is divisible by D, if no solution is obtained.
Then the next will be obtained x multiplication (C/D), so that r=b/d, will x%r+r%r can;
Code:
#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<ctime>#include<cctype>#include<cstring>#include<string>#include<algorithm>using namespacestd;Long Longx,y,m,n,l,d;Long Longx, y;voidgcdintAintb) { if(b==0) {D=A; X=1; Y=0; return; } Else{gcd (b,a%b); Long Longtemp=X; X=x; Y=temp-a/b*x; }}intMain () {//freopen ("a.in", "R", stdin);scanf"%lld%lld%lld%lld%lld",&x,&y,&m,&n,&l); Long Longa=n-m; Long Longb=l; Long Longc=x-y; GCD (A, b); if(c%d!=0) {cout<<"Impossible"<<Endl; return 0; } Long Longr=b/D; printf ("%lld\n", (x* (C/D)%r+r)%0); return 0;}
Algorithm review-expanding Euclid (poj1061)