当前位置:网站首页>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;
}
边栏推荐
- CAD ARX 获取当前的视口设置
- Luogu p4127 [ahoi2009] similar distribution problem solution
- Cf1036c class numbers solution
- Make learning pointer easier (3)
- Inspiration from the recruitment of bioinformatics analysts in the Department of laboratory medicine, Zhujiang Hospital, Southern Medical University
- [Yugong series] February 2022 U3D full stack class 011 unity section 1 mind map
- 让学指针变得更简单(三)
- Linked list interview questions (Graphic explanation)
- 【T31ZL智能视频应用处理器资料】
- Asia Pacific Financial Media | designer universe | Guangdong responds to the opinions of the national development and Reform Commission. Primary school students incarnate as small community designers
猜你喜欢
Go learning notes (3) basic types and statements (2)
Risk planning and identification of Oracle project management system
Golang DNS write casually
Asia Pacific Financial Media | female pattern ladyvision: forced the hotel to upgrade security. The drunk woman died in the guest room, and the hotel was sentenced not to pay compensation | APEC secur
File upload of DVWA range
【T31ZL智能视频应用处理器资料】
Machine learning - decision tree
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
将 NFT 设置为 ENS 个人资料头像的分步指南
"Designer universe" Guangdong responds to the opinions of the national development and Reform Commission. Primary school students incarnate as small community designers | national economic and Informa
随机推荐
上线APS系统,破除物料采购计划与生产实际脱钩的难题
Qualitative risk analysis of Oracle project management system
NFT smart contract release, blind box, public offering technology practice -- jigsaw puzzle
Launch APS system to break the problem of decoupling material procurement plan from production practice
Easy to use tcp-udp_ Debug tool download and use
Position() function in XPath uses
[1. Delphi foundation] 1 Introduction to Delphi Programming
National economic information center "APEC industry +": economic data released at the night of the Spring Festival | observation of stable strategy industry fund
【云原生】手把手教你搭建ferry开源工单系统
Inspiration from the recruitment of bioinformatics analysts in the Department of laboratory medicine, Zhujiang Hospital, Southern Medical University
Database basic commands
珠海金山面试复盘
在 uniapp 中使用阿里图标
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
P3047 [usaco12feb]nearby cows g (tree DP)
Epoll and IO multiplexing of redis
How to estimate the number of threads
1015 reversible primes (20 points) prime d-ary
Notes on software development
Generator Foundation