当前位置:网站首页>[4.6 detailed explanation of Chinese remainder theorem]
[4.6 detailed explanation of Chinese remainder theorem]
2022-07-27 00:35:00 【Romantic dog】
Better reading experience \color{red}{ Better reading experience } Better reading experience
Concept
If there is an integer m 1 , m 2 , m 3 … m n m_1,m_2,m_3\dots m_n m1,m2,m3…mn The two are mutual , Then for any integer a 1 , a 2 , a 3 … a n a_1,a_2,a_3\dots a_n a1,a2,a3…an, Univariate Linear Congruence Equations ( S ) (S) (S) Have a general solution
( S ) : { x ≡ a 1 ( m o d m 1 ) x ≡ a 2 ( m o d m 2 ) x ≡ a 3 ( m o d m 3 ) … x ≡ a n ( m o d m n ) (S): \begin{cases} x\equiv a_1(mod~m_1)\\ x\equiv a_2(mod~m_2)\\ x\equiv a_3(mod~m_3)\\ \dots\\ x\equiv a_n(mod~m_n)\\ \end{cases} (S):⎩⎨⎧x≡a1(mod m1)x≡a2(mod m2)x≡a3(mod m3)…x≡an(mod mn)
thought
about ( S ) (S) (S) The two formulas are merged
{ x ≡ a 1 ( m o d m 1 ) x ≡ a 2 ( m o d m 2 ) ⇒ { x = k 1 m 1 + a 1 x = k 2 m 2 + a 2 \begin{cases} x\equiv a_1(mod~m_1)\\ x\equiv a_2(mod~m_2)\\ \end{cases} \Rightarrow \begin{cases} x=k_1m_1+a_1\\ x=k_2m_2+a_2\\ \end{cases} { x≡a1(mod m1)x≡a2(mod m2)⇒{ x=k1m1+a1x=k2m2+a2
Combine the two formulas after equivalent conversion , Simplify to k 1 m 1 + k 2 ( − m 2 ) = a 2 − a 1 ① k_1m_1+k_2(-m_2)=a_2-a_1~① k1m1+k2(−m2)=a2−a1 ①
A set of solutions can be obtained from the extended Euclidean algorithm ( k 1 ′ , k 2 ′ ) ({k_1}',{k_2}') (k1′,k2′), bring : k 1 ′ m 1 + k 2 ′ ( − m 2 ) = g c d ( m 1 , − m 2 ) {k_1}'m_1+{k_2}'(-m_2)=gcd(m_1,-m_2) k1′m1+k2′(−m2)=gcd(m1,−m2)
if g c d ( m 1 , − m 2 ) gcd(m_1,-m_2) gcd(m1,−m2) Not divisible a 2 − a 1 a2-a1 a2−a1, There is no solution. , Otherwise, there is a general solution
set up g c d ( m 1 , − m 2 ) = d , y = ( a 2 − a 1 ) d gcd(m_1,-m_2)=d,y=\frac{(a_2-a_1)}{d} gcd(m1,−m2)=d,y=d(a2−a1)
From the inference of extended Euclidean algorithm : { k 1 = k 1 ′ y k 2 = k 2 ′ y \begin{cases}k_1={k_1}'y\\k_2={k_2}'y\end{cases} { k1=k1′yk2=k2′y
Introduce general solution { k 1 = k 1 + k m 2 d k 2 = k 2 + m 1 d \begin{cases}k_1={k_1}+k\frac{m_2}{d}\\k_2={k_2}+\frac{m_1}d\end{cases} { k1=k1+kdm2k2=k2+dm1, k ∈ Z k\in \Z k∈Z
Bring the general solution into ① ① ① have to ( k 1 + k m 2 d ) m 1 + ( k 2 + k m 1 d ) ( − m 2 ) = a 2 − a 1 ({k_1}+k\frac{m_2}{d})m_1+({k_2}+k\frac{m_1}{d})(-m_2)=a_2-a_1 (k1+kdm2)m1+(k2+kdm1)(−m2)=a2−a1
Simplify to k 1 m 1 + k 2 ( − m 2 ) = a 2 − a 1 k_1m_1+k_2(-m_2)=a_2-a_1 k1m1+k2(−m2)=a2−a1 and ① ① ① identical , Therefore, it is established that
Repeat the above steps , Continue to merge the remaining formulas , You can get the final general solution
Example Strange way to express integers
describe
Given 2n It's an integer a1,a2,…,an and m1,m2,…,mn, Find the smallest nonnegative integer x, Satisfy ∀i∈[1,n],x≡mi(mod ai).
Input format
The first 1 Row contains integer n.
The first 2…n+1 That's ok : Every time i+1 Line contains two integers ai and mi, The numbers are separated by spaces .
Output format
Output the minimum nonnegative integer x, If x non-existent , The output −1.
If there is x, Then the data guarantee x It must be 64 Bit integer range .
Data range
1≤ai≤231−1,
0≤mi<ai
1≤n≤25
sample input :
2
8 7
11 9
sample output :
31
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
void exgcd(LL a,LL b, LL &x,LL &y){
if(!b){
x=1,y=0;
return ;
}
else{
exgcd(b,a%b,x,y);
LL t=x;
x=y;
y=t-a/b*y;
}
}
LL gcd(LL a,LL b){
return b ? gcd(b,a%b) : a;
}
int main(){
int n;
cin>>n;
LL x=0,m1,a1; // The coefficients of the first equation The backup data
cin>>m1>>a1; // First enter the first equation
for(int i=0;i<n-1;i++){
// Merge the following n-1 An equation
LL m2,a2;
cin>>m2>>a2;
LL k1,k2;
LL d=gcd(m1,m2);
exgcd(m1,m2,k1,k2);
if((a2-a1)%d){
// There is no solution at this time
x=-1;
break;
}
k1*=(a2-a1)/d;// Special solution
k1=(k1%(m2/d)+m2/d)%(m2/d); // Jean texie k1 Get the minimum positive integer solution
x=k1*m1+a1;
LL m=abs(m1/d*m2);
a1=k1*m1+a1;
m1=m;
}
if(x!=-1) x=(a1%m1+m1)%m1 // When the loop ends , The value at this time should be modular with the least common multiple , To find the minimum positive integer solution
cout<<x<<endl;
return 0;
}
边栏推荐
猜你喜欢

C language to find prime numbers, leap years and minimum common multiples and maximum common divisors

Signal and system impulse response and step response

CSDN文章语法规则

2020-12-20 99 multiplication table

动态联编和静态联编、以及多态

Deployment of yolov5 on Jetson nano deepstream

12_ Decision tree

CCPD data set processing (target detection and text recognition)

CDs simulation of minimum dominating set based on MATLAB

CSDN article syntax rules
随机推荐
A simple prime number program. Beginners hope that older bosses can have a look
Matlab based medical imaging technology filtering backprojection simulation, including direct backprojection, S-L filtering, R-L filtering, LeWitt filtering
My first blog - confused junior
蒙着头配置deeplabcut 1
【4.3 欧拉函数详解】
Drawing warehouse Tsai
View where Anaconda created the environment
12_决策树(Decision tree)
【Codeforces Round #808 (Div 2.) A·B·C】
Deeplabcut uses 1
Openharmony quick start
Dynamic binding, static binding, and polymorphism
【AcWing第61场周赛】
C语言 关机小程序
Comparative simulation of LEACH protocol performance, including the number of dead nodes, data transmission, network energy consumption, the number of cluster heads and load balance
【4.10 博弈论详解】
Downloading and processing of sentinel-2
【2. Tmux 操作】
解析网页的完整回顾
[Qt]解决中文乱码问题