vector<int> a(n), b(n); for (auto &x : a) x = read(); for (auto &x : b) x = read();
int L = 1; for (auto x : a) L = lcm(L, x);
int N = L * n; vector safe(L, vector(n, 1ll)); for (int x = 0; x < L; x++) { for (int t = 0; t < n; t++) { int i = (t - 1 + n) % n; if (x % a[i] == b[i]) { safe[x][t] = 0; } } }
vector f(N, -1ll), cost(N, 0ll); auto get = [&](int id) -> void { if (f[id] != -1) return;
int x = id / n; int r = id % n;
for (int w = 0; w < n; w++) { int ok = 1; for (int k = 1; k <= w; k++) { int t = (r + k) % n; if (!safe[x][t]) { ok = 0; break; } } if (!ok) continue;
int nx = (x + 1) % L; int nr = (r + w + 1) % n; int nid = nx * n + nr; if (safe[nx][nr]) { f[id] = nid; cost[id] = w + 1; return; }
}
f[id] = -2; };
vector sum(N, 0ll), d(N, -1ll); int id = 0, tot = 0, step = 0; int cur = id;
while (step < m and d[cur] == -1) { d[cur] = step; sum[cur] = tot;
if (step == m) { cout << tot << '\n'; } else { int clen = step - d[cur]; int ccost = tot - sum[cur]; int need = m - d[cur]; int ans = sum[cur] + need / clen * ccost; for (int i = 0; i < need % clen; i++) { get(cur); ans += cost[cur]; cur = f[cur]; } cout << ans << '\n'; } }
structTag { int x = 0; voidapply(const Tag &t) & { x += t.x; } };
structInfo { int x = inf; Info() = default; Info(int v) : x(v) {} voidapply(const Tag &t) & { x += t.x; } };
Info operator+(const Info &a, const Info &b) { return {min(a.x, b.x)}; }
voidsolve(){ int ac = read(); int dr = read(); int n = read(); vector<int> a(n + 1), d(n + 1); for (int i = 1; i <= n; i++) a[i] = read(); for (int i = 1; i <= n; i++) d[i] = read();
auto calc = [&](int i) -> int { int res = max(a[i] - ac, 0ll) + max(d[i] - dr, 0ll); returnmin(n + 1, res); };
vector<int> cnt(n + 2); for (int i = 1; i <= n; i++) { cnt[calc(i)]++; } vector<int> pre(n + 3); for (int i = 1; i <= n + 1; i++) { pre[i] = pre[i - 1] + cnt[i - 1]; } auto f = pre; for (int i = 0; i <= n + 1; i++) f[i] -= i; LazySegmentTree<Info, Tag> seg(f); // debug(seg.n);
int m = read(); while (m--) { int k = read(); int na = read(); int nd = read();
seg.rangeApply(calc(k) + 1, n + 2, {-1}); a[k] = na, d[k] = nd; seg.rangeApply(calc(k) + 1, n + 2, {+1});
int res = seg.findFirst(0, n + 2, [&](auto i) { return i.x < 0; }); if (res == -1) res = n + 1; cout << res - 1 << '\n'; } }
voidsolve(){ int n = read(); int k = read(); string s; cin >> s;
int cnt[3] { }; for (auto c : s) { ++cnt[c - '0']; }
string ans; for (int i = 1; i <= n; ++ i) { if (cnt[0] < i and cnt[1] <= n - i) { if (cnt[0] + cnt[2] < i and cnt[1] + cnt[2] <= n - i) { ans += '+'; } else { if (cnt[0] + cnt[1] + cnt[2] == n) { ans += '-'; } else { ans += '?'; } } } else { ans += '-'; } } cout << ans << '\n'; }
A - Candies for Nephews
1 2 3 4 5
voidsolve(){ int n = read(); int ans = (n + 2) / 3 * 3 - n; cout << ans << '\n'; }
Educational Codeforces Round 183 (Rated for Div. 2)