POJ 1061 The date of the frog. The minimum number of steps T for solving (X+MT)%l= (y+nt)%l.

Source: Internet
Author: User
Tags gcd greatest common divisor uppercase letter

Because is congruence, so is (X+MT)%l-(y+nt)%l=0. can be written as (x-y+ (m-n) T)%l=0. The number is a multiple of L. Then I can do this x-y+ (m-n) T + Ls = 0. Can be, s can be negative, you can meet the conditions.

#include <cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>using namespacestd;#defineINF (0X3F3F3F3F)typedefLong Long intLL; #include<iostream>#include<sstream>#include<vector>#include<Set>#include<map>#include<queue>#include<string>ll gcd (ll n,ll m) {if(n%m==0)returnm; Else returnGCD (m,n%m);} ll EXGCD (ll A,ll mod,ll&x,ll &y) {    //solving a about mod's inverse element    if(mod==0) {x=1; y=0; returnA//retains basic functionality and returns greatest common divisor} LL G=EXGCD (mod,a%mod,x,y); LL T=x;//here according to Mod==0 return back,X=y;//x, Y is the newest value x2,y2, change it, so the assignment is to X1=y2y=t-(a/mod) *y;//y1=x2 (became T)-[a/mod]y2;    returnG//retains basic functionality and returns greatest common divisor}ll get_min_number (LL a,ll b,ll c,ll&x,ll &y) {    //get the minimum integer solution of A*x+b*y=cLL ABGCD = gcd (A, b);//system GCD    if(C%ABGCD! =0)return-1;//No solutiona/= abgcd; b/= ABGCD; C/=ABGCD;    LL Tx,ty; EXGCD (A,b,tx,ty); //first get the solution of A*x+b*y=1, note this time gcd (A, b) =1x = Tx*c; y = c*ty;//any solution is because you are asking for the solution of A*x+b*y=1, but what we need is the solution of a*x+b*y=c, so at the same time multiply C//then the solution of this equation is x0=tx*c+b*k,y0=ty*c-a*k. Here's A and b about Jane. K is { -1,-2,0,1,2}, etc.LL temp_x =x; X%= b;if(x<=0) x + = b;//Minimum SolutionLL k = (temp_x-x)/b; Y+ = k*A; return 1;//1 Delegates can}voidWork () {LL x,y,n,m,l; CIN>>x>>y>>m>>n>>m; LL a=m-N; LL b=L; LL C=y-x;    LL Tx,ty; LL T=Get_min_number (a,b,c,tx,ty); if(t==-1) {cout<<"Impossible"<<Endl; return ; }    if(b<0) b=-b;  while(tx<0) TX + =b; cout<<tx<<Endl; return ;}intMain () {#ifdef local freopen ("Data.txt","R", stdin);#endifWork (); return 0;}
View Code

★, solve the equation ax+by=c the smallest integer solution (a or b is a negative number, directly put in also no problem)

First of all, if you want to solve ax+by=c, use EXGCD can find Ax+by=1 x, Y. So we first make A and b about coprime (divided by gcd (A, A, a), we can find the Ax+by=1 x and y, but we want the solution of ax+by=c, so at the same time multiply C, where the uppercase letter is to represent about GCD (A, b) after the equation. Then the solution of this equation is x0=tx*c+b*k. Y0=ty*c-a*k. K is { -1,-2,0,1,2} and so on.

int get_min_number (int a,int b,int c,int &x,int &y)//Minimum integer solution to get A*x+b*y=c

{

int ABGCD = GCD (A, b);

if (C%ABGCD! = 0) return-1;//no solution

a/= abgcd; b/= ABGCD; C/= ABGCD;

int tx,ty;

EXGCD (A,b,tx,ty); First get the solution of A*x+b*y=1, note this time gcd (A, b) =1

x = Tx*c; y = c*ty; At the same time, C,c was a Jane.

int temp_x = x;

x%= B; if (x<=0) x + = b;//Minimum Solution

int k = (temp_x-x)/b;

Y + = k*a;

Return 1;//1 representative can

}

To get the smallest positive integer solution. Note that it is important to determine if there is a solution TLE .

int Bb=abs (B/GCD (A, b));

int Aa=abs (A/GCD (A, b));

while (x<0) x + = BB;

while (x*a+b*y!=c) y + = AA;

POJ 1061 The date of the frog. The minimum number of steps T for solving (X+MT)%l= (y+nt)%l.

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.