The date of the frog
Time Limit: 1000MS |
|
Memory Limit: 10000KB |
|
64bit IO Format: %i64d &%i64u |
Submit Status
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
Main topic:
2 frogs stand at x and y respectively, each can jump m and n meters, the total length of the dimension line L, to jump a few times will meet, if you can never meet, then output -1.
Problem Solving Ideas:
we can understand the subject as it is meant to be
Set T as the number of hops, K for an integer
(X+MT)-(Y+NT) =lk
(x-y)-(n-m) t=lk
(n-m) t+lk= (x-y)
where (n-m), L, (x-y) are known, T and K are unknown, T is the request.
So we turned this problem into an expansion of Euclid's demand for T.
Reference code:
#include <map> #include <stack> #include <queue> #include <cmath> #include <vector># include<cctype> #include <cstdio> #include <cstring> #include <iostream> #include < Algorithm>using namespace Std;const double eps=1e-10;const int inf=0x3f3f3f3f;const int maxn=1100;typedef long Long LL ; LL m,n,x,y,l; ll GCD (ll A,ll b) {return B?GCD (b,a%b): A;} void Exgcd (LL a,ll b,ll c,ll& ansx,ll& ansy) {if (b==0) {c=a; Ansx=1; ansy=0; } else {EXGCD (B,A%B,C,ANSY,ANSX); Ansy-=ansx* (A/b); }}int Main () {#ifndef Online_judge freopen ("In.txt", "R", stdin), #endif//Online_judge while (scanf ("%lld%lld%lld%lld%l LD ", &x,&y,&m,&n,&l)!=eof) {LL a=n-m,b=l,c=x-y; LL G=GCD (A, b); if (c%g| | M==n) {printf ("impossible\n"); Continue } LL Ansx,ansy; a/=g; b/=g; c/=g; EXGCD (A,b,c,ansx,ansy);//a*ansx+b*ansY=c Ansx*=c; Ansx%=b; while (ansx<0)//ANSX may be negative ansx+=l; printf ("%lld\n", ANSX); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 1061 frog Dating (expanding Euclid)