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

Source: Internet
Author: User

Introduction and derivation of dating-number theory Extended Euclidean Algorithm 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
  
   #include
   
    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)

Related Article

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.