题解:AT_arc155_d [ARC155D] Avoid Coprime Game
Felix72
·
·
题解
设状态 f_{x, 0 / 1} = 0 / 1 表示当前全局 \gcd 为 x,且局面中存在的x 的倍数的个数是偶数或奇数是,先手的胜负性。
转移是枚举 y 满足 ky = x,并分别判断 f_{y, op_y} \to f_{x, op_x} 是否可以转移。判断的依据是是否存在 a_i 使得 \gcd(a_i, x) = y。我们要做的就是快速求函数 G(x, y) 表示 \gcd(a_i, x) = y 的 i 个数。
令 cnt_x = \sum_{i = 1}^{n} [a_i = x],常数 m = 2 \times 10^5。
G(x, y) = \sum_{i = 1}^{n}[\gcd(a_i, x) = y]
=\sum_{i = 1}^{\lfloor \frac{m}{y} \rfloor} [\gcd(\frac{x}{y}, i) = 1]cnt_{iy}
到这就能莫比乌斯反演,得
G(x, y) = \sum_{d | \frac{x}{y}} \mu(d) \sum_{d|i} cnt_{iy}
得到 $G$ 的 $O(1)$ 求法之后就可以转移了。
```cpp
/* 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;
}
/*
*/
```