#include<bits/stdc++.h> // #define int long long using pii = std::pair<int, int>; using tuu = std::tuple<int, int, int>; #define pb push_back using std::cin, std::cout, std::string, std::vector; intread(int x = 0, int f = 0, char ch = getchar()) { while (ch < 48or57 < ch) f = ch == 45, ch = getchar(); while(48 <= ch and ch <= 57) x = x * 10 + ch - 48, ch = getchar(); return f ? -x : x; } constint N = 1e6 + 5; // const int INF = 1e18; int n, m, a[N]; voidsolve() { n = read(), m = read(); if (m >= n - 1) returnputs("1"), void(); int k = n - 1; while (m >= k and k > 0) n--, m -= k, k--; cout << n << '\n'; }
signedmain() { #ifndef ONLINE_JUDGE freopen("A.in", "r", stdin); #endif for (int T = read(); T--; solve()); return0; }
#include<bits/stdc++.h> // #define int long long using pii = std::pair<int, int>; using tuu = std::tuple<int, int, int>; #define pb push_back using std::cin, std::cout, std::string, std::vector; intread(int x = 0, int f = 0, char ch = getchar()) { while (ch < 48or57 < ch) f = ch == 45, ch = getchar(); while(48 <= ch and ch <= 57) x = x * 10 + ch - 48, ch = getchar(); return f ? -x : x; } constint N = 1e6 + 5; // const int INF = 1e18; int n, m, a[N]; voidsolve() { n = read(), m = read() * 2; int cnta = m, cntb = m; vector<int> a(n), b(n), ansa, ansb; for (int i = 0; i < n; i++) a[i] = read() - 1; for (int i = 0; i < n; i++) b[i] = read() - 1; vector<int> visa(n, 0), visb(n, 0), pushed(n, 0); for (int i = 0; i < n and cnta > 0; i++) { if (visa[a[i]] and cnta >= 2) cnta -= 2, ansa.pb(a[i]), ansa.pb(a[i]), pushed[a[i]] = 1; visa[a[i]] = 1; } for (int i = 0; i < n and cntb != cnta; i++) { if (visb[b[i]] and cntb >= 2) cntb -= 2, ansb.pb(b[i]), ansb.pb(b[i]), pushed[b[i]] = 1; visb[b[i]] = 1; } for (int i = 0; i < n and cnta > 0; i++) if (!pushed[a[i]]) ansa.pb(a[i]), ansb.pb(a[i]), cnta --; for (auto x : ansa) cout << x + 1 << ' '; puts(""); for (auto x : ansb) cout << x + 1 << ' '; puts(""); }
signedmain() { #ifndef ONLINE_JUDGE freopen("B.in", "r", stdin); #endif for (int T = read(); T--; solve()); return0; }
#include<bits/stdc++.h> // #define int long long using pii = std::pair<int, int>; using tuu = std::tuple<int, int, int>; #define pb push_back using std::cin, std::cout, std::string, std::vector; intread(int x = 0, int f = 0, char ch = getchar()) { while (ch < 48or57 < ch) f = ch == 45, ch = getchar(); while(48 <= ch and ch <= 57) x = x * 10 + ch - 48, ch = getchar(); return f ? -x : x; } constint N = 1e6 + 5; // const int INF = 1e18; int n, m, a[N]; voidsolve() { n = read(); vector<int> cnt(n + 1, 0); for (int i = 1; i <= n; i++) a[i] = read(), cnt[a[i]] ++; for (int i = 0, tmp = 0; i <= n; i++) { tmp += (cnt[i] == 1); if (!cnt[i] or cnt[i] and tmp == 2) { cout << i << '\n'; break; } } }
signedmain() { #ifndef ONLINE_JUDGE freopen("C.in", "r", stdin); #endif for (int T = read(); T--; solve()); return0; }
D
A string $t$ is said to be $k$-good if there exists at least one substring$^\dagger$ of length $k$ which is not a palindrome$^\ddagger$. Let $f(t)$ denote the sum of all values of $k$ such that the string $t$ is $k$-good.
You are given a string $s$ of length $n$. You will have to answer $q$ of the following queries:
Given $l$ and $r$ ($l < r$), find the value of $f(s_ls_{l + 1}\ldots s_r)$.
一个字符串 t 如果至少存在一个长度为 k 的子串不是回文,则可以被称作是 k-good。 每次求一个串的所有 k-good 的和
e.g. 对于 s[1…..10],如果 k = 4,而且 s[1…4] 是回文串。也就是说 s[1] == s[4], s[2] == s[3]
假如后面也都是回文串,那么有 s[2] == s[5], s[3] == s[4] ……
不考虑自身的情况下:
如果一个串偶数位奇数位对应相等,那么对于所有 奇数 k 都是不好的
如果一个串每一位都相等,那么对于所有 k 都是不好的
如果一个串不是奇数位和偶数位都对应相等,那么 2 <= k <= len - 1 的奇数 k 也都是好的
#include<bits/stdc++.h> #define int long long using pii = std::pair<int, int>; using tuu = std::tuple<int, int, int>; #define pb push_back using std::cin, std::cout, std::string, std::vector; intread(int x = 0, int f = 0, char ch = getchar()) { while (ch < 48or57 < ch) f = ch == 45, ch = getchar(); while(48 <= ch and ch <= 57) x = x * 10 + ch - 48, ch = getchar(); return f ? -x : x; } constint N = 1e6 + 5; // const int INF = 1e18; int n, m, a[N]; std::vector<int> manacher(string s) { string t = "#"; for (auto ch : s) t += ch, t += '#'; int n = t.size(); vector<int> r(n); for (int i = 0, j = 0; i < n; i++) { if (2 * j - i >= 0and j + r[j] > i) r[i] = std::min(r[2 * j - i], j + r[j] - i); while (i - r[i] >= 0and i + r[i] < n and t[i - r[i]] == t[i + r[i]]) r[i]++; if (i + r[i] > j + r[j]) j = i; } return r; } voidsolve() { n = read(), m = read(); string s; cin >> s; vector<int> f1(n), f2(n); for (int i = n - 1; ~i; i--) f1[i] = i + 1 < n and s[i] == s[i + 1] ? f1[i + 1] : i, f2[i] = i + 2 < n and s[i] == s[i + 2] ? f2[i + 2] : i; auto rad = manacher(s); for (int l, r; m--; ) { l = read() - 1, r = read() - 1; int ans = 0, len = r - l + 1; if (f1[l] < r) { // 存在偶数 k int m = (len - 1) - (len - 1) % 2; ans += (m + 2) * (m / 2) / 2; } if (f2[l] + 1 < r or f2[l + 1] + 1 < r) { // 存在奇数 k int m = len - 1 - len % 2; ans += (m + 3) * ((m - 1) / 2) / 2; } if (rad[l + r + 1] < len) ans += len; cout << ans << '\n'; } }
signedmain() { #ifndef ONLINE_JUDGE freopen("D.in", "r", stdin); #endif for (int T = read(); T--; solve()); return0; }
#include<bits/stdc++.h> // #define int long long using pii = std::pair<int, int>; using tuu = std::tuple<int, int, int>; #define pb push_back using std::cin, std::cout, std::string, std::vector; intread(int x = 0, int f = 0, char ch = getchar()) { while (ch < 48or57 < ch) f = ch == 45, ch = getchar(); while(48 <= ch and ch <= 57) x = x * 10 + ch - 48, ch = getchar(); return f ? -x : x; } constint N = 1e6 + 5; // const int INF = 1e18; int n, m, a[N];
voidsolve() { n = read(); vector<vector<int>> g(n); for (int i = 1, u, v; i < n; i++) u = read() - 1, v = read() - 1, g[u].pb(v), g[v].pb(u); vector<int> fa(n); auto find = [&](int rt) { vector<int> dep(n, -1); fa[rt] = -1, dep[rt] = 0; std::queue<int> Q; Q.push(rt); for (int u; !Q.empty(); ) { u = Q.front(); Q.pop(); for (auto v : g[u]) if (dep[v] == -1) dep[v] = dep[u] + 1, Q.push(v), fa[v] = u; } return std::max_element(dep.begin(), dep.end()) - dep.begin(); }; int X = find(0); int Y = find(X); vector<int> a; for (int i = Y; i != -1; i = fa[i]) a.pb(i); vector<pii> ans; int d = a.size(); if (d & 1) { int p = a[d / 2]; for (int i = 0; i <= d / 2; i++) ans.pb({p, i}); } else { int p = a[d / 2], q = a[(d - 1) / 2]; for (int i = 1; i <= d / 2; i += 2) ans.pb({p, i}), ans.pb({q, i}); } cout << ans.size() << '\n'; for (auto [x, y] : ans) cout << x + 1 << ' ' << y << '\n'; }
signedmain() { #ifndef ONLINE_JUDGE freopen("D.in", "r", stdin); #endif for (int T = read(); T--; solve()); return0; }
#include<bits/stdc++.h> usingnamespace std; #define int long long using pii = std::pair<int, int>; using tuu = std::tuple<int, int, int>; #define pb push_back using std::cin, std::cout, std::string, std::vector; intread(int x = 0, int f = 0, char ch = getchar()) { while (ch < 48or57 < ch) f = ch == 45, ch = getchar(); while(48 <= ch and ch <= 57) x = x * 10 + ch - 48, ch = getchar(); return f ? -x : x; } constint N = 1e6 + 5; // const int INF = 1e18; int n, k, p; voidsolve() { n = read(), k = read(), p = read(); vector<vector<int>> f(k + 1, vector<int>(k + 1, 0)); f[0][0] = 1;
for (int i = 1; i <= n; i++) { vector<vector<int>> g(k + 1, vector<int>(k + 1, 0)); // g 先存恰好 c = b - a 的,最后再求和 for (int b = 0; b <= k; b++) { for (int a = 0; a <= k; a++) { int c = std::max(0LL, b - a); g[b][c] += f[a][b], g[b][c] %= p; } for (int j = 1; j <= k; j++) g[b][j] += g[b][j - 1], g[b][j] %= p; } f.swap(g); }
int ans = 0; for (int a = 0; a <= k; a++) for (int b = 0; b <= a; b++) ans += f[a][b], ans %= p; cout << ans << '\n'; }
signedmain() { #ifndef ONLINE_JUDGE freopen("D.in", "r", stdin); #endif for (int T = read(); T--; solve()); return0; }