当前位置:网站首页>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 is the difference between enterprise wechat applet and wechat applet
- This function has none of DETERMINISTIC, NO SQL..... (you *might* want to use the less safe log_bin_t
- 150 ppt! The most complete "fair perception machine learning and data mining" tutorial, Dr. AIST Toshihiro kamishima, Japan
- Slurm view node configuration information
- Global and Chinese market of box seals 2022-2028: Research Report on technology, participants, trends, market size and share
- Safety tips - seat belt suddenly fails to pull? High speed police remind you how to use safety belts in a standardized way
- Package and download 10 sets of Apple CMS templates / download the source code of Apple CMS video and film website
- @Scheduled scheduled tasks
- Handler source code analysis
- How about the ratings of 2022 Spring Festival Gala in all provinces? Map analysis helps you show clearly!
猜你喜欢

Rhcsa day 2
![[Wu Enda deep learning] beginner learning record 3 (regularization / error reduction)](/img/e9/818bdfeae766dca7d2318b52b4424d.jpg)
[Wu Enda deep learning] beginner learning record 3 (regularization / error reduction)

150 ppt! The most complete "fair perception machine learning and data mining" tutorial, Dr. AIST Toshihiro kamishima, Japan

Code Execution Vulnerability - no alphanumeric rce create_ function()
![[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush](/img/94/2bdc31ec05595dbbc8a7a8d6b22252.jpg)
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush

1day vulnerability pushback skills practice (3)

The first spring of the new year | a full set of property management application templates are presented, and Bi construction is "out of the box"

Add token validation in swagger

Jenkins continuous integration environment construction V (Jenkins common construction triggers)

Command Execution Vulnerability - command execution - vulnerability sites - code injection - vulnerability exploitation - joint execution - bypass (spaces, keyword filtering, variable bypass) - two ex
随机推荐
Constantly changing harmonyos custom JS components during the Spring Festival - Smart Koi
AI 助力藝術設計抄襲檢索新突破!劉芳教授團隊論文被多媒體頂級會議ACM MM錄用
Monitoring - Prometheus introduction
Measurement fitting based on Halcon learning [4] measure_ arc. Hdev routine
Li Chuang EDA learning notes IX: layers
Zhihu million hot discussion: why can we only rely on job hopping for salary increase? Bosses would rather hire outsiders with a high salary than get a raise?
The "message withdrawal" of a push message push, one click traceless message withdrawal makes the operation no longer difficult
Ai aide à la recherche de plagiat dans le design artistique! L'équipe du professeur Liu Fang a été embauchée par ACM mm, une conférence multimédia de haut niveau.
Setting methods, usage methods and common usage scenarios of environment variables in postman
what does ctrl + d do?
Don't disagree, this is the most powerful "language" of the Internet
Basé sur... Netcore Development blog Project Starblog - (14) Implementation of theme switching function
Formulaire day05
warning: LF will be replaced by CRLF in XXXXXX
What are the conditions for the opening of Tiktok live broadcast preview?
1day vulnerability pushback skills practice (3)
Network byte order
Problems and solutions of several concurrent scenarios of redis
Rhcsa day 2
Tsinghua University product: penalty gradient norm improves generalization of deep learning model