POJ 1061 frog appointment (Extended Euclidean), poj1061

Source: Internet
Author: User

POJ 1061 frog appointment (Extended Euclidean), poj1061

Frog appointment

Time Limit:1000 MS   Memory Limit:10000 K
Total Submissions:91753   Accepted:16849

Description

The two frogs met each other on the Internet. They had a good chat, so they thought it was necessary to meet each other. They are happy to find that they live on the same latitude line, so they agreed to jump westward until they met each other. However, before they set out, they forgot a very important thing. They did not know the characteristics of the other party, nor agreed on the specific location of the meeting. However, frogs are optimistic. They think that as long as they keep jumping in a certain direction, they will always meet each other. However, unless the two frogs jump to the same point at the same time, they will never be able to meet each other. To help the two optimistic frogs, you are asked to write a program to determine whether the two frogs can meet and when.
The two frogs are called Frog A and frog B respectively, and The 0th degree of the latitude line is the origin, from East to West is the positive direction, the unit length is 1 meters, in this way, we get a number axis that is connected at the beginning and end. Set the starting coordinate of frog A to x, and that of frog B to y. Frog A can jump m meters at A time, and frog B can jump n meters at A time. It takes the same time for the two frogs to jump at A time. The total length of the latitude line is L meters. Now you need to find out how many times they will be met.

Input

The input contains only five integers x, y, m, n, and L in a row. x = y <2000000000,0 <m, n <2000000000,0 <L <2100000000.

 

Output

Outputs the number of jumps required for the meeting. If the meeting is never possible, an "Impossible" line is output"

Sample Input

1 2 3 4 5

Sample Output

4

Analysis: If two frogs meet after S and A is faster than B, then (x + S * m)-(y + S * n) = k * L (k = 0, 1, 2 ......).

(N-m) * S + k * L = (x-y) after the merge operation ). make a = n-m, B = L, c = x-y, that is, a * S + B * L = c (1)

If expression (1) has an integer, the two frogs can meet each other. Otherwise, they cannot. Therefore, the problem is converted to the integer solution of the equation.

First, calculate d = gcd (a, B). If d cannot be divisible by c, the equation has no integer solution. Otherwise, divide the equation by d at the same time and obtain a's + B '* L = C'. In this case, gcd (A', B') = 1.

Then, we use the Extended Euclidean Algorithm to obtain a group of Integers of a' * S + B '* L = 1' to solve x0 and y0. Then (c' * x0, C' * y0) is a group of Integer Solutions of a' * S + B '* L = C, all solutions of a' * S + B '* L = C' are (x = C' * x0 + B' * k, y = C' * y0-a' * k). It is also all solutions for a * S + B * L = c.

 

 

 

 

 

 

 

# Include <cstdio> # include <cmath> typedef long LL; ll x, Y, M, N, L; LL gcd (LL a, LL B) {while (B) {LL r = a % B; a = B; B = r;} return a;} void extend_gcd (LL a, LL B, LL & x, LL & y) {if (B = 0) {x = 1; y = 0; return;} else {extend_gcd (B, a % B, x, y); LL tmp = x; x = y; y = tmp-a/B * y;} int main () {LL x, y, d; while (~ Scanf ("% I64d % I64d % I64d % I64d % I64d", & X, & Y, & M, & N, & L) {LL a = N-M; LL B = L; LL c = X-Y; d = gcd (a, B); if (c % d! = 0) {printf ("Impossible \ n"); continue;} a/= d; B/= d; c/= d; extend_gcd (a, B, x, y); LL t = c * x % B; if (t <0) t + = B; printf ("% I64d \ n", t);} return 0 ;}View Code

 

 

# Include <iostream> typedef long LL; using namespace std; ll x, Y, M, N, L; void extend_gcd (LL a, LL B, LL & d, LL & x, LL & y) {if (B = 0) {d = a; x = 1; y = 0;} else {extend_gcd (B, a % B, d, y, x); y-= x * (a/B) ;}} int main () {while (cin> X> Y> M> N> L) {LL d, x, y; extend_gcd (N-M, L, d, x, y); if (X-Y) % d = 0) {LL p = L/d; x = (X-Y)/d * x; x = (x % p + p) % p; // Prevent cout <x <endl;} else cout <"Impossible" <endl;} return 0 ;} /* d is the maximum common divisor of N-M and L, x is the inverse element of (N-M)/d to L/d, that is (N-M)/d) * x fill 1 (mod L/d), I .e. (N-M)/d) * x + (L/d) * y = 1 of a group of solutions, so (N-M)/d) * x + (L/d) * y = (X-Y)/d of a group of solutions for x0 = (X-Y)/d * x. this is also a group of solutions (N-M) * x + L * y = (X-Y. */View Code

 

Related Article

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.