POJ 1061 Frog Dating (extended Euclid)

Source: Internet
Author: User
Tags greatest common divisor

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
The input includes only one line of 5 integer x,y,m,n,l, where X≠y < 2000000000,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

According to test instructions, two frogs jumped to the same point to be met, so there is (x+m*t)-(y+n*t) = P * l; (T is the number of hops, L is a frog jumps the number of laps and B frog's lap number is the latitude line total length.) The whole is the distance difference equal to the circumference of the latitude line integer multiple)

Convert: (n-m) * t + ll* p = computes;
Make a = n-m, B = l, c = gcd (A, b), d = x-y;
There is a * t + b * p = D;
The minimum integer solution for T is required.

The equation now conforms to the extended Euclidean form: A*X+B*Y=GCD (A, B)

The algorithm can also be used to solve the equation of a*x+b*y=c form. The method is to solve A*X+B*Y=GCD (A, b) first. Then the two ends are divided by gcd (A, b) multiplied by C to sort out the solution of the original equation. That is, A * (x*c/g) +b* (y*c/g) =c. The condition that the equation has solution is that C can be divisible by gcd (A, B).

Using the extended Euclidean to find one group of solutions T0, p0, and C = gcd (A, b);
There is a * t0 + b * p0 = c; (2)
Because C = gcd (A, b), so A * t/c is an integer, b * t/c is also an integer, so d/c also needs to be an integer, otherwise no solution.
(2) Both sides multiply (D/C) Get A * t0 * (D/C) + b * p0 * (d/c) = D;
So t0 * (D/C) is the smallest solution, but it can be negative.
Because A * (t0 * (d/c) + b*n) + b * (P0 * (d/c) –a*n) = D; (n is the natural number)
So the solution is (t0 * (d/c)% B + b)% B;

There is another question, how to use the extended Euclid to find out t0 and p0?
For non-negative integers a, B, gcd (A, b) that are not exactly 0, indicate the greatest common divisor of a, B. Then there is an integer x, y makes gcd (a, b) = a * x + b * y;
Set a > B
①, when B = 0 o'clock, gcd (A, b) = A, at which point x = 1, y = 0;
②, when a * b <> 0 o'clock,
Set A * x + b * y = gcd (A, b); (1)
b * x0 + (a% b) * y0 = GCD (b, a% B); (2)
By the simple Euclidean formula; GCD (A, b) = gcd (b, a% B);
(1), (2) A * x + b * y = b * x0 + (a% b) * y0
= b * x0 + (a–a/b * b) * y0
= A * y0 + (x0–a/b * y0) * b
So x = y0, y = x0–a/b * y0;
This leads to the extension of Euclid's recursive procedure:

void Extend_euclid (int a, int b) {    if (b = = 0)    {        x = 1;        y = 0;        Q = A;    }    else    {        Extend_euclid (b, a% b);        int temp = x;        x = y;        y = temp-a/b * y;    }}

#include <iostream>using namespace Std;long long T, p, c;void extend_euild (int a, int b) {    if (b = = 0)    {        T = 1;        p = 0;        c = A;    }    else    {        Extend_euild (b, a%b);        int temp = t;        t = P;        p = temp-a/b*p;    }} int main () {    int i, j, OK = 0, D, a, B;    Long long x, Y, M, N, l;    Freopen ("In.txt", "R", stdin);    Cin >> x >> y >> m >> n >> l;    if (n = = m)        OK = 1;    else    {        a = n-m;        d = x-y;        b = l;        Extend_euild (A, b);        if (d% c!=0)            OK = 1;    }    if (OK)        cout << "impossible" << Endl;    else    {        d = d/c;        Long Long v = d * t;        cout << (v%b+b)%b << Endl;    }    return 0;}


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.