跳到正文

题解

题解:P16792 [蓝桥杯 2026 国 A] 不互质游戏

JustinRochester约 11 分钟
原文
文章信息与版本

文章信息

发布
2026年6月14日
收录
2026年6月16日
更新
2026年11月13日
篇幅
约 11 分钟
题目
P16792

版本

  1. v1当前

碎碎念

传送门

太神秘了,至今不知道是不是正解…

而且我严重怀疑不是正解。

分析

记 A=max⁡aiA=\max a_i

Task 1

显然,根据 Nim 博弈原理,若预处理每个数 xx 求出 SG 函数 sg(x)\text{sg}(x),则游戏的 SG 函数值为 ⊕i=1nsg(ai)\oplus_{i=1}^n \text{sg}(a_i),直接看这个结果是不是 0 就知道是否先手必胜了。

根据题意,sg(x)=mex{sg(y)∣y∈Z∧0≤y<x∧gcd⁡(y,x)>1}\text{sg}(x)=\text{mex}\{\text{sg}(y)\mid y\in \mathbb Z\wedge 0\leq y<x\wedge \gcd(y, x)>1\},其中 mex(S)\text{mex}(S) 表示集合 SS 中最小未出现的自然数。

根据上式暴力求解,应该可以在 O(A2log⁡A)O(A^2\log A) 时间内求解出 Task 1 的结果。


Task 2

由于若 gcd⁡(y,x)>1\gcd(y, x)>1,则 x,yx, y 必有公共质因子 pp。故考虑将所有小于 xx 的数 y=p1e1p2e2⋯pmemy=p_1^{e_1}p_2^{e_2}\cdots p_m^{e_m} 的 SG 函数都加入到其所有质因子 p1,p2,⋯ ,pmp_1, p_2, \cdots, p_m 的桶(集合) BpmB_{p_m} 中。

这可以通过先预处理每个数 xx 的质因子集合 pf(x)={p1,p2,⋯ ,pm}\text{pf}(x)=\{p_1, p_2, \cdots, p_m\},然后在求解 sg(x)\text{sg}(x) 后,在线地将 sg(x)\text{sg}(x) 插入到 xx 的所有质因子对应的桶中。

当需要计算 sg(x)\text{sg}(x) 时,只需要查询所有 pf(x)\text{pf}(x) 中质数所对应的桶,取并集即可得到 {sg(y)∣y∈Z∧0≤y<x∧gcd⁡(y,x)>1}\{\text{sg}(y)\mid y \in \mathbb Z\wedge 0\leq y<x\wedge \gcd(y, x)>1\}。

暴力枚举自然数,检查是否在并集中出现,即可求解 sg(x)\text{sg}(x)。

然而上述做法无法通过 Task 2。原因是合并集合较慢,而由于 sg(x)\text{sg}(x) 的值域过大(能到 O(A)O(A) 的规模),也不容易使用 bitset 优化。

但基于上述做法,输出 BpB_p 进行观察,可以观察到以下现象:

  1. sg(x)=0\text{sg}(x)=0 当且仅当 x=1x=1 或 xx 是质数(易证);
  2. 求解合数的 sg(x)\text{sg}(x) 时,几乎任意时刻,BpB_p 对应的集合都是一个足够长的前缀连续区间+后续若干个零散的小区间(因为 mex\text{mex} 操作会将集合中最小未出现的数字填补,只要序列足够长,总能把前面填补成一个足够长的前缀连续区间)。

所以可以每个桶 BpB_p 用从小到大的若干个区间 [l,r][l, r] 维护。将当前已有并集和一个新的桶合并时,可以类似于归并排序的方法合并两个有序区间的集合。

桶大小 #Bp\#B_p 显然是 O(x)O(x) 规模的,而每个 xx 需要合并的桶数为 #pf(x)\#\text{pf}(x) 个。合并后,直接查询 0 是否在桶内、第一个区间的右端点,即可直接知道 sg(x)\text{sg}(x)。

由于 2×3×5×7×11×13=210×143>200×100=2×1042\times 3\times 5\times 7\times 11\times 13=210\times 143>200\times 100=2\times 10^4,所以 #pf(x)≤5\#\text{pf}(x)\leq 5。

故总复杂度是 O(A2)O(A^2) 的,常数极小。需要保证有 O(A)O(A) 个 xx 对应的桶都是严格 O(x)O(x) 个数才能卡满。

而将 sg(x)\text{sg}(x) 更新到桶 BpB_p 中时,只需要查询左端点 ≤sg(x)\leq \text{sg}(x) 的最后一个区间 [l1,r1][l_1, r_1] 和左端点 >sg(x)>\text{sg}(x) 的第一个区间 [l2,r2][l_2, r_2](这俩必相邻,查一个就行)。

若 r1≥sg(x)r_1\geq \text{sg}(x) 说明这个数已出现在集合中,不必更新;若 r1=sg(x)−1r_1=\text{sg}(x)-1 则合并 [l1,r1][l_1, r_1] 与 sg(x)\text{sg}(x);若 l2=sg(x)+1l_2=\text{sg}(x)+1 则再合并 [l2,r2][l_2, r_2] 与 sg(x)\text{sg}(x)。

最坏情况下每次 O(log⁡#Bp)O(\log \#B_p) 的复杂度查询区间,然后删除两个区间,再插入一个新区间,故修改部分复杂度是 O(Alog⁡A)O(A\log A) 的。


Task 3

维护已有元素的集合存在一个问题:你必须将所有区间都遍历一遍,你才知道并集,才有办法知道 sg(x)\text{sg}(x)。

注:我后面发现,其实也可以每次遍历所有质因数,然后暴力合并左端点位于现有第一个连续区间内的所有区间,来获取第一个连续区间。应该和下文做法等价。

考虑反过来,一开始每个桶 BpB_p 维护 [0,+∞)[0, +\infty) 表示未用数所构成的区间集合。其他过程和上述全部对称(实际实现时,由于 sg(x)≤x≤n\text{sg}(x)\leq x\leq n,直接用 n+1n+1 代替 +∞+\infty)。

因此插入 sg(x)\text{sg}(x) 对应到在 BpB_p 中至多将一个区间破坏成两个区间,因此维护复杂度仍然是 O(Alog⁡A)O(A\log A)。

而查询时,sg(x)=min⁡⋂p∈pf(x)Bp\displaystyle \text{sg}(x)=\min \bigcap_{p\in \text{pf}(x)} B_p。

考虑一开始将所有 BpB_p 的第一个区间的左端点和右端点维护起来。若满足最大的左端点 ≤\leq 最小的右端点,则说明所有 BpB_p 的第一段区间交集非空,左端点最大值即是答案。这可以用 set 的 begin 和 rbegin 轻易实现。

若不满足上述条件,一定是因为右端点最小的那个区间所对应的 BpB_p,其内部的第一段区间太前面了,无法和别人交为非空。于是暴力将其从左右端点的 set 中删去,然后换下一个区间。

由于 2×3×5×7×11×13×17>200×100×15=3×1052\times 3\times 5\times 7\times 11\times 13\times 17>200\times 100\times 15=3\times 10^5,故每次都是从 #pf(x)≤6\#\text{pf}(x)\leq 6 个不同质因数的桶中跳转。

而由于 BpB_p 的每次更新都发生在 pp 的倍数处,且每次更新最多将 BpB_p 的某一个区间分成两个区间(即区间数+1+1);所以到 xx 时,BpB_p 中的区间数 ≤⌊xp⌋+1\leq \lfloor\frac x p\rfloor+1。

由于每次从 BpB_p 中选取下一个区间的操作类似于双指针(每个区间只会被取最多一次),故 xx 处的复杂度为 ∑p∈pf(x)⌊xp⌋+1\displaystyle \sum_{p\in \text{pf}(x)}\lfloor\frac{x}{p}\rfloor+1。

因此总复杂度为:

T(A)=∑x=1A∑p∈pf(x)(⌊xp⌋+1)=∑x=1A∑p∈pf(x)⌊xp⌋+∑x=1A#pf(x)≤∑p∈PA∑x=1A⌊xp⌋+6A\displaystyle T(A)=\sum_{x=1}^A\sum_{p\in \text{pf}(x)}(\lfloor\frac{x}{p}\rfloor+1)=\sum_{x=1}^A\sum_{p\in \text{pf}(x)}\lfloor\frac{x}{p}\rfloor+\sum_{x=1}^A\#\text{pf}(x)\leq \sum_{p\in \mathbb P}^A\sum_{x=1}^A\lfloor\frac x p\rfloor+6A。

前面那个东西就是埃氏筛的复杂度,所以 T(A)=O(Alog⁡log⁡A+6A)=O(Alog⁡log⁡A)T(A)=O(A\log \log A+6A)=O(A\log\log A)。

不知道是因为这常数真的有点大,还是我写法复杂度不太对,总之我直接这么做没过。

考虑到这个做法仍然存在局限性,当某个 BpB_p 已被挑选的数字过于零散时,其内部的区间会非常零散;若另一个 Bp′B_{p'} 是一个超长的区间,则会出现多次重复从 BpB_p 中选择后继区间的操作。

故将本来的选取下一个区间的操作,直接改为去 BpB_p 中二分第一个右端点 ≥\geq 剩余最大左端点的区间,然后直接从原区间一步跳转到当前区间。这样,可以保证每次操作的都是不同的 pp 所对应的桶。


代码

C++
#include <bits/stdc++.h>
using namespace std;
vector<int> sg;
void init() {
	const int n = 300000;
	sg.resize(n+1);
	vector<set<pair<int, int> > > us(n+1);
	vector<vector<int> > divs(n+1);
	bitset<n+1> nprime;
	for(int i=2; i<=n; ++i) {
		if(nprime[i]) continue;
		for(int j=i+i; j<=n; j+=i) {
			divs[j].push_back(i);
			nprime[j]=1;
		}
		divs[i].push_back(i);
		us[i].emplace(0, n+1);
	}
	for(int i=2; i<=n; ++i) {
		if(!nprime[i])
			sg[i]=0;
		else {
			vector<tuple<int, int, int> > lr;
			set<pair<int, int> > ls, rs;
			for(const auto &p : divs[i]) {
				auto iter = us[p].begin();
				ls.emplace(iter->first, lr.size());
				rs.emplace(iter->second, lr.size());
				lr.emplace_back(iter->first, iter->second, p);
			}
			while(ls.rbegin()->first > rs.begin()->first) {
				int idx = rs.begin()->second;
				auto [l, r, p] = lr[idx];
				ls.erase(ls.find(make_pair(l, idx)));
				rs.erase(rs.find(make_pair(r, idx)));
				auto iter = us[p].upper_bound(make_pair(ls.rbegin()->first, n+1));
				--iter;
				if(iter->second < ls.rbegin()->first)
					++iter;
				ls.emplace(iter->first, idx);
				rs.emplace(iter->second, idx);
				lr[idx]=make_tuple(iter->first, iter->second, p);
			}
			sg[i]=ls.rbegin()->first;
		}
		for(const auto &p : divs[i]) {
			auto &now = us[p];
			auto iter = now.upper_bound(make_pair(sg[i], n+1));
			if(iter == now.begin())
				continue;
			--iter;
			auto [l, r] = *iter;
			if(r<sg[i]) continue;
			now.erase(iter);
			if(l!=sg[i]) now.emplace(l, sg[i]-1);
			if(r!=sg[i]) now.emplace(sg[i]+1, r);
		}
	}
}
int main() {
	init();
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int T; cin>>T;
	while(T--) {
		int n; cin>>n;
		int res=0;
		for(int i=1; i<=n; ++i) {
			int v; cin>>v;
			res^=sg[v];
		}
		if(res!=0) cout<<"Yes\n";
		else cout<<"No\n";
	}
	return 0;
}
原文