POJ-1061 Date of Frogs (Extended Euclid)

【Title description】

The two frogs met online, and they had a good time chatting, so they felt it was necessary to meet. They were delighted to find that they lived on the same line of latitude, so they agreed to hop westward until they met. But before they set off, they forgot one very important thing. They neither asked about the characteristics of each other nor agreed on a specific location to meet. However, the frogs are very optimistic. They feel that as long as they keep jumping in a certain direction, they will always meet each other. But unless the two frogs jumped to the same spot at the same time, they would never meet. To help the two optimistic frogs, you are asked to write a program to determine if and when the two frogs can meet.
We call these two frogs frog A and frog B respectively, and specify the origin at 0 degrees east longitude on the latitude line, the positive direction from east to west, and the unit length is 1 meter, so that we get a number line that is end-to-end. . Let the coordinate of the starting point of frog A be x, and the coordinate of the starting point of frog B is y. Frog A can jump m meters at a time, frog B can jump n meters at a time, and both frogs take the same amount of time to jump once. The total length of the latitude line is L meters. Now you need to ask them to jump a few times before they meet.

The input consists of only one line of 5 integers x, y, m, n, L, where x≠y < 2000000000, 0 < m, n < 2000000000, 0 < L < 2100000000.

Output the number of jumps required to meet, or a line of "Impossible" if it is never possible to meet

Sample Input:

1 2 3 4 5

Sample Output:

4

[Analysis]
This question can be seen to be exgcd, but it is impossible to write. Then I messed around for a long time qwq The
meaning of the question is obvious, x+(m t)%L == y+(n t)%L, let you find the smallest t. Then this formula can be sorted into t (mn)+k L == yx.
Let a=mn, b=L, c=yx, X=t, Y=k, the above formula can be sorted into the form of aX+bY=c, so it can be solved by exgcd.

【Code】

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325924913&siteId=291194637