intexgcd(int a, int b, int& x, int& y){ if (b == 0) { x = 1, y = 0; return a; } int d = exgcd(b, a%b, y, x); y -= a/b*x; return d; }
intmain(){ int a, b, x, m; cin >> a >> b; int d = exgcd(a, b, x, m), k = b/d; // 写 (x%k+k)%k 可能爆 int x %= k; if (x < 0) x += k; cout << x << endl; return0; }
intqmul(int a, int k, int p){ int res = 0; while (k) { if (k & 1) res = (res + a) % p; a = (a+a) % p; k >>= 1; } return res; }
intqmi(int a, int k, int p){ int res = 1; while (k) { if (k & 1) res = qmul(res, a, p); a = qmul(a, a, p); k >>= 1; } return res; }
intgcd(int a, int b){ return b ? gcd(b, a%b) : a; }
inteuler(int c){ int res = c; for (int i = 2; i <= c/i; i++) { if (c % i == 0) { res -= res/i; while (c % i == 0) c /= i; } } if (c > 1) res -= res/c; return res; }
intsolve(int l){ int c = 9*l/gcd(8, l); if (c % 2 == 0 || c % 5 == 0) return0; int phi = euler(c); int res = 1e18; for (int i = 1; i <= phi/i; i++) { if (phi % i == 0) { // 从小到大找到的一定是最小 if (qmi(10, i, c) == 1) return i; if (qmi(10, phi/i, c) == 1) res = min(res, phi/i); } } return res; }
signedmain(){ int l; signed t = 0; while (scanf("%lld", &l), l) { printf("Case %d: %lld\n", ++t, solve(l)); } return0; }
曹冲养猪
第一行包含一个整数 n —— 建立猪圈的次数,接下来 n 行,每行两个整数 ai, bi,表示建立了 ai 个猪圈,有 bi 头猪没有去处,即 x≡bi(mod ai)。你可以假定 a1~an 互质。
typedeflonglong LL; constint N = 15; LL a[N], b[N];
LL exgcd(LL a, LL b, LL& x, LL& y){ if (b == 0) { x = 1, y = 0; return a; } LL d = exgcd(b, a%b, y, x); y -= a/b*x; return d; }
intmain(){ int n; scanf("%d", &n); LL M = 1; for (int i = 0; i < n; i++) { scanf("%lld%lld", &a[i], &b[i]); M *= a[i]; } LL res = 0; for (int i = 0; i < n; i++) { LL Mi = M / a[i]; // Mi*t = ka[i] + 1 // Mi*t-ka[i]=1 LL t, k; exgcd(Mi, a[i], t, k); res += Mi*t*b[i]; } res %= M; if (res < 0) res += M; printf("%lld\n", res); return0; }