当前位置:网站首页>[number theory] congruence (4): univariate linear congruence equations (elimination of two, Chinese remainder theorem)
[number theory] congruence (4): univariate linear congruence equations (elimination of two, Chinese remainder theorem)
2022-04-22 07:20:00 【default111】
The congruence problem is 7part, My blog links :
- Basic concepts and properties
- Inverse element : Concept 、 Solution method and derivation
- Linear congruence equation
- Higher order congruence equation :BSGS Algorithm ( Large and small step algorithm 、 Bashan algorithm )
- Fast power and fast power of matrix
Univariate Linear Congruence Equations
Form like :
{ x ≡ a 1 ( m o d m 1 ) x ≡ a 2 ( m o d m 2 ) ⋮ x ≡ a n ( m o d m n ) \begin{cases} x\equiv a_1&(\mod m_1)\\ x\equiv a_2&(\mod m_2)\\ &\vdots \\ x\equiv a_n&(\mod m_n)\\ \end{cases} ⎩⎪⎪⎪⎪⎨⎪⎪⎪⎪⎧x≡a1x≡a2x≡an(modm1)(modm2)⋮(modmn)
Method 1 : The two disappear
Let's start with two equations :
{ x ≡ a 1 ( m o d m 1 ) x ≡ a 2 ( m o d m 2 ) \begin{cases} x\equiv a_1(\mod m_1)\\ x\equiv a_2(\mod m_2)\\ \end{cases} {
x≡a1(modm1)x≡a2(modm2)
Turn into
{ x + m 1 k 1 = a 1 x + m 2 k 2 = a 2 (1) \begin{cases}{} x+m_1k_1=a_1\tag{1}\\ x+m_2k_2=a_2\\ \end{cases} {
x+m1k1=a1x+m2k2=a2(1)
Then we get a 1 − m 1 k 1 = a 2 − m 2 k 2 a_1-m_1k_1=a_2-m_2k_2 a1−m1k1=a2−m2k2 namely m 1 k 1 − m 2 k 2 = a 1 − a 2 m_1k_1-m_2k_2=a_1-a_2 m1k1−m2k2=a1−a2 Then the equivalent condition of a solution is gcd ( m 1 , m 2 ) ∣ ( a 1 − a 2 ) \gcd(m_1,m_2)\mid(a_1-a_2) gcd(m1,m2)∣(a1−a2) Use the solution of univariate linear congruence equation in the previous section to solve k 1 k_1 k1 A special solution of k ′ k' k′ , So the general solution k 1 = k ′ + c m 2 gcd ( m 1 , m 2 ) , c ∈ N k_1=k'+\frac{cm_2}{\gcd(m_1,m_2)},c\in N k1=k′+gcd(m1,m2)cm2,c∈N Plug in ( 1 ) (1) (1) Type available , x = a 1 − m 1 k ′ + c m 1 m 2 gcd ( m 1 , m 2 ) x=a_1-m_1k'+\frac{cm_1m_2}{\gcd(m_1,m_2)} x=a1−m1k′+gcd(m1,m2)cm1m2 So it is transformed into a new congruence equation : x ≡ a 1 − m 1 k ′ ( m o d lcm ( m 1 , m 2 ) ) x\equiv a_1-m_1k'(\mod \text{lcm}(m_1,m_2)) x≡a1−m1k′(modlcm(m1,m2)) Merge in turn to get the final answer .
// solve x==a[] mod m[], Array from 0 Numbered starting , common n Formulas
int mod_equations(int a[], int m[], int n)
{
int a1 = a[0], a2, m1 = m[0], m2, k1, k, d;
for (int i = 1; i < n; i++)
{
a2 = a[i], m2 = m[i];
int k2, tmp;
exgcd(m1, m2, d, k1, k2);
if ((a2 - a1) % d)
return -1;
tmp = m2 / d;
k1 = (k1 * (a2 - a1) / d % tmp + tmp) % tmp; // Special solution k', Ensure the minimum positive number
a1 += m1 * k1;
m1 *= m2 / d;
a1 = (a1 + m1) % m1;
}
return a1; // The general solution is a1+cm1,c Is an integer
}
Method 2 : Chinese remainder theorem
Conditions : Original equations m 1 , m 2 ⋯ m n m_1,m_2\cdots m_n m1,m2⋯mn Relatively prime
Theorem : Patterned M = m 1 ⋅ m 2 ⋯ m n M=m_1\cdot m_2\cdots m_n M=m1⋅m2⋯mn The only solution
deduction : Make M i = M / m i M_i=M/m_i Mi=M/mi , Mutual prime knowledge gcd ( M i , m i ) = 1 \gcd(M_i,m_i)=1 gcd(Mi,mi)=1 , also M i ⋅ M i − 1 ≡ 1 ( m o d m i ) M_i\cdot M_i^{-1}\equiv 1(\mod m_i) Mi⋅Mi−1≡1(modmi) , The general solution is c M + ∑ i = 1 n a i M i M i − 1 cM+\sum_{i=1}^{n}a_iM_iM_i^{-1} cM+i=1∑naiMiMi−1
// Chinese remainder definite understanding univariate Linear Congruence Equations , Array from 0 label
ll CRT(int a[], int m[], int n)
{
ll M = 1ll, ans = 0;
for (int i = 0; i < n; i++)
M *= m[i];
for (int i = 0; i < n; i++)
ans = (ans + a[i] * M % M * inv(M) % M) % M;
return ans;
}
版权声明
本文为[default111]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220611440700.html
边栏推荐
- [SVN] subversion installation notes
- Prompt the user to enter his name, and the user will write his name to the file guest Txt program determines that when it is not equal to N, it executes to create the file data Txt, a total of 100000
- 短路
- If an error is reported, process the ES6 filter to filter the array
- 安裝和修改uTools及vscode插件安裝路徑
- Write a method Sanjiao (a, B, c) to judge whether the three parameters can form a triangle. If not, throw an exception illegalargumentexception and display the exception information a, B, C "cannot fo
- a5 transceiver 信号vod和预加重调整关系
- Pyhon3 batch merges M4S video files cached by BiliBili
- Zhejiang University Edition "C language programming (3rd Edition)" topic set exercise 7-4 find out the elements that are not common to two arrays
- JS实现点击头像上传图片修改
猜你喜欢

Choose any novel website, crawl any novel and save it in the form of Notepad.

【Bug小记】input:-webkit-autofill:输入框自动填充背景问题

a5 transceiver 信号vod和预加重调整关系

(四)Sql Server中的字符集(排序规则)

Nacos cluster configuration

Beyond Compare“授权密钥已被吊销”的解决办法

MySQL learning notes

(二)Sql Server的基本配置以及使用Navicat连接Sql Server
![[DRC RTSTAT-1] Unrouted nets: 1 net(s) are unrouted](/img/bf/0851c85f766432f6d6306d52e67188.png)
[DRC RTSTAT-1] Unrouted nets: 1 net(s) are unrouted

14行代码完成任意选择图片爬取
随机推荐
SQLSERVER存储过程开发笔记----零碎问题以及关于操作文件的操作
Leetcode punch in
微信浏览器无法长期保存cookie
Practice of using generics and reflection (including a generic cache) -- handwritten ORM ORM framework
[bug notes] the sessionstorage data cannot be obtained after the page is refreshed
Format control of format() method
Quartus II防止信号被综合
ASP. Net daily development notes ----- WebService server development
Distributed task scheduling and computing framework: powerjob advanced features - OpenAPI 04
【数论】同余(二):逆元
日常开发随手记------VS上一些好用的设置
Mongodb install self start service
JS实现点击头像上传图片修改
[jeecg] modify VISER chart color style
【数论】同余(四):一元线性同余方程组(两两相消、中国剩余定理)
CSDN text style
安装和修改uTools及vscode插件安装路径
jeecg项目部署笔记
浙大版《C语言程序设计(第3版)》题目集 练习7-4 找出不是两个数组共有的元素
XPath of crawler notes