Summary of the extension of the Chinese remainder theorem (excrt)

Source: Internet
Author: User
Tags greatest common divisor modulus mul

Clear a misunderstanding

Although the Chinese remainder theorem and the expansion of the Chinese remainder theorem is only two words, but they two of the solution difference of 108,000, so will not CRT does not matter

Use

Like $$\begin{cases}x \equiv b_{1}\pmod{a_{1}} \\x \equiv b_{2}\pmod{a_{2}} \\...\\x \equiv b_{n}\pmod{a_{n}} \ \ \end{ Solution of linear congruence equations for cases}$$

Specific process

Suppose now we have only two congruence equations $ $x \equiv b_{1}\pmod{a_{1}}$$ $ $x \equiv b_{2}\pmod{a_{2}}$$

Rewrite them to $ $x =a_{1}k_{1}+b_{1}①$$ $ $x =a_{2}k_{2}+b_{2}$$

United $ $a _{1}k_{1}+b_{1}=a_{2}k_{2}+b_{2}$$

So $ $a _{1}k_{1}=b_{2}-b_{1}+a_{2}k_{2}$$

Recorded $ $c =gcd (a_{1},a_{2}) $$ $ $d _{1}=\frac{a_{1}}{c}$$ $ $d _{2}=\frac{a_{2}}{c}$$

Divide both sides with $c$ $ $d _{1}k_{1}=\frac{b_{2}-b_{1}}{c}+d_{2}k_{2}$$

That is, $ $d _{1}k_{1} \equiv \frac{b_{2}-b_{1}}{c}\%d_{2}\pmod{d_{2}}$$

Obviously, the conditions for the equations to be solved are $c|. (B_{2}-b_{1}) $

Why should I take the $\%d_{2}$ at this time? Because the modulus and the modulus are not coprime after the module is taken.

Remember $d_{1}$ in the $d_{2}$ meaning of the inverse of $p$, both sides of the same multiply $p$ $ $k _{1} \equiv \frac{p (B_{2}-b_{1})}{c}\%d_{2} \pmod{d_{2}}$$

That is, $ $k _{1} = \frac{p (B_{2}-b_{1})}{c}\%d_{2}+d_{2}*y$$

Bring in ① $ $x =\frac{p (b_{2}-b_{1})}{c}\%d_{2}*a_{1}+d_{2}a_{1}y+b_{1}$$

That is, $ $x \equiv \frac{p (b_{2}-b_{1})}{c}\%d_{2}*a_{1}+b_{1}\pmod{a_{1}d_{2}}$$

Note that the $a_{1}$ in the upper style must not be $\%d_{2}$ in front of the

So we've got a new congruence equation. This equation can also be written as $ $x \equiv b \pmod a$$ where $ $b = (INV (\frac{a_{1}}{(A_{1},a_{2})},\frac{a_{2}}{(A_{1},a_{2})}) *\frac{b_{2}-b_{ 1}}{(A_{1},a_{2})}\%\frac{a_{2}}{(a_{1},a_{2})}*a_{1}+b_{1}) $$ $ $a =\frac{a_{1}a_{2}}{(a_{1},a_{2})}$$

where $INV (M,n) $ represents $m$ in modulo $n$ meaning, $ (m,n) $ represents $m$ and $n$ greatest common divisor

The newly-obtained equation is then linked to the subsequent equation, and the solution is iterated until only one equation is left.

Accuracy issues

There is no doubt that it is easy to explode $long long$ when solving equations, what should we do?

__int128 (Turtle) (fast) multiply (multiply) is a good idea.

What is fast multiplication?

Is something like a fast power, using binary to find the result of two number multiplication on another number in log time and not explode long long

It is important to note, however, that a negative number is not allowed to appear in the fast multiply, especially the split multiplier, which can be looped.

1 intMulintXintKintMoD) {2ll ans=0;3      while(k) {4         if(k&1) (ans+=x)%=MoD;5k>>=1;6(x<<=1)%=MoD;7     }8     returnans;9}
Fast MultiplyTopic

Rokua "template" Extending the Chinese remainder theorem (excrt)

1#include <bits/stdc++.h>2 using namespacestd;3 #defineINF 0x7fffffff4 #defineME 0x7f5 #defineFO (s) freopen (S ".", "R", stdin); Freopen (S ". Out", "w", stdout)6 #defineFui (I,A,B,C) for (int i= (a); i<= (b); i+= (c))7 #defineFDI (I,A,B,C) for (int i= (a); i>= (b); i-= (c))8 #defineFel (i,a) for (register int i=hd[a];i;i=dg[i].nxt)9 #definell Long LongTen #defineint long Long One #defineMEM (A, B) memset (A,b,sizeof (a)) A #defineMAXN 100010 - ll N; - ll a[maxn],b[maxn],a0,b0; thetemplate<classT> -Inline T read (T &N) { -n=0;intt=1;Doublex=Ten;Charch; -      for(Ch=getchar ();! IsDigit (CH) &&ch!='-'; Ch=getchar ());(ch=='-')? t=-1: n=ch-'0'; +      for(Ch=getchar (); isdigit (ch); Ch=getchar ()) n=n*Ten+ch-'0'; -     if(ch=='.') for(Ch=getchar (); isdigit (ch); Ch=getchar ()) n+= (ch-'0')/x,x*=Ten; +     return(n*=t); A } attemplate<classT> - T Write (t n) { -     if(n<0) Putchar ('-'), n=-N; -     if(n>=Ten) Write (n/Ten);p Utchar (n%Ten+'0');returnN; - } -template<classT> inT Writeln (t N) {write (n);p Utchar ('\ n');returnN;} - intEXGCD (intAintBint&x,int&y) { to     if(!B)returnA + ((x=1) * (y=0)); +     intXX,YX,Z;Z=EXGCD (b,a%B,xx,yx); -X=yx,y=xx-a/b*yx;returnZ; the } * intgcdintAintb) {if(!B)returnAreturnGCD (b,a%b);} $ intMulintXintKintMoD) {Panax Notoginsengll ans=0; -      while(k) { the         if(k&1) (ans+=x)%=MoD; +k>>=1; A(x<<=1)%=MoD; the     } +     returnans; - } $ signed Main () { $ read (n); -Fui (I,1N1) read (A[i]), read (B[i]); -a0=a[1],b0=b[1]; theFui (I,2N1){ -         intC=GCD (A0,a[i]), d1=a0/c,d2=a[i]/c,p,x,y;Wuyi         intA1=a0,a2=a[i],b1=b0,b2=B[i]; the         if(((B2-B1)/c*c) ^ (B2-B1))return 0*writeln (-1); -EXGCD (d1,d2,p,y);((p%=d2) +=d2)%=D2; WuX=mul (P, (((B2-B1)/C)%d2+d2)%d2,d2) *a1+B1; -Y=a1*d2;b0=x,a0=y; ((b0%=a0) +=a0)%=A0; About     } $     intX,Y;EXGCD (1, a0,x,y); -((x%=a0) +=a0)%=A0; -x=Mul (x,b0,a0); - writeln (x); A     return 0; +}
AC Code

As if there were NOI2018 Dragon Slayer Warriors? 23333 just wait for me to get a drop of it.

Summary of the extension of the Chinese remainder theorem (excrt)

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.