[POJ 1061]--Frog's date (extended Euclid)

Source: Internet
Author: User
Tags greatest common divisor

the date of the Frog
Time Limit: 1000MS
Memory Limit: 10000K


Title Link: http://poj.org/problem?id=1061

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

Input includes only one line of 5 integers x,y,m,n,l, where x≠y < 20
00000000,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

Problem-solving ideas: First, the problem is transformed into the relevant mathematical equation: (n-m) t+pl=x-y, that is, two yuan an indefinite equation at+bp=c, according to the correlation theorem of number theory, when C%GCD (A, A, b) ==0 equation has an integer solution,

Otherwise, there is no solution to this problem.

Refer to the following theorem:

theorem 1 gcd (A, b) is the smallest positive integer of the linear combination of ax+by, x,y∈z;
theorem 2 if ax+by=c,x,y∈z, then c%gcd==0;
theorem 3 If a, B is a positive integer of coprime, C is an integer and the equation ax+by=c

(1) There is a set of integer solutions x0,y0 the whole integer solution of this equation can be expressed as x=x0+bt;y=y0-at;t∈z;

(2) The equation At+bp=c the left and right sides of the same divided by gcd (A, b), get the A1T+B1P=C1, and then solve the smallest positive integer linear combination of a group of solutions X1,y1,

Then a set of solutions of the equation is t=x1*c1,p=y1*c1, according to the (2) type can obtain T's minimum positive integer solution to (T%B1+B1)%b1, which is solved when feasible,

Note: Because the data given is large, it is all used __int64.

The code is as follows:

1#include <iostream>2 using namespacestd;3 4 __int64 T, p;5 __int64 GET_GCD (__int64 A, __int64 b) {6     return!B?A:GET_GCD (b, a%b);7 }8 9 voidEXTENDED_GCD (__int64 A, __int64 b) {Ten     if(!b) { Onet =1; Ap =0; -     } -     Else{ the __int64 temp; -EXTENDED_GCD (b, a%b); -temp = t-a/b*p; -t =p; +p =temp; -     } + } A  at intMain () { - __int64 x, y, N, M, L, GCD; -Cin >> x >> y >> m >> n >>L; -     if(M = =N) { -cout <<"Impossible"<<Endl; -         return 0; in     } - __int64 A, B, C, C1; toA = N-m; +b =L; -c = x-y; theGCD =GET_GCD (A, b); *C1 = c%gcd; $     if(C1! =0){Panax Notoginsengcout <<"Impossible"<<Endl; -         return 0; the     } +C/=gcd; AA/=gcd; theb/=gcd; + EXTENDED_GCD (A, b); -T *=C; $P *=C; $T = (t%b + b)%b; -cout << T <<Endl; -     return 0; the}
View Code

Of course, you can also write this:

D is the greatest common divisor of n-m and L, X is (N-M)/d to the inverse of l/d, i.e. ((N-M)/d) * x≡1 (mod l/d), i.e. ((N-M)/d) * x + (l/d) * y = 1 A set of solutions,

So ((M-M)/d) * x + (l/d) * y = (x y)/d is a set of solutions for x0 = (x-y)/d * x. This is also a set of solutions (n-m) * x+ L * y = (xy). (The cycle in the code represents D).

1#include <iostream>2 using namespacestd;3 LonggcdLongALongb) {4     return!b? A:GCD (b, a%b);5 }6 intMain () {7     Longx, Y, M, N, L, I, cycle;8      while(Cin >> x >> y >> m >> n >>L) {9         if(M > N) cycle = (y-x + L)%L;Ten         Else{ OneCycle = (x-y + L)%L; A Swap (M, n); -         } -         if(n = = m | | cycle%gcd (L, M-N)) { thecout <<"Impossible"<<Endl; -             Continue; -         } -          for(i =0;; i++){ +             if((I*l + cycle)% (m-n) = =0){ -cout << (i*l + cycle)/(M-n) <<Endl; +                  Break; A             } at         } -     } -     return 0; -}
View Code

[POJ 1061]--Frog's date (extended Euclid)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.