[AGC001D] Arrays and Palindrome
不会构造题/ll
回文串的限制意味着有一些位置是必须相等的,可以看作连边,那么全部相等即意味着图连通。
连通至少需要 条边,而长度为 的回文限制可以提供 条边,可以发现若奇数个数 个一定不合法。
现在考虑构造,可以发现 是偶数时,要求 和 回文即可使得 连通,因此想到先把奇数放到两边,然后构造 即可。
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, m, t, a[N];
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; i++) cin >> a[i], t += a[i] & 1;
if (t > 2) return cout << "Impossible", 0; t = 0;
for (int i = 1; i <= m; i++) if (a[i] & 1) swap(a[t++ ? m : 1], a[i]);
for (int i = 1; i <= m; i++) cout << a[i] << ' '; cout << '\n';
if (m == 1) { if (n == 1) cout << "1\n1"; else cout << "2\n1 " << n - 1; return 0; }
cout << m - (a[m] == 1) << '\n';
for (int i = 1; i <= m; i++) {
if (i == 1) cout << a[i] + 1 << ' ';
else if (i < m) cout << a[i] << ' ';
else if (a[i] > 1) cout << a[i] - 1 << ' ';
}
}[AGC001E] BBQ Hard
化一下问题,现在要求:
直接 GF:
考虑怎么求括号里那一坨玩意,直接 做是不可行的,考虑转为枚举 :
设后面那一坨为 ,暴力乘是 或者 的,不过看到形如 的形式,可以使用秦九韶算法,也即化为 ,复杂度优化至 。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5, M = 8e3 + 5, D = 4e3, p = 1e9 + 7;
int n, a[N], b[N], f[M], fac[M], inv[M], ans;
basic_string<int> G[M];
int qpow(int a, int b) { int c = 1; while (b) { if (b & 1) c = (ll)c * a % p; a = (ll)a * a % p, b >>= 1; } return c; }
int C(int n, int m) { return n < m ? 0 : (ll)fac[n] * inv[n - m] % p * inv[m] % p; }
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0), cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i] >> b[i], G[a[i] + b[i]] += D - a[i];
for (int i = D; i; i--) {
for (int j : G[i]) f[j]++;
for (int j = M - 1; j; j--) f[j] += f[j - 1], f[j] -= p & -(f[j] >= p);
}
for (int i = -D; i <= D; i++) ans = (ans + (ll)f[D - i] * f[D + i]) % p;
fac[0] = 1; for (int i = 1; i < M; i++) fac[i] = (ll)fac[i - 1] * i % p;
inv[M - 1] = qpow(fac[M - 1], p - 2); for (int i = M - 2; ~i; i--) inv[i] = (ll)inv[i + 1] * (i + 1) % p;
for (int i = 1; i <= n; i++) ans = (ans - C(a[i] + b[i] << 1, a[i] << 1) + p) % p;
cout << (ll)ans * (p + 1 >> 1) % p;
}[AGC001F] Wide Swap
这个限制一看就是要从逆置换 上考虑,也就是说对于 ,如果 就能交换 ,目标是较小的 尽量靠前。
有一个调整策略:每次选择一个 满足 并交换 ,可以发现交换之后一定会变得更优,感性理解一下,这样一定能得到最优解,证明等我头不晕了再补 qwq。
这样做冒泡就是 的,考虑使用归并排序优化,对于 及 , 能够交换到 的位置上,如果 内的每个数都 ,发现 交换到前面之后后面的数会往右平移一个位置,可以直接预处理 表示 的最小值,这样把 放到前面后仍能直接沿用 的值。
时间复杂度为 。
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
int n, k, p[N], a[N], suf[N], b[N];
void merge(int l, int r) {
if (l == r) return; int mid = l + r >> 1;
merge(l, mid), merge(mid + 1, r), suf[mid] = a[mid];
for (int i = mid - 1; i >= l; i--) suf[i] = min(suf[i + 1], a[i]);
int i = l, j = mid + 1, t = 0;
while (i <= mid && j <= r) b[++t] = a[suf[i] - k >= a[j] ? j++ : i++];
while (i <= mid) b[++t] = a[i++]; while (j <= r) b[++t] = a[j++];
for (int i = l; i <= r; i++) a[i] = b[i - l + 1];
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> k;
for (int i = 1, x; i <= n; i++) cin >> x, a[x] = i;
merge(1, n);
for (int i = 1; i <= n; i++) b[a[i]] = i;
for (int i = 1; i <= n; i++) cout << b[i] << '\n';
}[AGC002D] Stamp Rally
建出 Kruskal 重构树,这样一个点 经过编号 的边能到达的点的数量即为其祖先链上最浅的对应边编号 的祖先的子树内原图点(节点编号 )的个数。询问显然可以二分,找祖先使用倍增,时间复杂度为 。
这题还能够 ?不会。
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, m, q, t, Fa[N], id[N], siz[N], fa[N][18];
basic_string<int> G[N];
int get(int x) { return x == Fa[x] ? x : Fa[x] = get(Fa[x]); }
void dfs(int u) { for (int v : G[u]) dfs(v), siz[u] += siz[v]; }
int find(int x, int y, int z) {
for (int i = 17; ~i; i--) x = id[fa[x][i]] <= z ? fa[x][i] : x, y = id[fa[y][i]] <= z ? fa[y][i] : y;
return siz[x] + (x == y ? 0 : siz[y]);
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m, t = n, id[0] = 1e9;
iota(Fa + 1, Fa + 1 + (n << 1), 1), fill(siz + 1, siz + 1 + n, 1);
for (int i = 1, u, v; i <= m; i++) {
cin >> u >> v, u = get(u), v = get(v);
if (u != v) id[++t] = i, Fa[u] = Fa[v] = fa[u][0] = fa[v][0] = t, G[t] += u, G[t] += v;
}
dfs(t);
for (int i = 1; i < 18; i++) for (int j = 1; j <= t; j++) fa[j][i] = fa[fa[j][i - 1]][i - 1];
cin >> q;
for (int i = 1, u, v, w; i <= q; i++) {
cin >> u >> v >> w;
int L = 1, R = m, mid, ans = -1;
while (L <= R) mid = L + R >> 1, find(u, v, mid) >= w ? (ans = mid, R = mid - 1) : L = mid + 1;
cout << ans << '\n';
}
}[AGC002E] Candy Piles
没想到可以转化成网格走路,输。
先把 从大到小排序,然后画成点阵,这样操作就相当于每次删去最左边一列或者最下边一行。考虑最左下角点在原图的位置,发现本质上是在一个杨图上网格走路,走到边界就输了:
发现同一个对角线上的点的状态是相同的,为了判定原点是必胜态还是必败态,找到原点对应的对角线上毗邻边界的点:
显然若该点到上/左边界的距离至少有一个奇数则必败,否则必胜。
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, a[N], x = 1, d;
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n; for (int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + 1 + n, greater<int>());
while (x <= a[x]) x++; x--;
while (a[x + d + 1] >= x) d++;
cout << ((a[x] - x & 1) || (d & 1) ? "First" : "Second");
}[AGC002F] Leftmost Ball
一个序列合法的充要条件为恰有 个白球,其余 种彩球每种各 个,且前缀白球数 前缀颜色数。
考虑 dp,往 个位置里放球,设 表示放了 个白球以及 种彩球的剩余 个的方案数。
可以选择放一个白球,为防止算重,强制让该白球放在从左往右第一个空位上,贡献为 。
也可以选择放一种颜色的 个球,即从 转移过来,先从剩余的 种颜色中选择一种,再强制让其中一个球放在从左往右第一个空位上,方案数为 。
注意特判 。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2005, M = N * N, p = 1e9 + 7;
int n, k, fac[M], inv[M], f[N][N];
int qpow(int a, int b) { int c = 1; while (b) { if (b & 1) c = (ll)c * a % p; a = (ll)a * a % p, b >>= 1; } return c; }
int C(int n, int m) { return n < 0 ? 0 : (ll)fac[n] * inv[n - m] % p * inv[m] % p; }
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> k; if (k == 1) return cout << 1, 0; int mx = n * k;
fac[0] = 1; for (int i = 1; i <= mx; i++) fac[i] = (ll)fac[i - 1] * i % p;
inv[mx] = qpow(fac[mx], p - 2); for (int i = mx - 1; ~i; i--) inv[i] = (ll)inv[i + 1] * (i + 1) % p;
f[0][0] = 1;
for (int i = 1; i <= n; i++) {
f[i][0] = 1;
for (int j = 1; j <= i; j++) f[i][j] = (f[i - 1][j] + (ll)f[i][j - 1] * (n - j + 1) % p * C(n * k - i - (j - 1) * (k - 1) - 1, k - 2)) % p;
}
cout << f[n][n];
}[AGC003D] Anticube
弱智题。
把每个数分解素因数之后把每个素因子的指数按 取模,这样不能同时选的点就是一对一对的,选比较多的那一个即可,注意 的情况。
分解素因子使用 Pollard-,时间复杂度为 。
std 居然是 的,魔如怔。
#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef __int128 lll;
const int base[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
int n, t, tt, cff[60], ans; ll f[60], ff[60];
gp_hash_table<ll, int> cnt; gp_hash_table<ll, ll> qwq;
ll qpow(ll a, ll b, const ll p) { ll c = 1; while (b) { if (b & 1) c = (lll)c * a % p; a = (lll)a * a % p, b >>= 1; } return c; }
bool millerRabin(const ll n) {
if (n < 3 || !(n & 1)) return n == 2;
ll u = n - 1, t = 0; while (!(u & 1)) u >>= 1, t++;
for (ll a : base) {
ll v = qpow(a, u, n);
if (v == 1 || v == n - 1 || !v) continue;
for (int i = 1; i <= t; i++) {
v = (lll)v * v % n;
if (v == n - 1 && i != t) { v = 1; break; }
if (v == 1) return 0;
}
if (v != 1) return 0;
}
return 1;
}
ll pollardRho(const ll n) {
ll s = 0, t = 0, c = rand() % (n - 1) + 1, w = 1, d;
for (int v = 1; ; v <<= 1, s = t, w = 1) {
for (int u = 1; u <= v; u++) {
t = ((lll)t * t + c) % n, w = (lll)w * abs(t - s) % n;
if (!(u & 127)) { d = __gcd(w, n); if (d > 1) return d; }
}
d = __gcd(w, n); if (d > 1) return d;
}
}
void solve(ll n) {
if (n < 2) return;
if (millerRabin(n)) return f[++t] = n, void();
ll p = n; while (p >= n) p = pollardRho(n);
solve(p), solve(n / p);
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for (ll _ = 1, a; _ <= n; _++) {
cin >> a, t = tt = 0, solve(a), sort(f + 1, f + 1 + t);
for (int i = 1; i <= t; i++) f[i] == f[i - 1] ? cff[tt]++ : (ff[++tt] = f[i], cff[tt] = 1);
ll b = 1, c = 1;
for (int i = 1; i <= tt; i++) {
int x = cff[i] % 3;
if (x) (x == 1 ? b : c) *= ff[i], (x == 1 ? c : b) *= ff[i] * ff[i];
}
cnt[b]++, qwq[b] = c;
}
for (auto [x, y] : cnt) {
if (x == 1) { ans++; continue; }
ll z = qwq[x];
if (cnt.find(z) == cnt.end()) ans += y;
else {
int w = cnt[z];
if (w < y || w == y && x < z) ans += y;
}
}
cout << ans;
}[AGC004C] AND Grid
简单构造题。
由于边缘没有墙,对于要构造的两个矩阵 ,需要用互不相同的额外的墙使得给出的墙连通,让 的第一列和 的最后一列全为墙,同时对于奇数行 , 为墙,对于偶数行 , 为墙即可,容易发现这是合法的。
#include <bits/stdc++.h>
using namespace std;
const int N = 505;
int n, m; char a[N][N], b[N][N];
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%s", a[i] + 1), memcpy(b[i] + 1, a[i] + 1, m);
for (int i = 1; i <= n; i++) {
a[i][1] = b[i][m] = '#';
for (int j = 2; j < m; j++) (i & 1 ? a : b)[i][j] = '#';
}
for (int i = 1; i <= n; i++) puts(a[i] + 1); puts("");
for (int i = 1; i <= n; i++) puts(b[i] + 1);
}[AGC004D] Teleporter
用脚贪。
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, k, a[N], s, h[N];
basic_string<int> G[N];
void dfs(int u, int fa) {
h[u] = 1;
for (int v : G[u]) if (v != fa) dfs(v, u), h[u] = max(h[u], h[v] + 1);
if (a[u] != 1 && h[u] == k) h[u] = 0, s++;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
if (a[1] != 1) a[1] = 1, s++;
for (int i = 2; i <= n; i++) G[a[i]] += i, G[i] += a[i];
dfs(1, 0), cout << s;
}[AGC009B] Tournament
比较好想的树形 dp。
输者向打败他的人连边,形成了一棵有根树。设 表示 要赢所需的最小场数。 可以以任意顺序击败它的儿子,当加入一个儿子 时有:
贪心,将所有儿子按照 从小到大排序即可。
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, f[N];
vector<int> G[N];
void dfs(int u) {
for (int v : G[u]) dfs(v);
sort(G[u].begin(), G[u].end(), [](int x, int y) { return f[x] < f[y]; });
for (int v : G[u]) f[u] = max(f[u], f[v]) + 1;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 2, f; i <= n; i++) cin >> f, G[f].emplace_back(i);
dfs(1), cout << f[1];
}[AGC009C] Division into Two
假设 ,先把无解的情况判掉:由于相邻三个元素中必定有至少两个在同一集合中,若 必定无解。
设 表示考虑了 这段前缀,且 是 中元素的方案数,对于上一个 中元素 ,其合法当且仅当 ,且 且 。第三个条件已经在上面判掉了,而符合前两个条件的一定是一段区间,可以直接双指针维护,时间复杂度为 。
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 5, p = 1e9 + 7, INF = 2e18;
int n, A, B, a[N], s, f[N];
signed main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> A >> B; if (A > B) swap(A, B);
a[0] = -INF, a[n + 1] = INF, f[0] = 1;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) if (a[i + 1] - a[i - 1] < A) return cout << 0, 0;
for (int i = 1, j = 0; i <= n + 1; i++) {
while (a[i] - a[j] >= B) s = (s + f[j]) % p, j++;
f[i] = s; if (a[i] - a[i - 1] < A) s = 0, j = i - 1;
}
cout << f[n + 1];
}[AGC011C] Squared Graph
考虑新图上 和 连通的充要条件。对于新图上的一条边 ,其意味这原图上存在边 ,即 和 分别走了一条边到达 和 。因此, 能走到 ,当且仅当存在一对 到 和 到 的长度相等的路径。
发现一条边可以反复走,因此只要存在一对 到 和 到 的奇偶性相等的路径即可。而如果一个连通块内有奇环,则该连通块内任意两点间同时存在长度为奇数的路径和长为偶数的路径;如果没有奇环,即二分图,那么将该连通块黑白染色,同色点之间只有长为偶数的路径,异色点之间只有长为奇数的路径。
因此将连通块分为三类:孤立点、二分图和非二分图,设其个数分别为 。对于孤立点,只要它存在在某一维中就一定会在新图上产生一个孤立点,因此共会产生 个连通块;对于非二分图与另一个非孤立点的连通块,其中每个点对都是连通的,因此有 个连通块;而一对二分图之间,相同颜色和不同颜色的点对会分别构成一个连通块,因此有 个连通块。综上所述,答案为 。
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 5;
int n, m, a, b, c, col[N], cnt, flg;
basic_string<int> G[N];
void dfs(int u, int f) {
cnt++, col[u] = f;
for (int v : G[u]) if (!~col[v]) dfs(v, f ^ 1); else if (col[u] == col[v]) flg = 1;
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m, memset(col, -1, sizeof(col));
for (int i = 1, u, v; i <= m; i++) cin >> u >> v, G[u] += v, G[v] += u;
for (int i = 1; i <= n; i++) if (!~col[i]) cnt = flg = 0, dfs(i, 0), (cnt == 1 ? a : flg ? c : b)++;
cout << 2 * a * n - a * a + 2 * b * c + c * c + 2 * b * b;
}[AGC012B] Splatter Painting
水题,不说了。
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, m, q, c[N], a[N][11];
vector<int> G[N];
int main() {
scanf("%d%d", &n, &m);
for (int i = 1, u, v; i <= m; i++) {
scanf("%d%d", &u, &v);
G[u].emplace_back(v), G[v].emplace_back(u);
}
scanf("%d", &q);
int v, d;
for (int i = 1; i <= q; i++) {
scanf("%d%d%d", &v, &d, &c[i]);
a[v][d] = i;
}
for (int d = 10; d; d--) {
for (int i = 1; i <= n; i++) {
for (int v : G[i]) a[v][d - 1] = max(a[v][d - 1], a[i][d]);
}
}
for (int i = 1, t; i <= n; i++) {
t = 0;
for (int j = 0; j <= 10; j++) t = max(t, a[i][j]);
printf("%d\n", c[t]);
}
}[AGC012C] Tautonym Puzzle & Its SPJ
构造题,非常的困难!
既然是子序列,想到把序列拆成两半,前面 个是 的逆序排列,后面是一个长度为 的排列 ,这样 中的每个下降子序列都会产生 的贡献。
现在问题转化成求一个排列 ,其下降子序列个数恰好为 。先把 加 (空串),考虑从小到大插入元素,放在前面方案数 ,放在后面方案数 ,那就递归,如果 是偶数就放后面,否则放前面。
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, m; basic_string<int> a, b;
signed main() {
cin >> n, n++;
while (n > 1) n & 1 ? (a += ++m, n -= 1) : (b += ++m, n >>= 1);
cout << (m << 1) << '\n';
for (int i = m; i >= 1; i--) cout << i << ' ';
for (int i = 0; i < a.size(); i++) cout << a[i] << ' ';
for (int i = b.size() - 1; ~i; i--) cout << b[i] << ' ';
}再考虑这个题的 SPJ 怎么写,也就是求形如 的子序列数。枚举左侧最后一个字符的位置 ,设 表示左侧最后一个字符到了 ,右侧最后一个字符到了 的方案数,可以做到 。
发现可以对合法的转移使用前缀和优化,时间复杂度为 。
[AGC013C] Ants on a Circle
先考虑链怎么做,这样弹来弹去等价于方向不变但是交换编号,并且在每个时刻编号的相对顺序不变。
现在是一个环了,发现当一个人逆时针穿过原点时所有人 rank 会减 ,顺时针穿过时 rank 会加 (均为模 意义下)。
那么我们算出 时刻哪些位置有蚂蚁以及 rank 的偏移量,最后对应下来即可。
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 5;
int n, l, t, c, p[N];
signed main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> l >> t;
for (int i = 0, x, w; i < n; i++) {
cin >> x >> w, x += w == 1 ? t : -t;
p[i] = (x % l + l) % l, c = (c + (int)floor((double)x / l) + n) % n;
}
sort(p, p + n);
for (int i = 0; i < n; i++) cout << p[(i + c) % n] << '\n';
}

