POJ1061 (linear with congruence)

Source: Internet
Author: User

The date of the frog
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 105587 Accepted: 20789

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
#include <iostream>using namespacestd;typedef __int64 LL;//int front Double ' _ 'll EXTGCD (ll a,ll b,ll &x,ll &y) {LL D=A; if(b!=0) {D=EXTGCD (b,a%b,y,x); Y-= (a/b*x); }    Else{x=1; y=0; }    returnD;} ll GCD (ll A,ll b) {if(b==0)returnA; Else    returnGCD (b,a%b);} LL s1,s2,v1,v2,m;intMain () { while(cin>>s1>>s2>>v1>>v2>>m) {//the two meet conditions s1+v1*t=s2+v2*t-k*m = (v1-v2) *t+m*k=s2-s1//linear congruence Equation Ax+by=c (a:v1-v2,x:t,b:m,k:y,c:s1-s1)LL a=v1-v2; if(a<0) a+=m; LL b=m; LL C=s2-S1; if(c<0) c+=m; LL Div=gcd (A, b); if(c%div!=0)//the necessary and sufficient condition for the Ax+by=c equation is c|gcd (A, b).{cout<<"Impossible"<<Endl; Continue; } A/=div;//reduce each factor to Div timesB/=div;//ax+by=c = a ' x+b ' y=c 'C/=Div; LL x=0, y=0; EXTGCD (a,b,x,y);//solving linear congruence equation Ax+by=1x= (x*c)%b;//The expansion of Euclid is the X in Ax+by=1, and the result needs to expand the X by C times         while(x<0) x+=b; cout<<x<<Endl; }    return 0;}

POJ1061 (linear with congruence)

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.