Acm:poj 1061 Frog Dating-Number theory topic-expand Euclid

Source: Internet
Author: User

POJ 1061The date of the frogTime limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld &%llu< /c8>

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

/* /English: Test instructions, two frogs jump in the same direction, to make two frogs meet, it is necessary to ensure that at the same time, two frogs jump to the same place. x+nx-(Y+MX) =yl; X is the number of hops, and Y is the number of laps. ==>x-y-x (n-m) =ly; a=x-y,b=n-m; The original is changed  to a-bx=ly ==>  ly+bx=a  ==>  bx=a (mod L); then you can call expand Euclidean algorithm  EXGCD (b,l,&x,&y); EXGCD the solution of the equation, but also to do one step processing. ans=xy/d*x%l/d+l/d;  Ans is the final answer to this question. Ability a bit of slag, direct set of template. "TAT" number theory abuse me like a vegetable worm, even vegetable chicken can not forget ... AC Template ... /*/
#include "map" #include "Cmath" #include "string" #include "Cstdio" #include "vector" #include "CString" #include "iostream "#include" algorithm "using namespace std;typedef long LL; ll EXGCD (ll a,ll b,ll &x,ll &y) {//Direct reference to save X and y in the entire process (N-M) x+ly=x-y; if (!b) {X=1;y=0;return A;} LL D=EXGCD (b,a%b,x,y); LL t=x;x=y;y= (t-a/b*y); return D;} int main () {ll x,y,m,n,l,x,y;while (~scanf ("%lld%lld%lld%lld%lld", &x,&y,&m,&n,&l)) {ll xy=x-y; LL D=EXGCD (n-m,l,x,y); if (xy%d!=0| | M==n) {printf ("impossible\n"); continue;} LL r=l/d; LL ans=xy/d*x%r+r;printf ("%lld\n", ans%r);} return 0;}

  

Acm:poj 1061 Frog Dating-Number theory topic-expand Euclid

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.