当前位置:网站首页>中国剩余定理 AcWing 204. 表达整数的奇怪方式
中国剩余定理 AcWing 204. 表达整数的奇怪方式
2022-07-05 06:16:00 【T_Y_F666】
中国剩余定理 AcWing 204. 表达整数的奇怪方式
原题链接
算法标签
数学知识 同余方程 扩展中国剩余定理
思路
代码
#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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- Is it impossible for lamda to wake up?
- One question per day 1020 Number of enclaves
- [rust notes] 16 input and output (Part 2)
- Appium基础 — 使用Appium的第一个Demo
- 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
- 可变电阻器概述——结构、工作和不同应用
- [rust notes] 14 set (Part 2)
- [leetcode] day94 reshape matrix
- [BMZCTF-pwn] ectf-2014 seddit
- 实时时钟 (RTC)
猜你喜欢
Redis publish subscribe command line implementation
Leetcode-6108: decrypt messages
leetcode-6111:螺旋矩阵 IV
SQL三种连接:内连接、外连接、交叉连接
SQLMAP使用教程(二)实战技巧一
MatrixDB v4.5.0 重磅发布,全新推出 MARS2 存储引擎!
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
高斯消元 AcWing 884. 高斯消元解异或线性方程组
Doing SQL performance optimization is really eye-catching
leetcode-6110:网格图中递增路径的数目
随机推荐
Records of some tools 2022
Golang uses context gracefully
TypeScript 基础讲解
Is it impossible for lamda to wake up?
Spark中groupByKey() 和 reduceByKey() 和combineByKey()
Regulations for network security events of vocational group in 2022 Guizhou Vocational College skill competition
MySQL advanced part 2: storage engine
快速使用Amazon MemoryDB并构建你专属的Redis内存数据库
[rust notes] 17 concurrent (Part 1)
wordpress切换页面,域名变回了IP地址
Leetcode heap correlation
leetcode-9:回文数
CPU内核和逻辑处理器的区别
多屏电脑截屏会把多屏连着截下来,而不是只截当前屏
A reason that is easy to be ignored when the printer is offline
liunx启动redis
C Primer Plus Chapter 15 (bit operation)
LeetCode 0108.将有序数组转换为二叉搜索树 - 数组中值为根,中值左右分别为左右子树
[rust notes] 14 set (Part 2)
Leetcode-3: Longest substring without repeated characters