跳到正文

题解

NOIP2020 T2

Enoch_xm约 9 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年2月28日
篇幅
约 9 分钟
题目
P7114

版本

  1. v1当前

今天模拟赛 vp NOIP2020,赛时想到一个很复杂度很神奇的做法。

约定:下文字符串使用 1-index\text{1-index},输入的字符串称作 ss,τ(n)\tau(n) 表示正整数 nn 的因数个数。

部分题解提到的暴力做法是 O(nn)O(n \sqrt{n}) 的,令 X=ABX = AB,也就是枚举所有可能的 (X)kC(X)^kC 形式,但这是完全不必要的,可以证明,这种形式的本质不同个数只有 O(nlog⁡n)O(n \log n) 个,巧妙地暴力枚举即可。

暴力做法具体是,对于每个串 ss 的前缀 s1∼is_{1 \sim i},求出其最小正周期 pip_i:

  • 如果 pi∤ip_i \nmid i 则 s1∼is_{1 \sim i} 没有整周期,只有 (X=s1∼i)C\left(X = s_{1 \sim i}\right)C 这一种划分。

  • 否则 pip_i 的因数都是可能的划分,可以得到 s1∼i=s1∼pi(ipi)s_{1 \sim i} = {s_{1 \sim p_i}}^{\left(\frac{i}{p_i}\right)},也就是 s1∼is_{1 \sim i} 由 (ipi)\left(\frac{i}{p_i}\right) 个最小周期 s1∼pis_{1 \sim p_i} 组成;

    将 (ipi)\left(\frac{i}{p_i}\right) 简称为 zz,对于 zz 的因数 dd,可以令 (X=xd)(zd)C\left(X = x^d\right)^{\left( \frac{z}{d} \right)}C,这样 XX 总共有 O(τ(z))O(\tau(z)) 种,其中 τ(n)\tau(n) 表示 nn 的因数个数。

这样,我们将问题转化成若干形如 (X)kC(X)^kC 的询问(准确的说是 O(nlog⁡n)O(n \log n) 个,下文会证明),求有多少划分满足 X=A+BX = A + B 且 F(A)≤F(C)F(A) \le F(C)。设 XX 长为 zz,我们枚举 XX 的前缀,统计 ∑i=1z−1[F(X1∼i)≤F(C)]\sum_{i = 1}^{z - 1} [F(X_{1 \sim i})\le F(C)] 就是答案(注意 i≠zi \neq z,因为 BB 不能为空)。

注意到 XX 一定是 ss 的前缀,也就是我们实际在查询 ∑i=1z−1[F(s1∼i)≤F(C)]\sum_{i = 1}^{z - 1} [F(s_{1 \sim i})\le F(C)],F(s1∼i)F(s_{1 \sim i}) 是好预处理的。同理也有 CC 是 ss 的后缀,F(si∼n)F(s_{i \sim n}) 也是好预处理的。不难发现 F(⋅)F(\cdot) 的值域是 [0,26][0, 26],定义 G(k,x)=∑i=1k[F(s1∼i)≤x]G(k, x) = \sum_{i = 1}^{k} [F(s_{1 \sim i})\le x]。我们只需要对 k∈[1,n]k \in [1, n],x∈[0,26]x \in [0, 26] 预处理即可,这部分的复杂度是 O(n∣Σ∣)O(n |\Sigma|)。

对于询问 (X)kC(X)^kC,其中 X=s1∼k1X = s_{1 \sim k_1},C=sk1∼nC = s_{k_1 \sim n},答案是 G(k1−1,F(sk1∼n))G(k_1 - 1, F(s_{k_1 \sim n})),可以 O(1)O(1) 回答。

于是乎,时间复杂度是

O ⁣(∑i=1n{τ(ipi),if pi∣i,1,otherwise.)O\!\left( \sum_{i=1}^{n} \begin{cases} \tau(\frac{i}{p_i}), & \text{if } p_i \mid i,\\ 1, & \text{otherwise.} \end{cases} \right)

这个我场上认为是 O(nτ(n))O(n \tau(n)) 的,但是却跑的很快。赛后才发现可以证明这是 O(nlog⁡n)O(n \log n) 的。

我们忽略 pi∤ip_i \nmid i 的情况,这部分最多 O(n)O(n)。剩下的所有 pip_i 都有 pi∣ip_i | i,可以得到 τ(ipi)≤τ(i)\tau \left(\frac{i}{p_i}\right) \le \tau(i),所以有

∑i=1nτ(ipi)≤∑i=1nτ(i)=O(nlog⁡n)\sum_{i = 1}^{n} \tau \left(\frac{i}{p_i}\right) \le \sum_{i = 1}^{n} \tau(i) = O(n \log n)

代码出乎意料的好写。

C++
#include <bits/stdc++.h>
// #pragma GCC optimize("Ofast")
using namespace std;
using uint = unsigned int; using ll = long long; using ull = unsigned long long;
using pii = pair<int, int>; using pll = pair<ll, ll>;
using ld = long double; using i128 = __int128; using u128 = unsigned __int128;
template<typename T, size_t N, size_t M> using matrix = array<array<T, M>, N>;
template<typename T1, typename T2> bool chkmin(T1& a, const T2& b) { return b < a ? (a = b, true) : false; }
template<typename T1, typename T2> bool chkmax(T1& a, const T2& b) { return b > a ? (a = b, true) : false; }
template<class... T> void debug(T&&... x) { cout << "(debug) "; ((cout << x << " "), ...); cout << "\n"; }
void fileIO(const string& x) { freopen((x + ".in").c_str(), "r", stdin), freopen((x + ".out").c_str(), "w", stdout); }
mt19937_64 gen(random_device{}());
ll test_num = 1, test_case = 1;
const int int_Inf = 1e9; const ll Inf = 1e18;

const ll N = (1 << 20) + 5;

int n,  cnt[26], pre[N], suf[N], bd[N], sum[N][27]; // sum_{p, x} := G(p, z)
string s;

vector<int> P[N];

void solution() {
	cin >> s; n = s.size(); s = '#' + s;
	fill(cnt, cnt + 26, 0);
	for(int i = 1; i <= n; i++) { // pre_i := F(s_{1 ~ i})
		pre[i] = 0, cnt[s[i] - 'a']++;
		for(int c = 0; c < 26; c++) pre[i] += (cnt[c] % 2 == 1);
		for(int x = 0; x <= 26; x++) sum[i][x] = sum[i - 1][x] + (pre[i] <= x);
	}
	fill(cnt, cnt + 26, 0);
	for(int i = n; i >= 1; i--) { // suf_i := F(s_{i ~ n})
		suf[i] = 0, cnt[s[i] - 'a']++;
		for(int c = 0; c < 26; c++) suf[i] += (cnt[c] % 2 == 1);
	}

	bd[0] = 0; // 求 border 长度
	for(int i = 1; i <= n; i++) {
		ll p = i - 1;
		while(p != 0 && s[bd[p] + 1] != s[i]) p = bd[p];
		bd[i] = p == 0 ? 0 : bd[p] + 1;
	}

	ll ans = 0;
	for(int i = 1; i < n; i++) {
		ll k = i - bd[i], z = i / k, x = suf[i + 1];
		// k: 最小周期长度
		// z: s_{1 ~ i} 由 z 个最小周期 s_{1 ~ k} 组成
		// x: F(C) = x
		if(i % k != 0) {
			ans += sum[i - 1][x];
		} else {
			for(auto d : P[z]) { // P[x] : 集合 {d : d | x}
				ans += sum[d * k - 1][x];
			}
		}
	}
	cout << ans << "\n";
}

void prework() { // 预处理因数集合
	for(int d = 1; d < N; d++) {
		for(int x = d; x < N; x += d) {
			P[x].emplace_back(d);
		}
	}
}

int main() {
	// fileIO("string");
	ios::sync_with_stdio(false), cin.tie(nullptr);
	prework();
	cin >> test_num;
	for(test_case = 1; test_case <= test_num; test_case++) solution();
	cerr << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
	return 0;
}

这份代码常数略大,std::vector 换成内存池才可以通过,但是代码的可读性较差,就不放在题解内了。优化版本参见 Link。

原文