Poj 1061-frog appointment (Extended Euclidean)

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

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
Extends the application of Euclidean.
Analysis: Set frog a to catch up with frog B after s steps. The following formula is met:
(X + S * m)-(Y + S * n) = K * l (k = 0, 1, 2...) Deformation:
(N-m) * s + K * l = x-y; make a = N-m; B = L; C = x-y;
A * S + B * k = C; (quadratic equation) original equation ①
As long as the above formula has an integer solution, the two frogs can meet each other; otherwise, the two frogs cannot; if there is a solution, we need to find min_s;
So that r = gcd (A, B) can be obtained from the extended gcd ax + by = r; solution, if a/= r, B/= R; then, we can find a group of solutions (x0, y0) with a'x + B 'y = 1, and multiply the two sides by C at the same time to obtain a 'cx + B' Cy = C; (that is, the original equation ①) So x0 * C is a solution of the original equation, so that x1 = x0 * C, then (x1% B + B) % B is the non-negative partial solution of a group of X of the original equation. (Adding B to prevent X1 from being negative)
#include <iostream>#include <cstring>#include <cstdio>#include <cctype>#include <cstdlib>#include <algorithm>#include <set>#include <vector>#include <string>#include <cmath>#include <map>#include <queue>using namespace std;#define LL long longLL gcd(LL a,LL b){    if(b==0)return a;    return gcd(b,a%b);}void exgcd(LL a,LL b,LL &x,LL &y){    if(b==0)    {        x=1;        y=0;        return ;    }    exgcd(b,a%b,y,x);    y-=a/b*x;}int main(){    LL x,y,m,n,L,s,k;    cin>>x>>y>>m>>n>>L;    LL a=n-m;    LL b=L;    LL c=x-y;    LL r=gcd(a,b);    if(c%r)    {        puts("Impossible");        return 0;    }    a/=r;b/=r;c/=r;    exgcd(a,b,s,k);    if(b<0)b=-b;    s*=c;    cout<<(s%b+b)%b<<endl;    return 0;}


 

Poj 1061-frog appointment (Extended Euclidean)

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.