EXTEND_GCD solving indefinite equation/membrane linear equation/multiplication Inverse element

Source: Internet
Author: User
Tags gcd greatest common divisor

Shaped like A*x+b*y=c

For indefinite equations, a,b>0 actually doesn't matter, because GCD (A, B) =gcd (|a|,|b|) GCD to greatest common divisor

Known by the theorem of number theory, when c%gcd==0, indefinite equations have solutions, now we seek this solution.

GCD=GCD (A, b); A*B=GCD*LCM; LCM for Least common multiple

A ' =a/gcd;b ' =b/gcd;c ' =c/gcd; A ' currently with B ' coprime

Make C ' =1=a ' *x+b ' *y;

A set of special solutions is obtained by using EXTEND_GCD. (X0,Y0)

(X0*c '. Y0*c ') a set of special solutions for a ' *x+b ' *y=c '

The solution of the equation is to find out its general solution; X=x0*c ' +b ' *t; Y=y0*c '-a ' *t; solve the problem with the general solution

It took a whole night to tangle up an obvious question today. Sure enough, lookers.

I want to laugh now. haha haha haha

uva,10413

The title means: T a case,n Savage, c,p,l respectively represents the initial cave, the next day to move the number of caves, survival days.

Q: The smallest number of caves, which makes the N savages not met in their respective lifetimes.

Data: 1*10^6 can be accepted, so this problem with the initial value of the savage in the original maximum number of caves, enumeration can be over.

Source:

1#include <iostream>2#include <algorithm>3#include <cstdio>4 5 using namespacestd;6 7 intX,y,gcd,n;8 intc[ -],p[ -],l[ -];9 Ten voidEXTEND_GCD (intAintb) One { A     if(b==0) -     { -x=1; they=0; -Gcd=A; -         return; -     } +EXTEND_GCD (b,a%b); -     inttmp=x; +x=y; Ay=tmp-a/b*y; at     return; - } -  - BOOLSolveintIintJintm) - { -     intc=c[i]-C[j]; in     intb=m; -     inta=p[j]-P[i]; to EXTEND_GCD (A, b); +     if(C%GCD)return false; -x*=c/gcd; the     if(gcd<0) gcd=-gcd; * //because of the X=x+b/gcd*k, X contains a number of algebraic and differential b/gcd $ //so take the X to the B/GCD and pay attention to the minimum positive value of the negative number x.Panax Notoginsengx= (x (B/GCD) +b/gcd)% (b/gcd); -     if(X<=l[i] && x<=L[j]) the         return true; +     return false; A } the BOOLTestintm) + { -      for(intI=0; i<n-1; i++) $          for(intj=i+1; j<n;j++) $         { -             if(Solve (I,J,M))return false; -         } the     return true; - }Wuyi  the  - intMain () Wu { -     intT; Aboutscanf"%d",&t); $      while(t--) -     { -scanf"%d",&n); -         intlb=0; A          for(intI=0; i<n;i++) +         { thescanf" %d%d%d", c+i,p+i,l+i); -lb=Max (lb,c[i]); $c[i]--; the         } the          while(lb<=1000000) the         { the             if(Test (LB)) Break; -lb++; in         } theprintf"%d\n", LB); the     } About     return 0; the } the //provide test data the Sample Input + 2 - 3 the 1 3 4Bayi 2 7 3 the 3 2 1 the 5 - 1 2  - - 4 4 6 the 8 5 9 the  One 8  - the  - 9 Ten the Sample Output - 6 the  -

EXTEND_GCD solving indefinite equation/membrane linear equation/multiplication Inverse element

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.