LL solve(LL n){ if (n < fir) return0; return (LL)ceil((double)(n - fir + 1) / (double)cc); }
inlinevoidmain(){ read(a), read(aa), read(b), read(bb), read(l), read(r); if (a < b) swap(a, b), swap(aa, bb); d1 = aa - a, d2 = bb - b; for (int i = a; i <= 1e5; ++i) { if ((i - a) % d1 == 0 && (i - b) % d2 == 0) { if (!fir) fir = i; else { cc = i - fir; break; } } } printf("%lld\n", solve(r) - solve(l-1)); }
inlineboolcheck(){ int sum = 0; for (int i = 1; i <= n; ++i) { if (i > B) sum -= str[i - B] == '1'; sum += str[i] == '1'; if (i >= B && sum < L) returnfalse; } returntrue; }
inlinevoidmain(){ read(L), read(B), read(n); scanf("%s", str+1); if (!check()) returnputs("IMPOSSIBLE"), void();
int l = 1, r = L; g[L] = 1; for (int i = L + 1; i <= n; ++i) { if (str[i] == '1') { r = i, l++; while (str[l] == '0') l++; } g[i] = l; } l = L, r = B; while (r < n) { ans += L; l = r, r = g[r] + B - 1; } if (l < n) ans += L; printf("%lld\n", ans + n - B); }
int K, m; int dis[N], ans[N], pre[N], q[N]; vector<int> e[N]; vector<char> output;
inlinevoidmain(){ read(K), read(m); for (int u = 0; u < m; ++u) { for (int j = 0; j < K; ++j) { int v = (u * 10 + j) % m; e[u].push_back(v); } pre[u] = -1; } memset(dis, 0x3f, sizeof dis); int ql = 1, qr = 0; for (int u = 1; u < K; ++u) { ans[u] = u; dis[u] = 0; q[++qr] = u; } while (ql <= qr) { int u = q[ql++]; for (int i = 0; i < e[u].size(); ++i) { int v = e[u][i]; if (dis[v] > dis[u] + 1) { ans[v] = i; dis[v] = dis[u] + 1; pre[v] = u; q[++qr] = v; } } } for (int u = 0; u != -1; u = pre[u]) { output.push_back(ans[u] + '0'); } for (int i = output.size() - 1; ~i; --i) putchar(output[i]); puts(""); }