POJ 1061 Frog Dating (extended Euclid)

Source: Internet
Author: User

Main topic:

That is, give you two starting x and Y, then x increments m,y each time n, as well as length L, to find the smallest number of changes T, there is (x+m*t)-(y+n*t) ==p*l.

Problem Solving Ideas:

Naked naked expansion of Euclid.

Analysis: Assume that after jumping the T-time, the Frog 1 coordinates is x+m*t, Frog 2 coordinates of Y+N*T. They can meet in the case of (x+m*t)-(y+n*t) ==p*l, where P is an integer, deform a bit

Get (N-M) *t+p*l==x-y we set a= (n-m), b=l,c=x-y,t=x,p=y. So we get ax+by==c. Excited ah, this is not the same formula above.

Directly apply the extended Euclidean function to get a set of solutions for X, Y. Since the question is how many times to jump at least, then only X is the information we need. So, do you think X is the smallest?

The answer is probably not!  So how do we get the smallest solution? We consider the formula of all the solutions of x: X=x0+b/d*t. X0 is what we just asked for, obviously there is a monotone function on the right, and when T is a number opposite to x positive or negative, the smallest x can be obtained. So that the positive and negative properties of X are positive, then X=X0-B/D*T1 (T1==-T). Make x==0, then t=x0*d/b, the smallest x equals x0 minus t*b/d. Here the X may be negative, if it is negative, we add a b/d to it is the answer!

Code:

1# include<cstdio>2# include<iostream>3# include<fstream>4# include<algorithm>5# include<functional>6# include<cstring>7# include<string>8# include<cstdlib>9# include<iomanip>Ten# include<numeric> One# include<cctype> A# include<cmath> -# include<ctime> -# include<queue> the# include<stack> -# include<list> -# include<Set> -# include<map> +  - using namespacestd; +  A Const DoublePi=4.0*atan (1.0); at  -typedefLong LongLL; -typedef unsignedLong LongULL; -  -# define INF999999999 -  in LL x,y,a,b,c,d; - LL n,m,x,y,l; to  + ll EXT_GCD (ll A,ll b) - { the LL t,d; *     if(b==0 ) $     {Panax Notoginsengx =1; -y =0; the         returnA; +     } AD = EXT_GCD (b,a%b); thet =x; +x =y; -y = t (A/b) *y; $     returnD; $ } -  -  the intMainvoid) - {Wuyi      while(cin>>x>>y>>m>>n>>L) the     { -A = nm; Wub =L; -c = XY; AboutD =EXT_GCD (A, b); $         if(c%d!=0 ) -         { -printf"impossible\n"); -             Continue; A  +         } thex = x* (c/d); -y = y* (c/d); $  theLL k = x*d/b; theK = x-k*b/D; the         if(k<0 ) the         { -k+=b/D; in         } thecout<<k<<Endl; the     } About  the  the     return 0; the}

POJ 1061 Frog Dating (extended 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.