跳到正文

题解

题解:AT_arc155_d [ARC155D] Avoid Coprime Game

Felix72约 5 分钟
原文
文章信息与版本

文章信息

发布
2025年6月24日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 5 分钟
获赞
3

版本

  1. v1当前

设状态 fx,0/1=0/1f_{x, 0 / 1} = 0 / 1 表示当前全局 gcd⁡\gcd 为 xx,且局面中存在的**xx 的倍数的个数**是偶数或奇数是,先手的胜负性。

转移是枚举 yy 满足 ky=xky = x,并分别判断 fy,opy→fx,opxf_{y, op_y} \to f_{x, op_x} 是否可以转移。判断的依据是是否存在 aia_i 使得 gcd⁡(ai,x)=y\gcd(a_i, x) = y。我们要做的就是快速求函数 G(x,y)G(x, y) 表示 gcd⁡(ai,x)=y\gcd(a_i, x) = y 的 ii 个数。

令 cntx=∑i=1n[ai=x]cnt_x = \sum_{i = 1}^{n} [a_i = x],常数 m=2×105m = 2 \times 10^5。

G(x,y)=∑i=1n[gcd⁡(ai,x)=y]G(x, y) = \sum_{i = 1}^{n}[\gcd(a_i, x) = y] =∑i=1⌊my⌋[gcd⁡(xy,i)=1]cntiy=\sum_{i = 1}^{\lfloor \frac{m}{y} \rfloor} [\gcd(\frac{x}{y}, i) = 1]cnt_{iy}

到这就能莫比乌斯反演,得

G(x,y)=∑d∣xyμ(d)∑d∣icntiyG(x, y) = \sum_{d | \frac{x}{y}} \mu(d) \sum_{d|i} cnt_{iy}

G(x,y)≠0G(x, y) \neq 0 的 (x,y)(x, y) 二元组数量最多 O(mlog⁡m)O(m\log m) 个,可以用这个式子预处理出来。

得到 GG 的 O(1)O(1) 求法之后就可以转移了。

C++
 /* NE_Cat 4.3 */
#include <bits/stdc++.h>
#define lowbit(x) ((x) & (-(x)))
using namespace std;

const int N = 200010;
int n, m = 200000, a[N], cnt[N], w[N]; bool f[N][2];
vector < int > fact[N], dp[N]; int cur[N], w_cur[N];
inline int Gcd(int x, int y) {return ((y == 0) ? x : Gcd(y, x % y));}

int pri[N], pn, mu[N]; bitset < N > vis;
inline void get_prime()
{
	mu[1] = 1;
	for(int i = 2; i <= m; ++i)
	{
		if(!vis[i]) pri[++pn] = i, mu[i] = -1;
		for(int j = 1; j <= pn && pri[j] * i <= m; ++j)
		{
			mu[pri[j] * i] = mu[i] * -1;
			vis[pri[j] * i] = true;
			if(i % pri[j] == 0)
			{
				mu[pri[j] * i] = 0;
				break;
			}
		}
	}
}

int main()
{
//	freopen("text.in", "r", stdin);
//	freopen("prog.out", "w", stdout);
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> n;
	for(int i = 1; i <= n; ++i) cin >> a[i], ++cnt[a[i]];
	for(int i = 1; i <= m; ++i)
		for(int j = i; j <= m; j += i)
			fact[j].push_back(i), w[i] += cnt[j];
	for(int i = 1; i <= m; ++i) dp[i].resize(m / i + 5);
	get_prime();
	for(int y = 1; y <= m; ++y)
	{
		int mx = m / y;
		for(int i = 1; i <= mx; ++i) cur[i] = cnt[i * y], w_cur[i] = 0;
		for(int i = 1; i <= mx; ++i)
			for(int j = i; j <= mx; j += i)
				w_cur[i] += cur[j];
		for(int i = 1; i <= mx; ++i)
			for(int d : fact[i])
				dp[y][i] += mu[d] * w_cur[d];
	}
	for(int i = 2; i <= m; ++i)
	{
		for(int op = 0; op <= 1; ++op)
		{
			if(op == 1 && !f[i][0]) f[i][op] = true;
			for(int j : fact[i])
			{
				if(j == i || j == 1) continue;
				if(!dp[j][i / j]) continue;
				if(!f[j][op ^ ((w[j] - w[i] - 1) % 2)]) f[i][op] = true;
			}
		}
	}
	for(int i = 1; i <= n; ++i)
	{
		if(!f[a[i]][(w[a[i]] - 1) % 2])
			cout << "Takahashi" << '\n';
		else cout << "Aoki" << '\n';
	}
	return 0;
}
/*

*/
原文