Poj 1061 frog appointment (Extended Euclidean)

Source: Internet
Author: User

Question link: http://poj.org/problem? Id = 1061


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


PS:

According to the question: x + M * t-(Y + N * t) = K * l; ----> T * (m-N) -K * l = Y-x ---> K * L + T * (N-m) = x-y;

Eventually simplified: K * L + T * (N-m) = x-y;


The Code is as follows:

# Include <cstdio> # include <cstring> # include <cmath> typedef _ int64 ll; ll exgcd (ll a, LL B, ll & X, ll & Y) {If (B = 0) {x = 1; y = 0; return a;} else {ll r = exgcd (B, A % B, x, y ); ll T = x; X = y; y = T-A/B * Y; return r;} ll CAL (ll a, LL B, ll c) {ll X, y; ll TT = exgcd (a, B, x, y); If (C % TT) // No integer solution {return-1;} x * = C/TT; b/= tt; ll ans = (X % B + B) % B; // return ans;} int main () {ll A, B, n, m, L, X, Y; Wh Ile (~ Scanf ("% i64d % i64d % i64d % i64d % i64d", & X, & Y, & M, & N, & L) {// T (n-m) + K * l = x-y; ll ans = CAL (n-M, L, x-y); If (ANS =-1) {printf ("impossible \ n"); continue;} printf ("% i64d \ n", ANS);} return 0 ;}



Poj 1061 frog appointment (Extended Euclidean)

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.