2 条题解
-
0问号 (周文浩) LV 10 @ 2024-3-30 20:30:20
#include <iostream> #include <algorithm> using namespace std; typedef long long LL; int n; LL exgcd(LL a, LL b, LL &x, LL &y) { if(!b) { x = 1, y = 0; return a; } LL d = exgcd(b, a % b, y, x); y -= a / b * x; return d; } int main () { cin >> n; bool flag = true; LL a1, m1; cin >> a1 >> m1; for(int i = 0; i < n - 1; i ++ ) { LL a2, m2; cin >> a2 >> m2; //先用扩展欧几里得求出k1和k2 LL k1, k2; LL d = exgcd(a1, a2, k1, k2); if((m2 - m1) % d) { flag = false; break; } k1 *= (m2 - m1) / d; LL t = a2 / d; k1 = (k1 % t + t) % t; m1 = a1 * k1 + m1; a1 = abs(a1 / d * a2); } if(flag) { cout << (m1 % a1 + a1) % a1; } else cout << "-1" << endl; return 0; }
-
02023-9-29 14:49:25@
#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=s10+ch-'0',ch=getchar(); return sw; } 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/bx; 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=k1m1+a1; int m=abs(m1/dm2); a1=k1*m1+a1; m1=m; } if(x!=-1){ x = (a1 % m1 + m1) % m1; } printf("%lld",x); return 0; }
- 1
信息
- ID
- 115
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 8
- 标签
- 递交数
- 11
- 已通过
- 9
- 上传者