1477: The frog's date time limit:2 Sec Memory limit:64 MB
submit:306 solved:192
[Submit] [Status] [Discuss] Description Two frogs met on the internet, they chatted very happy, so feel it is 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 inputs include 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 if it is never possible to meet then outputs a line of "impossible" Sample Input1 2 3) 4 5
Sample Output4
The following is an excerpt from Huang long, Portal: hzwer.com.
(X+MS)-(Y+ns) =lk
Deformed (n-m) s+lk=x-y
So expand GCD
The whole process of solving an integer solution of indefinite equation A * x + b * y = n using the extended Euclidean algorithm is as follows:
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), and the new indefinite equation a ' * x + b ' * y = n ', at this time gcd (a ', ' c ') = 1
2, using the extended Euclidean algorithm 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 ' * x + b ' * y = n ' A set of integer solutions;
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=0,1,2, ...)
Adjust to get the positive integer solution.
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<cmath>#definell Long Longusing namespacestd;ll gcd (ll a,ll b) {return(b==0)? A:GCD (b,a%b);}voidEX_GCD (ll a,ll b,ll &x,ll &y) { if(b==0) {x=1; y=0;return; } EX_GCD (B,a%b,x,y); ll T=x; X=y; y=t-a/b*x;}intMain () {ll a,b,c,x,y,m,n,l,t; scanf ("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&l); A=n-m;b=l;c=x-y; T=gcd (A, b); if(c%t!=0) {puts ("Impossible");return 0; } A/=t; b/=t; C/=T; EX_GCD (A,b,x,y); X= ((c*x)%b+b)%b; if(!x) x+=b; printf ("%lld\n", x); return 0;}
[BZOJ1477] The frog's date