当前位置:网站首页>Chinese remainder theorem acwing 204. strange way of expressing integers
Chinese remainder theorem acwing 204. strange way of expressing integers
2022-07-27 11:19:00 【T_ Y_ F666】
Chinese remainder theorem AcWing 204. Strange way to express integers
Original link
AcWing 204. Strange way to express integers
Algorithm tags
Math knowledge Congruence equation Expand the Chinese Remainder Theorem
Ideas




The picture is extracted from the solution of the question
Code
#include<bits/stdc++.h>
#define int long long
#define abs fabs
#define rep(i, a, b) for(int i=a;i<b;++i)
#define Rep(i, a, b) for(int i=a;i>=b;--i)
using namespace std;
const int N = 105;
int a[N][N], eps = 1e-8;
int n;
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void put(int x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) put(x/10);
putchar(x%10^48);
}
int exgcd(int a, int b, int &x, int &y){
if(!b){
x=1, y=0;
return a;
}else{
int d=exgcd(b, a%b, y, x);
y-=a/b*x;
return d;
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
n=read();
int x=0, m1=read(), a1=read();
rep(i, 0, n-1){
int m2=read(), a2=read();
int k1, k2;
int d=exgcd(m1, m2, k1, k2);
if((a2-a1)%d){
x=-1;
break;
}
k1*=(a2-a1)/d;
k1=(k1 % (m2/d) + m2/d) % (m2/d);
x=k1*m1+a1;
int m=abs(m1/d*m2);
a1=k1*m1+a1;
m1=m;
}
if(x!=-1){
x = (a1 % m1 + m1) % m1;
}
printf("%lld",x);
return 0;
}
Originality is not easy.
Reprint please indicate the source
If it helps you Don't forget to praise and support 
边栏推荐
- A deep analysis of the soul of C language -- pointer
- 博弈论 AcWing 894. 拆分-Nim游戏
- Luogu p1441 weight weighing
- 4 search insertion location
- 11 wrong set
- Longest ascending subsequence model acwing 1014. mountaineering
- Wilderness search --- search iterations
- Wechat push - template message parameter configuration
- Learning notes - wechat payment
- Take you hand-in-hand to develop a complete classic game [Tetris] from scratch, with less than 200 lines of logic.
猜你喜欢

Data assets are king. How to analyze the relationship between enterprise digital transformation and data asset management?

Digital triangle model acwing 275. pass note

最长上升子序列模型 AcWing 1012. 友好城市

Tree DP acwing 285. dance without boss

最长上升子序列模型 AcWing 1014. 登山

Shock simulation of engine mounting system transient modal dynamic analysis and response spectrum analysis

求组合数 AcWing 887. 求组合数 III

深析C语言的灵魂 -- 指针

背包模型 AcWing 1022. 宠物小精灵之收服

Instructions for mock platform
随机推荐
BeautifulSoup的使用
背包模型 AcWing 423. 采药
数字三角形模型 AcWing 275. 传纸条
Taishan Office Technology Lecture: scaling and opening files
Openatom openharmony sub forum, see you today at 14:00! Wonderful release of memorabilia attached
栈 AcWing 3302. 表达式求值
Longest ascending subsequence model acwing 1014. mountaineering
Wenzhou University X kangaroo cloud: how to "know well" in the construction of higher talent education
Learning notes - simple server implementation
Use of pyquery
IO stream_ Overview and explanation of data input and output flow
How to build a data index system is the most effective. From 0 to 1, we will take you a quick start - 02 live review
Neural network learning notes
What is the mystery of the gate of the meta universe?
The influence of the number of non-zero values in the picture on Classification
Local and overall differences between emergence and morphology
Cancer DDD
parsel的使用
Sorry, you guys have something to deal with in the bank recently, which has been delayed
Memory search acwing 901. Skiing