Poj1061 frog appointment (Extended Euclidean)

Source: Internet
Author: User
Question link: Ah, haha, click me
This question is the problem of extending Euclidean... Ah, the mathematics is too weak. It took only half a day to understand it ....
If you want to meet each other, (n-m) * t + p * c = x-y is true, then a * x + B * Y = C is obtained for replacement, then it is converted to the one mentioned above, so we use the Extended Euclidean Algorithm to obtain a group of solutions. Then the general formula for the solution is X = x0 + K * B/gcd (, b), then the right sub-statement is equal to 0 and can be .. In addition, C % gcd (A, B) is not equal to 0, so there is no integer solution... Then this problem is solved .... Question: frog's appointment
Time limit:1000 ms   Memory limit:10000 K
Total submissions:90518   Accepted:16492

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

Source

Zhejiang
Code:
# Include <cstdio> # include <iostream> # include <algorithm> # include <cstring> using namespace STD ;__ int64 X, Y, m, n, l, D,, b, c, temp ;__ int64 gcd (_ int64 A ,__ int64 B) {_ int64 T, D; If (B = 0) {x = 1; y = 0; return a;} d = gcd (B, A % B); t = x; X = y; y = T-(A/B) * Y; return D;} int main () {_ int64 X1, K, ans; while (~ Scanf ("% i64d % i64d % i64d % i64d % i64d", & X, & Y, & M, & N, & L) {A = N-m; B = L; C = x-y; D = gcd (a, B); If (C % d! = 0) {printf ("impossible \ n"); continue;} X = x * (C/D); // X is a group of solutions of the original uncertainty equation .. K =-x * D/B; ans = x + K * B/d; If (ANS <0) ans = ans + B/d; printf ("% i64d \ n", ANS);} return 0 ;}



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.