Explanation of extended Chinese Remainder Theorem and explanation of Chinese Theorem
Preface
Before reading this article, we recommend that you first learn the Chinese Remainder Theorem. In fact, it doesn't matter if you don't learn it. After all, there is no relationship between the two.
Extended CRT
We know that the Chinese Remainder Theorem is used to solve the homogeneous equations.
$ \ Begin {cases} x \ equiv c _ {1} \ left (mod \ m _ {1} \ right) \ x \ equiv c _ {2} \ left (mod \ m _ {2} \ right) \\\ ldots \ x \ equiv c_r \ left (mod \ m_r \ right) \ end {cases} $
However, one very unpleasant thing is that it requires $ m_1, m_2 \ ldots, and m_r $ mutual elements.
What if the subject of a cancer client is partial to the division of elements?
There are also solutions
It's about hanging up the subject and using the extended Chinese surplus theorem.
Extended Chinese surplus theorem has no half-Dime relationship with Chinese surplus theorem. One is Extended Euclidean, and the other is constructed.
First of all, let's start with a simple process. Consider the situation where the same equations only have two formulas.
$ X \ equiv c _ {1} \ left (mod \ m _ {1} \ right) \ x \ equiv c _ {2} \ left (mod \ m _ {1} \ right) $
Deformation of two sub-statements
$ X = c _ {1} + m _ {1} k _ {1} \ x = c _ {2} + m _ {2} k _ {2} $
Simultaneous
$ C _ {1} + m _ {1} k _ {1} = c _ {2} + m _ {2} k _ {2} $
Item
$ M _ {1} k _ {1} = c _ {2}-c _ {1} + m _ {2} k _ {2} $
We use $ (a, B) $ to represent the maximum common approx. of $ a and B $.
Note that the conditions for the solution of this equation are:
$ \ Left (m _ {1}, m _ {2} \ right) | \ left (c _ {2}-c _ {1} \ right) $, because $ \ dfrac {\ left (c _ {2}-c _ {1} \ right) }{\ left (m _ {2 }, m _ {1} \ right)} $. If not divisible, a decimal number is displayed.
For the above equation, the two sides have the same Division $ (m_1, m_2) $
$ \ Dfrac {m _ {1} k _ {1 }}{\ left (m _ {1}, m _ {2} \ right )} =\ dfrac {c _ {2}-c _ {1 }}{\ left (m _ {1}, m _ {2} \ right )} + \ dfrac {m _ {2} k _ {2 }}{\ left (m _ {1}, m _ {2} \ right) }$ $
$ \ Dfrac {m _ {1 }}{\ left (m _ {1}, m _ {2} \ right )} k _ {1 }=\ dfrac {c _ {2}-c _ {1 }}{\ left (m _ {1}, m _ {2} \ right )} + \ dfrac {m _ {2 }{\ left (m _ {1}, m _ {2} \ right)} k _ {2} $
Convert
$ \ Dfrac {m _ {1 }}{\ left (m _ {1}, m _ {2} \ right )} k _ {1} \ equiv \ dfrac {c _ {2}-c _ {1 }}{\ left (m _ {1 }, m _ {2} \ right)} (mod \ dfrac {m _ {2 }}{\ left (m _ {1 }, m _ {2} \ right)} $
At this point, $ k_2 $ has been deleted successfully.
Except $ \ dfrac {m _ {1 }{\ left (m _ {1}, m _ {2} \ right)} $
$ K_1 \ equiv inv ({m_1 \ over (m_1, m_2)}, {m_2 \ over (m_1, m_2)}) * {(c_2-c_1) \ over (m_1, m_2)} \ pmod {m_2 \ over (m_1, m_2) }$ $
$ Inv (a, B) $ indicates the reverse element of $ a $ in the model $ B $.
$ K_1 = inv ({m_1 \ over (m_1, m_2) },{ m_2 \ over (m_1, m_2)}) * {(c_2-c_1) \ over (m_1, m_2, m_2)} + {m_2 \ over (m_1, m_2)} * y $
What should we do next? This formula has been simplified ..
Don't forget. We have two orders at the beginning. Let's move $ k_1 $ back!
$ X = inv ({m_1 \ over (m_1, m_2) },{ m_2 \ over (m_1, m_2)}) * {(c_2-c_1) \ over (m_1, m_2, m_2)} * m_1 + y {m_1m_2 \ over (m_1, m_2)} + c_1 $
$ X \ equiv inv ({m_1 \ over (m_1, m_2) },{ m_2 \ over (m_1, m_2)}) * {(c_2-c_1) \ over (m_1, m_2)} * m_1 + c_1 \ pmod {m_1m_2 \ over (m_1, m_2) }$ $
Now, we know the elements in the entire formula.
Specifically, this formula can be viewed as $ x \ equiv c \ pmod m $
$ C = (inv ({m_1 \ over (m_1, m_2)}, {m_2 \ over (m_1, m_2)}) * {(c_2-c_1) \ over (m_1, m_2, m_2)}) \ % {m_2 \ over (m_1, m_2)} * m_1 + c_1 $
$ M = {m_1m_2 \ over (m_1, m_2)} $
Promotion
Each time we merge the two same-remainder formulas, a new same-remainder formula is obtained after the solution. Then, we can combine the new convolutional formula with other convolutionals to finally find solutions that meet the conditions.
Code
Question Link
#include<iostream>#include<cstdio>#define LL long long using namespace std;const LL MAXN=1e6+10;LL K,C[MAXN],M[MAXN],x,y;LL gcd(LL a,LL b){ return b==0?a:gcd(b,a%b);}LL exgcd(LL a,LL b,LL &x,LL &y){ if(b==0){x=1,y=0;return a;} LL r=exgcd(b,a%b,x,y),tmp; tmp=x;x=y;y=tmp-(a/b)*y; return r;}LL inv(LL a,LL b){ LL r=exgcd(a,b,x,y); while(x<0) x+=b; return x;}int main(){ #ifdef WIN32 freopen("a.in","r",stdin); #else #endif while(~scanf("%lld",&K)) { for(LL i=1;i<=K;i++) scanf("%lld%lld",&M[i],&C[i]); bool flag=1; for(LL i=2;i<=K;i++) { LL M1=M[i-1],M2=M[i],C2=C[i],C1=C[i-1],T=gcd(M1,M2); if((C2-C1)%T!=0) {flag=0;break;} M[i]=(M1*M2)/T; C[i]= ( inv( M1/T , M2/T ) * (C2-C1)/T ) % (M2/T) * M1 + C1; C[i]=(C[i]%M[i]+M[i])%M[i]; } printf("%lld\n",flag?C[K]:-1); } return 0;}
Add a bare question
Http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 1573
Question