当前位置:网站首页>[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;
}
边栏推荐
- 解析网页的完整回顾
- 9_ Logistic regression
- 类与对象笔记一
- Resolve Microsoft 365 and Visio conflicts
- SSRF (server side request forgery) -- Principle & bypass & Defense
- 八皇后 N皇后
- Draw impact function
- Convolutional neural network -- lenet (pytorch Implementation)
- 寻找真凶
- Matlab simulation of image reconstruction using filtered back projection method
猜你喜欢
![[Qt]属性](/img/ca/5f9d8f33e38b0ac5cbb0768a7b3ffd.png)
[Qt]属性

postman的使用
![[Qt]容器类、迭代器、foreach关键字](/img/88/d9d5be096009b4e5baa0966e6f292c.jpg)
[Qt]容器类、迭代器、foreach关键字

2020-12-20 99 multiplication table

7_主成分分析法(Principal Component Analysis)

Openharmony quick start

2022-07-17:1, 2, 3... N-1, N, n+1, n+2... In this sequence, only one number has repetition (n). This sequence is unordered. Find the repeated number n. This sequence is ordered. Find the repeated numb

V-viewer use

信号与系统冲激响应与阶跃响应

裁剪tif影像
随机推荐
输入一串字母 将里面的元音输出希望各位大佬能给指导
Deep learning of parameter adjustment skills
Signal and system impulse response and step response
9_ Logistic regression
Machine learning model -- lightgbm
Dynamic binding, static binding, and polymorphism
【4.10 博弈论详解】
Fourier analysis (basic introduction)
Leetcode - hash table
UNET notes
V-viewer use
画冲击函数
【AtCoder Beginner Contest 261 (A·B·C·D)】
放图仓库-3(功能图像)
寻找真凶
8_多项式回归及模型泛化(Polynomial Regression and Model Generalization)
【AtCoder Beginner Contest 261 (A·B·C·D)】
MySQL associative table queries (reducing the number of queries)
【3. Vim 操作】
CSDN article syntax rules