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
The other T is the total number of times, which lists the equation: (m-n) *t+l*k=y-x. where k is an integer. Another a=m-n, B=l, C=y-x, Xx=t, Yy=k. The title becomes the standard extended Euclidean form.
The subject exists an implied condition, that is, the number of times XX must be the smallest non-negative integer.
Set A,b,c as any integer, G=GCD (A, b), a set of interpretations of the equation Ax+by=g (x0,y0), when C is a multiple of G ax+by=c a set of explanations (x0*c/g, y0*c/g), when C is not a multiple of G no integer solution.
First, the AX+BY=GCD (A, b) was obtained by expanding Euclid. The solution of XX re-multiplication (C/GCD (A, B)), you get a solution. By a certain reason: if gcd (A, b) = d, then the equation is ax+by=c and there is a unique solution on [0, b/d-1].
The final non-negative and minimum xx can be obtained.
The above is summed up to seek to expand Euclid's X of the smallest non-negative integer solution.
#include <stdio.h>typedef long long LL; ll EGCD (ll a,ll b,ll& d,ll& xx,ll& yy) { if (!b) d=a,xx=1,yy=0; else {EGCD (b,a%b,d,yy,xx); yy-=xx* (A/b);}} int main () { LL x,y,m,n,l,xx,yy,d; scanf ("%i64d%i64d%i64d%i64d%i64d", &x,&y,&m,&n,&l); LL a=m-n,b=l,c=y-x; if (a<0) a=-a,c=-c; Guaranteed a,b,d, for positive egcd (A,B,D,XX,YY); Extended Euclid if (c%d!=0) printf ("impossible\n"); else { xx=xx* (C/D); Enlarge C/D times B/=d; xx= (xx%b+b)%b; There is a unique solution within [0, b/d-1]. printf ("%i64d", XX); } return 0;}