Although this problem is very simple, but there is a comparison pit, that is, GCD is not necessarily 1, there may be other numbers. So you cannot return 1, but return a;
The problem is dry:
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 row 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 one line if it is never possible to meet"Impossible"Sample Input1 2 3 4 5Sample Output4
Code:
#include <iostream>#include<cstdio>#include<cmath>#include<ctime>#include<queue>#include<algorithm>#include<cstring>using namespacestd;#defineDuke (I,a,n) for (int i = a;i <= n;i++)#defineLV (i,a,n) for (int i = a;i >= n;i--)#defineClean (a) memset (A,0,sizeof (a))Const intINF =1<< -; typedefLong LongLl;typedefDoubledb;template<classT>voidRead (T &x) { CharC; BOOLOP =0; while(c = GetChar (), C <'0'|| C >'9') if(c = ='-') op =1; X= C-'0'; while(c = GetChar (), C >='0'&& C <='9') x= x *Ten+ C-'0'; if(OP) x =-x;} Template<classT>voidwrite (T x) {if(X <0) Putchar ('-'), x =-x; if(x >=Ten) Write (X/Ten); Putchar ('0'+ x%Ten);} ll X,y,m,n,l;ll EXGCD (ll a,ll b,ll&x,ll &y) { if(!b) {x=1; Y=0; returnA; } ll T= EXGCD (b,a%b,y,x); Y-= (A/b *x); returnt;}intMain () {read (x); Read (y); read (m); Read (n); Read (l); LL b= N-m,a = x-y,x1,y1; if(b <0) {b= -b; A= -A; } ll ans=EXGCD (b,l,x1,y1); if(A% ans! =0) printf ("impossible\n"); Else{ll F= L/ans; printf ("%lld\n", ((x1 * (A/ans))% f + f)%f); } return 0;}
POJ1061 the frog's date exgcd