The date of the frog
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 96066 |
|
Accepted: 17907 |
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
Test instructions: More obvious, Chinese topic.
Analysis: To make two frogs can meet, then they walk the total distance of the difference must be the latitude total length l of integer times. Because they jump once to spend the same length of time, just jump not the same distance, so we set them all jump T, their starting position is x and Y, can jump distance is m,n, then there is (x+m*t)-(y+n*t) =k*l; (x-y) + (m-n) *t=k*l into linear The congruence equation is, (m-n) *t = (x-y) (mod L)
Form: a*x=c (mod b) equation has solution, when and only if GCD (A, B) | (X-y), the equation has a solution. Solving the linear congruence equation can be solved by expanding Euclid.
There are three theorems in the extended application of Euclid algorithm:
Theorem One: if d = gcd (A, B), the positive or negative integers k and L must be found, making d = a*x+ b*y.
Theorem Two: If gcd (A, b) = 1, then the equation ax≡c (mod b) has a unique solution on [0, B-1].
Theorem Three: If gcd (A, b) = d, then the equation ax≡c (mod b) has a unique solution on [0, b/d-1].
Proof: The above congruence equation is equivalent to ax + by = C, if there is a solution, divided by D on both sides, there is a/d * x + b/d * y = C/D, that is a/d * X≡C/D (mod b/d), obviously gcd (A/d, b/d) = 1, so by the theorem 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], i.e. ax≡c (mod b) has a unique solution on [0, b/d-1].
If you get a special solution x for ax≡c (mod b), then r = B/GCD (A, B), we know that X has a unique solution on [0, r-1], so you can find the smallest nonnegative integer solution x with x = (x% r + R)% r! (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].
How the EXGCD () works:
For non-negative integers A, b, not exactly 0. GCD (A, B) represents the greatest common divisor of A, b . Then there is an integer x, y makes gcd (A, b) = a * x + b * y;
Set a > B
① , when b = 0 ,gcd (A, b) = A, at which point x = 1, y = 0;
② , when A * b <> 0 ,
Set A * x + b * y = gcd (A, b); (1)
b * x0 + (a% b) * y0 = GCD (b, a% B); (2)
By the naïve Euclidean formula ; GCD (A, b) = gcd (b, a% b);
( 1),(2) A * x + b * y = b * x0 + (a% b) * Y0
= b * x0 + (a–a/b * b) * y0
= A * y0 + (x0–a/b * y0) * b
so x = y0, y = x0–a/b * y0;
#include <iostream> #include <stdio.h> #include <string> #include <cstring> #include <cmath > #include <algorithm>typedef long ll;using namespace Std;ll exgcd (ll a,ll b,ll &x,ll &y) { ll r,t ; if (b==0) { x=1; y=0; return A; } R=EXGCD (b,a%b,x,y); t=x; x=y; Y=t-a/b*y; return r;} int main () { ll x,y,m,n,l; ll D,r,xx,yy; while (~SCANF ("%lld%lld%lld%lld%lld", &x,&y,&m,&n,&l)) { d=exgcd ((n-m), l,xx,yy);// Greatest common Divisor if ((x-y)%d)//determine if the linear congruence equation has a solution { printf ("impossible\n"); Continue; } xx=xx* (x-y)/d); r=l/d; xx= (xx%r+r)%r; printf ("%lld\n", XX); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 1061 frog Dating Extended Euclid