POJ 1061 date of the Frog

Source: Internet
Author: User
Tags greatest common divisor

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

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

Idea: (x+t*n)%l== (y+t*m)%l; the smallest integer solution; The emphasis is on the solution x*a+t*l=b;

First a bout of searching;

Other people's ideas: expand Euclidean algorithm;

/* First, the Euclidean algorithm: Greatest common divisor (greatest common divisor) abbreviated to GCD.

⒈ R is a A/b derived remainder (0≤r<b) if r= 0, the algorithm ends; b is the answer. ⒉ Interchange: Place A←b,b←r, and return to the first step. Extending Euclid's theorem
for a a,b,gcd (a, b) with a nonnegative integer that is not exactly 0, the greatest common divisor. Then there is the whole
The number of x, Y makes gcd (a, b) =ax+by. (It seems that x, Y is the only one, which I am not too clear about).
The most important thing is how to understand the following code:
#inc lude<iostream>
using namespace Std;
int x,y,q;
void Extend_eulid (int a,int b)
{
if (b==0)
{
X=1; y=0; Q=a;
}
Else
{
Extend_eulid (B,A%B);
int temp=x;
X=y; Y=temp-a/b*y;
}
}
int main ()
{
int A, B;
cin>>a>>b;
if (a<b) swap (A, b);
Extend_eulid (A, b);
printf ("%d= (%d) *%d+ (%d) *%d\n", q,x,a,y,b);
}
Understanding of the method of solving X, y
We may as well set up a>b.
1, obviously when B=0,GCD (A, b) =a. At this time x=1,y=0;
When 2,ab<>0
Set Ax1 +by1 =gcd (A, b);
Bx2 + (a%b) y2 =gcd (b,a%b);
According to the naïve Euclid principle there are gcd (A, B) =gcd (B,A%B);
Then: Ax 1 +by1 =bx2 + (a%b) y2;
That is: Ax 1 +by1 =bx2 + (A-(A/b) *b) y2 =ay2 +bx2-(A/b) *by2;
According to the identity theorem: X1 =y2; Y1 =x2-(A/b) *y2;
This gives us a way to solve x1,y1: The value of X1,y1 is based on X2,y2.
The above thought is recursive definition, because GCD constant recursive solution will have a time b=0, so recursion can
End.
With this analysis, the understanding of the Extend_eulid function is not difficult. */ get a little tip freopen ("Input.txt", "R", stdin);
function: Simply put, is to achieve redirection. Directs several predefined standard stream files (stdin, stdout, stderr) to the file specified by path.
The role of Freopen ("Debug\\in.txt", "R", stdin) is to redirect stdin to the Debug\\in.txt file so that it is used in CIN or
Data is not extracted from the standard input stream when it is entered with scanf. Instead, get the input from the In.txt file. Just paste the input in advance
In.txt, the debugging is much more convenient.



Understanding Euclid, the problem was half solved. The only thing to do is to solve the common ax+by=d(D is not necessarily gcd (a, b)).
Detailed analysis process See http://blog.csdn.net/SwordHoly/article/details/4423543
Finally, the solution is kmin= (k0* (b/d)) mod (l/d); tmin= (t0*) mod (A/d);
#include <iostream>#include<stdio.h>#include<cmath>using namespacestd;Long Longx,y,m,n,l,k,t,q;voidEXTEND_GCD (Long LongALong Longb) {    if(b==0) {k=1; t=0; q=A; }    Else{EXTEND_GCD (b,a%b); inttemp=K; K=t;t=temp-(A/b) *T; }}intMain () {Freopen ("In.txt","R", stdin); CIN>>x>>y>>m>>n>>m; Long Longb; A=m-N; b=y-x; if(a<0) {a=-a;b=-b;}    EXTEND_GCD (a,l); if(b%q!=0) cout<<"Impossible"<<Endl; Else{k=k*b/Q; T=t*b/Q; L=l/Q; if(k>=0) K=k%L; Elsek=k%l+m; if(k==0) K=1; cout<<k<<Endl; }    return 0;}

POJ 1061 date of the Frog

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.