POJ 1061 Frog Dating (extended Euclid)

Source: Internet
Author: User
Tags greatest common divisor cmath

The date of the frog

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

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
#include <cstdio>#include<cmath>typedefLong LongLL; LL X, Y, M, N, L; ll GCD (ll A, ll b) { while(b) {LL R= a%b; A=b; b=R; }    returnA;}voidEXTEND_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; }}intMain () {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

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

Analysis: Set two frogs to jump S times and a to jump faster than B, then (x + S * m)-(y + S * n) = k * L (k = 0, 1, 2 ...). )。

Move items merged (n-m) * S + k * L = (x-y). Make a = n-m, B = L, c = x-y, i.e. A * S + b * L = C (1)

If the formula (1) has an integer solution, then two frogs can meet, otherwise not. So the problem is transformed to find the integer solution of the equation.

First, the D = gcd (A, b) is computed, and if D is not divisible by C, then the equation has no integer solution. Otherwise, divide both sides of the equation by D to get a ' * S + b ' * L = C ', at which point gcd (a ', B ') = 1.

Then the extended Euclidean algorithm is used to find a set of integer solution x0 for a ' * S + b ' * L = 1 ', y0, then (c ' * x0, c ' * y0) is a set of integer solutions for a ' * s + b ' * L = C ', a ' * s + b ' * L = C ' for all solutions (x = C ' * x0 + b ' * k, y = C ' * y0-a ' * k), and is also a * S + b * L = c all solutions.

#include <cstdio>#include<cmath>typedefLong LongLL; LL X, Y, M, N, L; ll GCD (ll A, ll b) { while(b) {LL R= a%b; A=b; b=R; }    returnA;}voidEXTEND_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; }}intMain () {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>typedefLong LongLL; using namespacestd;            LL X, Y, M, N, L; voidEXTEND_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); }      }            intMain () { 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 X from being negativecout << x <<Endl; }              Elsecout <<"Impossible"<<Endl; }          return 0; }            /*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 for a set of solutions, so ((N-M)/d)     * x + (l/d) * y = (XY)/d a set of solutions for x0 = (x-y)/d * x. This is also a set of solutions (n-m) * x+ L * y = (xy). */  
View Code

POJ 1061 Frog Dating (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.