Introduction and derivation of Extended Euclidean Algorithm in dating-number theory for POJ-1061 frogs

Source: Internet
Author: User

Introduction and derivation of Extended Euclidean Algorithm in dating-number theory for POJ-1061 frogs

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
#include<stdio.h>#include<iostream>using namespace std;__int64 t,p;__int64 gcd(__int64 a,__int64 b){    if(b==0)        return a;    else        return gcd(b,a%b);}void extend_gcd(__int64 a,__int64 b){    if(b==0)    {        t=1;        p=0;    }    else    {        extend_gcd(b,a%b);         __int64 temp=t;        t=p;        p=temp-a/b*p;    }}int main(){    __int64 x,y,n,m,l;    __int64 a,b,c,a1,b1,c1,count;   scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&l);        a=n-m;        b=l;        c=x-y;       if(c%gcd(a,b)!=0||m==n)        {            printf("Impossible\n");           return 0;        }            count=gcd(a,b);            a=a/count;            b=b/count;            c=c/count;            extend_gcd(a,b);        t*=c;        p*=c;        t=(t%b+b)%b;            printf("%lld\n",t);    return 0;}

Extended Euclidean algorithm-solutions to indefinite equations, linear homogeneous equations:
To solve the uncertainty Equation ax + by = n, follow these steps:
 
(1) calculate gcd (a, B ). if gcd (a, B) cannot divide n, the equation has no integer solution. Otherwise, the two sides of the equation are divided by gcd (a, B ),
Obtain the new uncertainty equation a' x + B 'y = n'. In this case, gcd (A', B') = 1
 
(2) Obtain a group of integers in the uncertainty equation a' x + B 'y = 1 to solve x0 and y0, then n' x0, n 'y0 is a group of Integer Solutions for the equation a' x + B 'y = n.
 
(3) According to the & @ ^ % W # & theorem, All integers of the equation a' x + B 'y = n' are obtained:
X = n' x0 + B't
Y = n' y0-a' t
(T is an integer)
This is all integer solutions of the equation ax + by = n.
 
Use the Extended Euclidean algorithm to calculate gcd (a, B) and x0 and y0 that satisfy d = gcd (a, B) = ax0 + by0,
That is, a group of Integer Solutions satisfying a' x0 + B 'y0 = 1 are obtained. Therefore, you can:
X = n/d * x0 + B/d * t
Y = n/d * y0-a/d * t
(T is an integer)
*/

Euclidean template:

_ Int64 gcd (_ int64 a ,__ int64 B) {if (B = 0) return a; else return gcd (B, a % B, principle: Theorem: gcd (a, B) = gcd (B, a mod B) Proof: a can be expressed as a = kb + r, then r = a mod B Assumes that d is a common divisor of a and B, then d | a, d | B, and r = a-kb, therefore, d | r is (B, a mod B). Assume that d is the common divisor of (B, a mod B), then d | B, d | r, however, because a = kb + r, d is also the common divisor of (a, B). Therefore, the common divisor of (a, B) and (B, a mod B) are the same, the maximum common approx. The maximum common approx. a = 481,221, B = 481; (221) therefore, the maximum public approx. Is 13; void extend_gcd (_ int64 a ,__ int64 B) {if (B = 0) {t = 1; p = 0 ;} else {extend_gcd (B, a % B); _ int64 temp = t; t = p; p = temp-a/B * p ;}} Euclidean extension derivation:
Comparing this implementation with the Recursive Implementation of Gcd, we found that the following x and y values are much greater, which is the essence of extending the Euclidean algorithm.
You can think like this:
For a' = B, B '= a % B, we obtain x and y to make a' x + B' y = Gcd (A', B ')
Because B '= a % B = a-a/B * B (Note: Here/is the division in the programming language)
Then we can get:
A' x + B 'y = Gcd (A', B') =>
Bx + (a-a/B * B) y = Gcd (A', B ') = Gcd (a, B) ==> ay + B (x-a/B * y) = Gcd (a, B)
Therefore, for a and B, their corresponding p and q are respectively y and (x-a/B * y)


Better understanding of Extended Euclidean Algorithms

The euclidean algorithm, also known as the moving phase division, is used to calculate the maximum approximate number of two integers a and B. The computation principle depends on the following theorem:
Theorem: gcd (a, B) = gcd (B,
Mod B)
Proof: a can be expressed as a = kb +
R, r = a mod B
Assume that d is a common divisor of a and B.
D |,
D | B, and r = a-kb, so d | r
Therefore, d is (B,
Mod B)
Assume that d is (B,
Mod B), then
D | B, d
| R, but a = kb + r
Therefore, d is also the common divisor of (a, B ).
So (a, B) and (B, a mod
B) the maximum number of common appointments must be the same.
The euclidean algorithm is based on this principle. Its algorithm is described in the C ++ language as follows:
Int
Gcd (int a, int B)
{
If (B =
0)
Return;
Return
Gcd (B, a % B );
}
Of course, you can also write it as an iteration:
Int
Gcd (int a, int B)
{
While (B! =
0)
{
Int r = B;
B = a % B;
A =
R;
}
Return
A;
}
In essence, the above principle is used.
Supplement:
The Extended Euclidean algorithm is used,
B solves a group of x and y so that a * x + B * y = Gcd (a, B) (the solution must exist, according to the related Theorem in number theory ). Extended Euclidean is often used in solving model Linear Equations and equations. Below is an example
Implementation with C ++:
Int
ExGcd (int a, int B, int & x, int
& Y)
{
If (B =
0)
{
X = 1;
Y = 0;
Return;
}
Int r =
ExGcd (B, a % B, x, y );
Int t =
X;
X =
Y;
Y = t-
/B * y;
Return
R;
}
Comparing this implementation with the Recursive Implementation of Gcd, we found that the following x and y values are much greater, which is the essence of extending the Euclidean algorithm.
You can think like this:
For a' = B,
B '= a % B, we obtain x and y to make a' x + B' y = Gcd (A', B ')
Because B '=
% B = a-a/B * B (Note: Here/is the division in the programming language)
Then we can get:
A' x + B 'y
= Gcd (A', B ') ==>
Bx + (a-a/B * B) y = Gcd (A', B ') = Gcd (a, B)
==>
Ay + B (x-a/B * y) = Gcd (a, B)
Therefore, for a and B, their corresponding p and q are
Y and (x-a/B * y ).
I have read a lot of questions about the solution of the Indefinite Equation on the Internet, but I have not fully explained it. I have only talked about some of them. After reading a lot of questions, I can really figure out the whole process of the solution. The steps are as follows:
Calculate a * x
+ Integer solution of B * y = n.
1. calculate Gcd (a, B) first. If n cannot be divisible by Gcd (a, B), the equation has no integer solution. Otherwise, the two sides of the equation are divided by Gcd (, b) to obtain the new INDEFINITE EQUATION'
* X + B '* y = n'. In this case, Gcd (A', B') = 1;
2. Use the Euclidean algorithm mentioned above to find a group of integers in the equation a' * x + B '* y = 1 to solve x0 and y0, then n' * x0, n '*
Y0 is a group of Integer Solutions for equation a' * x + B '* y = n'... the rest of the full text>

For Extended Euclidean Algorithms

-N * n' % r = 1 is invalid

N'if it is calculated as a negative number, the symbol cannot be ignored.

N' =-n ^ (-1) % r = (r-n) ^ (-1) % r can be converted

Where n ^ (-1) is the reciprocal of n? Is the reciprocal of Number Theory

N ^ (-1) * n modulus r Division 1

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.