当前位置:网站首页>Chinese Remainder Theorem (Sun Tzu theorem) principle and template code
Chinese Remainder Theorem (Sun Tzu theorem) principle and template code
2022-07-06 07:59:00 【Alkali!】
Background
Sun Tzu theorem is ancient China Solve the group of primary congruences Methods . Is an important theorem in number theory . Also known as Chinese Remainder Theorem . The system of Linear Congruence Equations with one variable can be seen in the northern and Southern Dynasties of China ( A.D. 5 century ) Mathematical works of 《 Sun Tzu's Sutra 》 Volume II question 26 , be called “ I don't know the number of things ” problem , The text is as follows :
I don't know the number of things , Two out of three , Three out of five , Two of the seven . Query geometry ? namely , An integer divided by three and two , Divide by five and three , Divide by seven and two , Find this integer .《 Sun Tzu's Sutra 》 The problem of Congruence Equations is mentioned for the first time , And the solution of the above specific problems , Therefore, the Chinese remainder theorem will also be called the Sun Tzu theorem in the Chinese mathematical literature .
principle


Template code






#include<iostream>
using namespace std;
typedef long long LL; // The data range is quite large , Here we use long long
LL exgcd(LL a,LL b,LL &x,LL &y) // extended euclidean algorithm
{
if(!b)
{
x=1,y=0;
return a;
}
LL ans=exgcd(b,a%b,x,y);
LL t=x;
x=y;
y=t-a/b*y;
return ans;
}
LL mod(LL a,LL b) // Guarantee a%b The value after taking the module must be non negative
{
return (a%b+b)%b;
}
int n;
int main()
{
scanf("%d",&n);
LL m1,a1,m2,a2,k1,k2;
scanf("%lld%lld",&a1,&m1);
for(int i=1;i<n;i++)
{
scanf("%lld%lld",&a2,&m2);
LL d=exgcd(a1,-a2,k1,k2);
if((m2-m1)%d) // If it's not divisible , Explain that there is no solution
{
printf("%d",-1);
return 0;
}
k1=mod(k1*(m2-m1)/d,abs(a2/d)); // Take mold at any time , Ensure the minimum number in the calculation process , Give Way k The value of is always optimal
m1=k1*a1+m1; // At this time m and a After the merger x In the expression m and a
a1=abs(a1/d*a2); //
}
printf("%lld",mod(m1,a1)); // The answer is this
return 0;
}
边栏推荐
猜你喜欢

Nft智能合约发行,盲盒,公开发售技术实战--合约篇

Easy to use tcp-udp_ Debug tool download and use
![[Yugong series] February 2022 U3D full stack class 011 unity section 1 mind map](/img/c3/1b6013bfb2441219bf621c3f0726ea.jpg)
[Yugong series] February 2022 U3D full stack class 011 unity section 1 mind map

. Net 6 learning notes: what is NET Core

Epoll and IO multiplexing of redis

【Redis】NoSQL数据库和redis简介
![08- [istio] istio gateway, virtual service and the relationship between them](/img/fb/09793f5fd12c2906b73cc42722165f.jpg)
08- [istio] istio gateway, virtual service and the relationship between them

Parameter self-tuning of relay feedback PID controller

Inspiration from the recruitment of bioinformatics analysts in the Department of laboratory medicine, Zhujiang Hospital, Southern Medical University

In the era of digital economy, how to ensure security?
随机推荐
Luogu p1836 number page solution
Opencv learning notes 8 -- answer sheet recognition
Wireshark grabs packets to understand its word TCP segment
Mex related learning
Circuit breaker: use of hystrix
上线APS系统,破除物料采购计划与生产实际脱钩的难题
Description of octomap averagenodecolor function
Artcube information of "designer universe": Guangzhou implements the community designer system to achieve "great improvement" of urban quality | national economic and Information Center
继电反馈PID控制器参数自整定
数据治理:数据质量篇
[非线性控制理论]9_非线性控制理论串讲
[Yugong series] creation of 009 unity object of U3D full stack class in February 2022
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Pre knowledge reserve of TS type gymnastics to become an excellent TS gymnastics master
链表面试题(图文详解)
Oracle time display adjustment
Data governance: Data Governance under microservice architecture
Machine learning - decision tree
File upload of DVWA range
MySQL view tablespace and create table statements