当前位置:网站首页>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
边栏推荐
- 4. Oracle redo log file management
- [2021]IBRNet: Learning Multi-View Image-Based Rendering Qianqian
- Bash exercise 17 writing scripts to install the server side of FRP reverse proxy software
- Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135
- Alibaba established the enterprise digital intelligence service company "Lingyang" to focus on enterprise digital growth
- 博弈论 AcWing 891. Nim游戏
- MySQL advanced part 1: stored procedures and functions
- New title of module a of "PanYun Cup" secondary vocational network security skills competition
- Daily question 1189 Maximum number of "balloons"
- TypeScript 基础讲解
猜你喜欢
快速使用Amazon MemoryDB并构建你专属的Redis内存数据库
MySQL advanced part 1: stored procedures and functions
Day 2 document
Open source storage is so popular, why do we insist on self-development?
SPI details
What is socket? Basic introduction to socket
Single chip computer engineering experience - layered idea
Navicat连接Oracle数据库报错ORA-28547或ORA-03135
Quickly use Amazon memorydb and build your own redis memory database
Gaussian elimination acwing 884 Gauss elimination for solving XOR linear equations
随机推荐
MySQL advanced part 2: storage engine
背包问题 AcWing 9. 分组背包问题
Paper reading report
如何正确在CSDN问答进行提问
2022-5-the fourth week daily
June 29, 2022 daily
Erreur de connexion Navicat à la base de données Oracle Ora - 28547 ou Ora - 03135
[rust notes] 16 input and output (Part 2)
Usage scenarios of golang context
How to make water ripple effect? This wave of water ripple effect pulls full of retro feeling
Redis-02.Redis命令
Sword finger offer II 058: schedule
Sqlmap tutorial (1)
Find the combination number acwing 887 Find combination number III
Leetcode stack related
4. 对象映射 - Mapping.Mapster
MySQL advanced part 1: View
7.Oracle-表结构
Record the process of configuring nccl and horovod in these two days (original)
Leetcode-31: next spread