Poj1061 frog appointment

Source: Internet
Author: User

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

Output The number of hops required for meeting. If it is never possible to meet each other, output an "impossible" line. This question needs to be solved using the same concept of number theory, when ax and B are in mod L, they are resolved only when gcd (A, L) can be divisible by B. Therefore, they cannot be divisible, directly output impossible can use the theorem under the same remainder condition and the Euclidean extension theorem to obtain the X that meets the condition. For this group of solutions, all things that match this X in mod L are constant values that match the condition. We can find a smallest positive integer in it as the result output, and then we can easily obtain the formula (m-N) in the question) * k = Q-P (mod L) Here, k is the X code as described above:
 1 #include <iostream> 2 #include <iostream> 3 #include <iomanip> 4 #include<string> 5 #include<cstring> 6 #define LL long long 7 using namespace std; 8  9 LL a,b,x,y,d;10 11 LL gcd(LL a,LL b)12 {13     if(b==0) return a;14     else return gcd(b,a%b);15 }16 void ex_gcd(LL a,LL b,LL &x,LL &y,LL &d)17 {18     LL t;19     if(b==0){20         d=a,x=1,y=0;21     }22     else{23         ex_gcd(b,a%b,x,y,d);24         t=x,x=y,y=t-a/b*y;25     }26 }27 int main()28 {29     LL p,q,m,n,L;30     cin>>p>>q>>m>>n>>L;31     a=m-n,b=q-p;32 33     if(a<0) a=-a,b=-b;34     LL Gcd=gcd(a,L);35 36     if(b%Gcd!=0) cout<<"Impossible"<<endl;37     else{38         LL temp;39         a=a/Gcd,temp=L/Gcd,b=b/Gcd;40         ex_gcd(a,temp,x,y,d);41         b=b*x;42 43         b%=L;44         if(b<0) cout<<b+L<<endl;45         else cout<<b<<endl;46     }47 48 49     return 0;50 }

 

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.