当前位置:网站首页>Summary of Chinese remainder theorem
Summary of Chinese remainder theorem
2022-07-04 03:19:00 【lzz£】
1. Grandson's question
Earliest , stay 《 Sun Tzu's Sutra 》 There is such a problem :“ I don't know how many things there are today , Two out of three , Three out of five , Two of the seven , Query geometry ? In colloquial terms, it means , Now there is a number I don't know , Only know this number divided by 3 more than 2, Divide 5 more than 3, Divide 7 more than 2, What is this number ?
The above problem can be transformed into the following equation system :

Among them the x It's the number we require . The Chinese remainder theorem is used to solve the above form of equations x Solution .
2. Chinese remainder theorem formula
Set positive integers m1,m2,....,mk pairwise coprime , Then Congruence Equations :


3. Solve examples
AcWing 204 Strange way to express integers
Their thinking :

The code is as follows :
#include<iostream>
using namespace std;
typedef long long ll;// Prevent data overflow
ll exgcd(ll a,ll b,ll &x,ll &y)
{
if(!b)
{
x=1,y=0;
return a;
}
ll d=exgcd(b,a%b,y,x);
y-=a/b*x;
return d;
}// extended euclidean algorithm
int main()
{
int n;
cin>>n;
bool flag=true;// Is there the smallest x value
ll a1,m1;
cin>>a1>>m1;
for(int i=0;i<n-1;i++)
{
ll a2,m2;
cin>>a2>>m2;
ll k1,k2;
ll d=exgcd(a1,a2,k1,k2);// Find the greatest common divisor
if((m2-m1)%d)// If m2-m1 No a1,a2 Multiple of the greatest common divisor of
{
flag=false;
break;
}
k1*=(m2-m1)/d;// because m2-m1 For the greatest common divisor d Expanded n times , therefore k1 It also needs to be expanded
ll t=a2/d;
k1=(k1%t+t)%t;// This problem is to find the minimum x, So find the smallest k1 value
m1=a1*k1+m1;// use m1 To represent the current x Value
a1=abs(a1/d*a2);// Find out a1,a2 The least common multiple of
}
if(flag) // If it exists, output the smallest x value
cout<<(m1%a1+a1)%a1<<endl;
else
puts("-1");
return 0;
}
边栏推荐
- ZABBIX API pulls the values of all hosts of a monitoring item and saves them in Excel
- Handler source code analysis
- Cache general management class + cache httpcontext Current. Cache and httpruntime Differences between caches
- Enhanced for loop
- Base d'apprentissage de la machine: sélection de fonctionnalités avec lasso
- [latex] production of complex tables: excel2latex and detail adjustment
- Global and Chinese market of contour projectors 2022-2028: Research Report on technology, participants, trends, market size and share
- Résumé des outils communs et des points techniques de l'examen PMP
- Easy to win insert sort
- Redis transaction
猜你喜欢

If you have just joined a new company, don't be fired because of your mistakes
![[Valentine's Day confession code] - Valentine's Day is approaching, and more than 10 romantic love effects are given to the one you love](/img/ab/066923f1aa1e8dd8dcc572cb60a25d.jpg)
[Valentine's Day confession code] - Valentine's Day is approaching, and more than 10 romantic love effects are given to the one you love
![Backpropagation formula derivation [Li Hongyi deep learning version]](/img/ef/f76eae39c4f8716a0030a60c85b09c.gif)
Backpropagation formula derivation [Li Hongyi deep learning version]
![[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush](/img/98/3e5f1094141e34d7e77f908e12acda.jpg)
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush

This function has none of DETERMINISTIC, NO SQL..... (you *might* want to use the less safe log_bin_t

Tsinghua University product: penalty gradient norm improves generalization of deep learning model

Imperial cms7.5 imitation "D9 download station" software application download website source code

(column 23) typical C language problem: find the minimum common multiple and maximum common divisor of two numbers. (two solutions)

AI 助力藝術設計抄襲檢索新突破!劉芳教授團隊論文被多媒體頂級會議ACM MM錄用

Node solves cross domain problems
随机推荐
Li Chuang EDA learning notes IX: layers
Eh, the log time of MySQL server is less than 8h?
7 * 24-hour business without interruption! Practice of applying multiple live landing in rookie villages
CSCI 2134
POSTECH | option compatible reward reverse reinforcement learning
Package and download 10 sets of Apple CMS templates / download the source code of Apple CMS video and film website
The "two-way link" of pushing messages helps app quickly realize two-way communication capability
Measurement fitting based on Halcon learning [4] measure_ arc. Hdev routine
Webhook triggers Jenkins for sonar detection
Want to do something in production? Then try these redis commands
Résumé: entropie, énergie libre, symétrie et dynamique dans le cerveau
MySQL is dirty
Global and Chinese market of contour projectors 2022-2028: Research Report on technology, participants, trends, market size and share
Easy to win insert sort
VRRP+BFD
How to use websocket to realize simple chat function in C #
Contest3145 - the 37th game of 2021 freshman individual training match_ G: Score
(practice C language every day) pointer sorting problem
warning: LF will be replaced by CRLF in XXXXXX
How to pipe several commands in Go?