namespace IO { /* 省略 */ } using IO::read; using IO::write;
constint N = 12; int n, m[N], a[N];
pair<int, int> exgcd(int a, int b){ if (!b) return {1, 0}; auto [y, x] = exgcd(b, a % b); return {x, y - a / b * x}; }
signedmain(){ int prod = 1, res = 0; read(n); for (int i = 1; i <= n; i++) read(m[i]); for (int i = 1; i <= n; i++) read(a[i]), prod *= a[i]; for (int i = 1; i <= n; i++) { int Mi = prod / a[i]; int Ti = exgcd(Mi, a[i]).first; res = (res + Mi * Ti * m[i]) % prod; } if (res < 0) res += prod; returnwrite(res), 0; }
namespace IO { /* 省略 */ } using IO::read; using IO::write;
intexgcd(int a, int b, int& x, int& y){ if (!b) return x = 1, y = 0, a; int d = exgcd(b, a % b, y, x); y -= a / b * x; return d; }
voidmerge(int a1, int a2, int m1, int m2, int& A, int& M){ int k1, k2, d = exgcd(m1, m2, k1, k2); k1 *= (a2 - a1) / d; M = m1 / d * m2; A = (k1 * m1 + a1) % M; if (A < 0) A += M; }
signedmain(){ int n, a1, m1, a2, m2; read(n, m1, a1); for (int i = 2; i <= n; i++) { read(m2, a2); merge(a1, a2, m1, m2, a1, m1); } returnwrite(a1), 0; }
[TJOI2009] 猜数字
给定 n 个元素的数列 a,b 问满足 bi∣(x−ai) 的最小整数解,1≤n≤10,∣ai∣≤109,1≤∏bi≤1018,满足 bi 两两互质。