Inverse-the frog's date

Source: Internet
Author: User
Tags greatest common divisor

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


------------------------------------------------I'm the split line ^_^------------------------------------------------------- ----

This problem is actually extended Euclidean algorithm-solving indefinite equation, linear congruence equation. after the S-step, the two frogs meet to

Inferior type: (x+m*s)-(y+n*s) =k*l (k belongs to integer)

slightly changed to form: (n-m) *s+k*l=x-y, make n-m=a, k=b,x-y=c, ie A*s+b*l=c as long as there is an integer solution, two frogs can meet . ,

Or not.


is a multiplication inverse of the topic, I did not control what principle, directly learn the use of templates, because it is too difficult to understand, looked at half a day
not yet fully understand the expansion of Euclid = =, in short There are many problems in solving the equations of indefinite equations on the net, but none of them say
All, only said a part, looked at a lot of after really understand the indefinite equation to solve the whole process, the steps are as follows:

An integer solution that asks A * x + b * y = n.

1, first calculate gcd (A, b), if n can not be divisible by gcd (A, B), then the equation has no integer solution; otherwise, the equation is divided by gcd (A, A, b) on both sides,

Get the new indefinite equation a ' * x + b ' * y = n ', at this time gcd (a ', B ') = 1;

2, using the Euclidean algorithm described above to find the equation a ' * x + b ' * y = 1 A set of integer solution X0,y0, then n ' * x0, n ' * y0 is the equation

A set of integer solutions of a ' * x + b ' * y = n ';

3, according to the correlation theorem in number theory, can get the equation a ' * x + b ' * y = n ' of all the integer solution is:

x = N ' * x0 + b ' * t
y = N ' * y0-a ' * t
(t is an integer)

The above solution is the whole integer solution of a * x + b * y = n.

Add a simple way to see a website, the smallest nonnegative integer solution is (n ' * x0% B ' + B ')% B '.

#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<vector>#include<queue>#include<cctype>UsingNamespace Std;#define INT __int64#define INF0x3f3f3f3f
Int Exgcd(IntA,IntB,Int &x,Int &y){/* Note that the problem requires the solution of the equation when the greatest common divisor, the number of conventions alone will timeout = =*/If(b==0){x=1; Y=0;Return a;}Intgcd=Exgcd(bA% bXY);IntT= y; Y= X-AP =* Y; X= T;return GCD;}IntMain(){Freopen ("Input.txt", "R", stdin);IntXYMN, L;While(scanf("%i64d%i64d%i64d%i64d%i64d",&x,&y,&m,&n,&l)! = EOF){IntA= N-M;IntB= L;IntC= X-Y;IntX1, y1;Intgcd=Exgcd(AB, x1, y1);If(c% GCD){Printf("Impossible\ n");Continue;} A/= gcd/= gcd C /= gcd; int ans = (c Span class= "Sh-symbol" >* x1 % b + b) % bprintf ( "%i64d \n "); return 0;< Span class= "Sh-cbracket" >

Inverse-Frog's appointment

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.