题解:P3179 [HAOI2015] 数组游戏 / ARIS0_0 - 47
SentencedFate · · 题解
我假装你会
- 一个游戏的
\text{SG} 函数值是其后继所有状态的\text{SG} 函数值的\operatorname{mex} 。 - 如果一个游戏可以拆分为多个子游戏,那么其
\text{SG} 函数值是其所有子游戏的\text{SG} 函数值的异或。 - 如果一个游戏的
\text{SG} 函数值不是0 ,那么先手必胜;否则后手必胜。
原游戏可以强化为加减而不是异或:白色格子权值为
这样之后,可以把新游戏拆分成多个子游戏,每个子游戏里只有一个位置是白色的。新游戏的
设
这就是我们说的
注意到当
所以递归 + 整除分块做这个事情。
复杂度的话,同学给我讲的时候说是杜教筛,但是我没学杜教筛啊那咋办。就当它是对的吧。
#include <cstdio>
#include <vector>
#include <unordered_map>
namespace FastIn
{
const int S = 4e6 + 7;
char *p1, *p2, buf[S];
#define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, S, stdin), p1 == p2) ? EOF : *p1++)
inline void read(int &x)
{
x = 0;
char c = gc();
while (c < '0' || c > '9')
c = gc();
for (; c >= '0' && c <= '9'; c = gc())
x = (x << 3) + (x << 1) + (c & 15);
}
#undef gc
}
using FastIn::read;
std::unordered_map<int, int> mp;
int n, m;
int SG(const int &x) // 注意这里 SG 的意义有不同:SG(x) 表示长度为 x 的序列,初始时只有第一个是白的
{
if (mp.count(x))
return mp[x];
if (x == 1)
return 1;
std::vector<bool> c(91);
int cur = 0, res = 1; c[0] = true;
auto add = [&](const int &v) -> void
{
c[v] = true;
while (c[res])
++res;
};
for (int l = 1, r = 1; l <= x && r <= x; l = r + 1)
if (r = (x / (x / l)), (r - l + 1) & 1)
l != 1 && (add(cur ^= SG(x / l)), true);
else
l != 1 && (add(cur ^ SG(x / l)), true);
return mp[x] = res;
}
int main()
{
read(n), read(m);
for (int t = 1, k, ans; t <= m; ++t)
{
read(k), ans = 0;
for (int i = 1, x; i <= k; ++i)
read(x), ans ^= SG(n / x);
if (ans)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
// 尘埃掩盖了背叛,落日血染了樱花
// 47