当前位置:网站首页>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;
}
边栏推荐
- What are the virtual machine software? What are their respective functions?
- Global and Chinese markets of advanced X-ray inspection system (Axi) in PCB 2022-2028: Research Report on technology, participants, trends, market size and share
- Contest3145 - the 37th game of 2021 freshman individual training match_ E: Eat watermelon
- POSTECH | option compatible reward reverse reinforcement learning
- Contest3145 - the 37th game of 2021 freshman individual training match_ D: Ranking
- Crawler practice website image batch download
- Recent learning fragmentation (14)
- Formulaire day05
- Jenkins continuous integration environment construction V (Jenkins common construction triggers)
- warning: LF will be replaced by CRLF in XXXXXX
猜你喜欢
Add IDM to Google browser
Solve the problem that the tabbar navigation at the bottom of vantui does not correspond to the page (window.loading.hash)
What are the virtual machine software? What are their respective functions?
Backpropagation formula derivation [Li Hongyi deep learning version]
Record a problem that soft deletion fails due to warehouse level error
Redis transaction
Webhook triggers Jenkins for sonar detection
No clue about the data analysis report? After reading this introduction of smartbi, you will understand!
If you have just joined a new company, don't be fired because of your mistakes
Unity controls the selection of the previous and next characters
随机推荐
Keepalived set the master not to recapture the VIP after fault recovery (it is invalid to solve nopreempt)
No clue about the data analysis report? After reading this introduction of smartbi, you will understand!
Slurm view node configuration information
Is it really so difficult to learn redis? Today, a fan will share his personal learning materials!
Solve the problems encountered by the laravel framework using mongodb
I stepped on a foundation pit today
96% of the collected traffic is prevented by bubble mart of cloud hosting
This function has none of DETERMINISTIC, NO SQL..... (you *might* want to use the less safe log_bin_t
(column 23) typical C language problem: find the minimum common multiple and maximum common divisor of two numbers. (two solutions)
7 * 24-hour business without interruption! Practice of applying multiple live landing in rookie villages
Amélioration de l'efficacité de la requête 10 fois! 3 solutions d'optimisation pour résoudre le problème de pagination profonde MySQL
Li Chuang EDA learning notes 13: electrical network for drawing schematic diagram
Fudan released its first review paper on the construction and application of multimodal knowledge atlas, comprehensively describing the existing mmkg technology system and progress
Résumé: entropie, énergie libre, symétrie et dynamique dans le cerveau
MySQL workbench use
Examination question bank of constructor decoration direction post skills (constructor) and examination data of constructor decoration direction post skills (constructor) in 2022
Site favorites
Unity writes a character controller. The mouse controls the screen to shake and the mouse controls the shooting
MySQL query
Li Chuang EDA learning notes IX: layers