POJ-1061 frog appointment extension GCD

Source: Internet
Author: User
Frog appointment
Time limit:1000 ms Memory limit:10000 K
Total submissions:62388 Accepted:9696

Description

Two
Frogs get to know each other on the Internet. They have a good chat, so they feel it is 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, it
Before departure, I forgot a very important thing. I 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,
Always hit the other party. 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 Program To determine the two frogs
Whether or not the meeting can be met. When will the meeting be met.

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 line
Number axis. 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

This is an extension of GCD application, that is, the use of GCD's division of the moving phase to solve a binary one-time infinitus. Of course, it also uses the Back-to-generation idea. For specific principles, refer to Baidu encyclopedia's "Extended EuclideanAlgorithm"For an indefinite equation, there must be n groups of solutions,
This is to find a minimum integer solution that satisfies the number of steps (one of the variable X) of the equation. Be sure to use long Int.
CodeAs follows:
#include 
  
    long int gcd (long int X, long int y) {If (y = 0) {return X;} return gcd (Y, X % Y);} void exgcd (long int A, long int B, long Int & X, long Int & Y) {If (B = 0) {x = 1; y = 0; return;} exgcd (B, A % B, x, y); long int T = x; X = y; y = T-A/B * Y; return;} int main () {long int X, Y, m, n, l; while (scanf ("% LLD % LLD ", & X, & Y, & M, & N, & L )! = EOF) {long int A = N-M, B = L, c = x-y, p, q; long int d = gcd (A, B ); if (C % d) {puts ("impossible"); continue;} A/= D, B/= D, C/= D; exgcd (A, B, P, q); p * = C; long int T = P % B; while (T <0) {T + = B;} printf ("% LLD \ n ", t) ;}}
  

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.