当前位置:网站首页>Chinese remainder theorem acwing 204 Strange way of expressing integers
Chinese remainder theorem acwing 204 Strange way of expressing integers
2022-07-05 06:24: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 
边栏推荐
- 2022/6/29-日报
- ADG5412FBRUZ-RL7应用 双电源模拟开关和多路复用器IC
- Gaussian elimination acwing 884 Gauss elimination for solving XOR linear equations
- [moviepy] unable to find a solution for exe
- Filter the numbers and pick out even numbers from several numbers
- [rust notes] 17 concurrent (Part 2)
- MySQL advanced part 2: MySQL architecture
- Doing SQL performance optimization is really eye-catching
- 3. Oracle control file management
- Bash exercise 17 writing scripts to install the server side of FRP reverse proxy software
猜你喜欢

Is it impossible for lamda to wake up?

Leetcode-6111: spiral matrix IV

MySQL advanced part 1: index

Alibaba's new member "Lingyang" officially appeared, led by Peng Xinyu, Alibaba's vice president, and assembled a number of core department technical teams

International Open Source firmware Foundation (osff) organization

3.Oracle-控制文件的管理

4. Object mapping Mapster

什么是套接字?Socket基本介绍

Series of how MySQL works (VIII) 14 figures explain the atomicity of MySQL transactions and the principle of undo logging

Traditional databases are gradually "difficult to adapt", and cloud native databases stand out
随机推荐
[moviepy] unable to find a solution for exe
Records of some tools 2022
求组合数 AcWing 887. 求组合数 III
Install opencv -- CONDA to establish a virtual environment and add the kernel of this environment in jupyter
MySQL advanced part 2: SQL optimization
Filter the numbers and pick out even numbers from several numbers
[rust notes] 17 concurrent (Part 2)
中国剩余定理 AcWing 204. 表达整数的奇怪方式
5.Oracle-表空间
11-gorm-v2-03-basic query
容斥原理 AcWing 890. 能被整除的数
Bash exercise 17 writing scripts to install the server side of FRP reverse proxy software
区间问题 AcWing 906. 区间分组
Leetcode-9: palindromes
Navicat连接Oracle数据库报错ORA-28547或ORA-03135
TCP's understanding of three handshakes and four waves
LeetCode 0108. Convert an ordered array into a binary search tree - the median of the array is the root, and the left and right of the median are the left and right subtrees respectively
Client use of Argo CD installation
论文阅读报告
快速使用Amazon MemoryDB并构建你专属的Redis内存数据库