POJ 1061: The frog's date

Source: Internet
Author: User
Tags greatest common divisor

The date of the frog
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 95878 Accepted: 17878

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 is the solution of an equation, namely (x+m*t)-(y+n*t) =p*l. Satisfies the minimum t of the equation. Transform the equation into (n-m) *t+p*l=x-y. X,y,n,m is known in this equation.

At first do not know how to expand Euclid's method, has been traversed to determine whether there can be qualified values, submitted 44 times or tle ...

(Excerpt from Baidu Encyclopedia) Extended Euclid:

The extended Euclidean algorithm is used to solve a set of X, y in known a, b to satisfy the Bézout equation: ax+by = gcd(A, B) =d (the solution must exist, according to the correlation theorem in number theory).

Euclidean algorithm

OverviewEuclidean algorithmalso known asEuclidean method, used to calculate the greatest common divisor of two integers A/b. Its computational principle relies on the following theorem:the GCD function is used to beg (A, b)Greatest common divisorthe. Basic properties of the GCD function:gcd (A, B) =gcd (b,a) =gcd (-a,b) =gcd (|a|,|b|)Formula Expressiongcd (A, a) =gcd (b,a mod b)Proof: A can be expressed as A = kb + R, then r = a mod bSuppose D is a A, BNumber of conventions, you haveD|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), thenD | 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 its greatest common divisor is necessarily equal, to be proven. C + + language implementation
<span style= "FONT-SIZE:12PX;" > #include <iostream> #include <cstdio>using namespace std;int x,y,q;void extend_eulid (int a,int b) {if (b= =0) {X=1;y=0;q=a;return;} Extend_eulid (b,a%b); int temp=x;x=y;y=temp-a/b*y;} int main () {int A,b;cin>>a>>b;extend_eulid (A, B);p rintf ("%d= (%d) *%d+ (%d) *%d\n", q,x,a,y,b); return 0;} </span>
extension Algorithm

for a non-negative integer A,B,GCD (A, B) that is not exactly 0, the greatest common divisor of A/b is bound to exist . number of x, Y, makes gcd (A, b) =ax+by.

C + + language implementation

<pre name= "code" class= "HTML" >int exgcd (ll a,ll b,ll &x,ll &y) {    if (a==0)    {        x=0;y=1;        return b;    }    else    {        ll tx,ty;        ll D=EXGCD (b%a,a,tx,ty);        x=ty-(b/a) *tx;        Y=TX;        return D;    }}
Understanding of the method of solving X, yset A>b. 1, obviously when B=0,GCD (A, b) =a. At this time x=1,y=0;2,ab<>0 ax1+ by1= gcd (A, B), bx2+ (a mod) y2= gcd (b,a mod b); According to the simple Euclidean principle there is gcd (b) = gcd (b,a mod b); then: ax1+ by1= BX (a mod b) y2, i.e.: ax1+ by1= bx2+ (A-[A/b] * B.) y2=ay2+ bx2-[A/b] * BY2; that is, ax1+ by1 = ay2+ B (x2-[A/b] *y2); According to the identity theorem : X1=y2; y1=x2-[A/b] *y2; so we get a way to solve x1,y1: The value of X1,y1 is based on x2,y2. The idea above is defined recursively, because GCD constant recursive solution will have a time b=0, so recursion can end.

Code:

#include <iostream> #include <vector> #include <string> #include <cstring> #include < Algorithm>using namespace Std;long long d;void ex_gcd (Long long A,long long B,long long &xx,long long &yy) {if (b ==0) {xx=1;yy=0;d=a;//d is the smallest convention number}ELSE{EX_GCD (B,A%B,XX,YY) for the finding of a, b; long long t=xx;xx=yy;yy=t-(A/b) *yy;}} int main () {Long long x,y,m,n,l,xx,yy;cin>>x>>y>>m>>n>>l;ex_gcd (N-M,L,XX,YY); if (X-y %d)//If the right side of the equation equation cannot be divided by the minimum number of conventions, the equation has no solution. {cout<< "Impossible" <<ENDL;} else{xx=xx* (x-y)/d); The Xx,yy is the solution when the equation equals the minimum number of conventions, when the solution is enlarged to (x-y) *d times.        long long r=l/d;        xx= (xx%r+r)%r;//the minimum value to be solved here        Cout<<xx<<endl;} System ("pause"); return 0;}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 1061: The frog's date

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.