Today in Peking University POJ to do a problem-frog dating, continuous research for several hours, submitted a total of more than 50 times, each time with the wrong anwser, or when the Limit exceeded was beaten back. I didn't have supper, and when I saw the last time I showed "Accepted", my inner restlessness suddenly stretched out. Title: 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**
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 Here can have two methods, the first slightly better understand some, but it is surprising that the same algorithm, C + + implementation of the result is the time Limit exceeded, and the C language has passed the submission of POJ. This will have to explain that although the C language is more monotonous, but because it is closer to the underlying operation, its speed is still a big advantage. Method One analysis: Two frog's initial displacement difference: X-y, jumping ability difference value m-n. Simple relative principle, we can assume that a frog is jumping, jumping ability is m-n; The initial position is X-y; that is, understanding that a frog starts at the X-y position, with m-n jumping ability, can return to 0 points after several jumps. Analysis will think, if not, the frog will be a certain rule in addition to some point outside the cycle of jumping. So we're going to mark the distance from the frog every time it jumps through the origin point, if there is a time to jump at the origin, then the answer, if a number of times, it not only did not jump to the origin, but jumped to the starting position, then it will be in this law infinite cycle jump and never jump to the original point, At this point, you can boldly output "impossible". Here's the code to implement it in C + +: #include using namespace Std; int main () { int x,y,m,n,l; cin>>x>>y>>m>>n>>l; if (m==n) { cout<< "Impossible" <<endl; return 0; } Else { int c,z; if (m>n) { C=m-n; z= (y-x+l)%l; } Else { C=n-m; z= (x-y+l)%l; } int w=z%c; int r=z/c; while (true) { if (z%c==0) { cout<<r<<endl;; Break } Else { int p=z%c+l; R+=P/C; z=p%c; } if (z==w) { cout<< "Impossible" <<endl; Break } } } return 0; } Unfortunately, this code is submitted after the display program operation timed out (c translation can actually pass, very much like C + + 's own spit groove ...) , although the algorithm is good understanding, but it seems to be more calculations and judgments are given to the computer, seemingly also have to find another way. The following method 2 is to understand the more complex than the above solution, after all, to save time and effort to the computer, but also to use more human brain cells bar. Method Two analysis: to another two frog meet, must be cumulative jump number is L of integer i times, another jump K step, namely (k*m+x)-(k+n+y) =l*i (i=0,1,2,3 ...). Simple finishing: l*i+ (n-m) *k=x-y; its model is a*i+b*k=t (a,b,t are all known integers) about whether I and K have integer solutions. Well, first put this problem, and now introduce a very critical mathematical nature: If a, B, if it is coprime, then their linear combination can get arbitrary integer, proof as follows: We first prove that a*i+b*k can be equal to 1, Then it is possible to indicate that it can be equal to any integer (directly multiplied by the corresponding multiple). Set n is the smallest positive integer that A and b linear combination can get. So that M is another number that can be linearly combined: m=f*n+r (0<=r); then F*n is a B linear combination can be obtained, then R is also, so R is only 0. In this case, we get a A, B linear combination can get the number of the set {p} in which all integers are the smallest multiple of n. The smallest number we get from another coprime B-linear combination is still n, then a*x0+b*y0=n; Another: A * (x0+1) +b*y0=n+a; a*x0+b* (y0+1) =n+b; So n+a and n+b are multiples of n, in the condition A, b coprime, then the value of n |