当前位置:网站首页>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;
}
边栏推荐
- 1day vulnerability pushback skills practice (3)
- Solve the problems encountered by the laravel framework using mongodb
- Imperial cms7.5 imitation "D9 download station" software application download website source code
- Baijia forum the founding of the Eastern Han Dynasty
- [Valentine's Day confession code] - Valentine's Day is approaching, and more than 10 romantic love effects are given to the one you love
- Development of digital collection trading platform development of digital collection platform
- Rhcsa day 3
- Record a problem that soft deletion fails due to warehouse level error
- (column 23) typical C language problem: find the minimum common multiple and maximum common divisor of two numbers. (two solutions)
- Rhcsa day 2
猜你喜欢
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"
@Scheduled scheduled tasks
Record a problem that soft deletion fails due to warehouse level error
Li Chuang EDA learning notes 13: electrical network for drawing schematic diagram
Www 2022 | taxoenrich: self supervised taxonomy complemented by Structural Semantics
Kiss number + close contact problem
How to use websocket to realize simple chat function in C #
Have you entered the workplace since the first 00???
Imperial cms7.5 imitation "D9 download station" software application download website source code
Add token validation in swagger
随机推荐
static hostname; transient hostname; pretty hostname
Practical multifunctional toolbox wechat applet source code / support traffic master
Global and Chinese market of cell scrapers 2022-2028: Research Report on technology, participants, trends, market size and share
Development of digital collection trading platform development of digital collection platform
Latex tips slash \backslash
Day05 錶格
Network communication basic kit -- IPv4 socket structure
AI 助力藝術設計抄襲檢索新突破!劉芳教授團隊論文被多媒體頂級會議ACM MM錄用
Global and Chinese market for travel wheelchairs 2022-2028: Research Report on technology, participants, trends, market size and share
Solve the problems encountered by the laravel framework using mongodb
[untitled]
Enhanced for loop
Unity controls the selection of the previous and next characters
Setting methods, usage methods and common usage scenarios of environment variables in postman
New year's first race, submit bug reward more!
MySQL is dirty
Résumé: entropie, énergie libre, symétrie et dynamique dans le cerveau
Handler source code analysis
Hospital network planning and design document based on GLBP protocol + application form + task statement + opening report + interim examination + literature review + PPT + weekly progress + network to
Command Execution Vulnerability - command execution - vulnerability sites - code injection - vulnerability exploitation - joint execution - bypass (spaces, keyword filtering, variable bypass) - two ex