Problem solving report POJ1061 frog dating
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
The main idea of the topic: slightly.
Analysis: First I have to say that the problem seems to have a bug, the topic did not say X<y and M<n bar. I have tested the code from many other places, there is this problem, and the data of the topic to indulge this kind of data, is it my test instructions understand wrong, also please everyone treatise? Error Data: 2 1 4 3 5 The answers to the questions on the network are all wrong. 1 2 4) 3 5
Okay, back to the chase. The problem is to expand the gcd of the entry, it is easy to get the equation (X+MT)-(Y+NT) =PL, said t seconds after the two frogs meet, slightly deformed, get (m-n) t-pl= y-x * *, where t,p is unknown, this is not an indefinite equation? Obviously extended gcd, if does not expand GCD please Baidu, I think speak very well. According to AX+BY=GCD (A, B) there must be an integer solution x, Y. Then the * * equation has an integer condition (y-x)%GCD (A, B). At this point the solution is extended gcd (y-x)/GCD (A, B). (Think about why?) Equivalent to both sides of the equation expand/Shrink k=(y-x)/GCD (A, B) times.
but we need to ask for the smallest integer. Because the extended GCD is a set of integer solutions, this requires us to adjust the answer. The specific method isar = (ar% abs (b/g) + ABS (b/g))% abs (b/g); AR is the time to find out T
You can limit the answer to 0~abs (b/g) and why ABS (b/g), because if you go through b/g, you return to the original state, equal to the beginning, completely white jump.
on the code:
#include <iostream> #include <cmath>using namespace std;typedef long Long ll;ll abs (ll N) { if (n>=0) return n; else Return-n;} ll EXGCD (ll A, ll B, ll& X, ll& y) {if (b = = 0) {x = 1; y = 0;return A;} ll ans = EXGCD (b, a%b, x, y); ll tem = X;x = Y;y = Tem-a/B*y;return ans;} int main () {ll x, y, M, N, l;while (cin >> x >> y >> m >> n >> L) {ll A = m-n;ll B =-l;ll ar , Br;ll g = EXGCD (A, B, AR, BR); if (y-x)%g! = 0| | M==n) {cout << "impossible" << Endl;} Else{ar = ar* (y-x)/G;ar = (ar% abs (b/g) + ABS (b/g))% abs (b/g); cout << ar << Endl;} return 0;}
Start number theory, prepare for the provincial races, come on!
Problem solving report POJ1061 frog dating