题解:P3179 [HAOI2015] 数组游戏 / ARIS0_0 - 47

· · 题解

我假装你会 \text{SG} 函数。如果你不会,那就只用记住:

原游戏可以强化为加减而不是异或:白色格子权值为 1,黑色格子权值为 0,每次操作给选定的 x 号格权值减 1,选定的 2x, 3x, \dots, kx 号格权值加 1,其中选定的起始格权值大于 0。这样如果先手必胜,则总有权值为奇数的格可以翻。如果后手翻了权值为偶数的格,那么先手再重复一次后手的操作,这样在原游戏的视角上什么都没发生;如果后手翻了奇数格,就和原游戏的决策一模一样了。

这样之后,可以把新游戏拆分成多个子游戏,每个子游戏里只有一个位置是白色的。新游戏的 \text{SG} 值是所有子游戏的 \text{SG} 值的异或。

设 \text{SG}(x) 为整个序列只有 x 位置为白色的时候游戏的 \text{SG} 值。显然

\displaystyle \text{SG}(x) = \operatorname{mex}\left(\{0\} \cup \left\{\operatorname*{\oplus}_{j = 2}^{i} \text{SG}(jx) \mid i \in \mathbb{Z} \cap \left[2, \left\lfloor\dfrac{n}{x}\right\rfloor\right]\right\}\right)

这就是我们说的 \text{SG} 函数值是其后继所有状态的 \text{SG} 函数值的 \operatorname{mex}。由于后继状态也可以拆分为子游戏,所以构成了上面的公式。

注意到当 \left\lfloor\dfrac{n}{x_1}\right\rfloor = \left\lfloor\dfrac{n}{x_2}\right\rfloor 相等的时候,一定有 \text{SG}(x_1) = \text{SG}(x_2),因为 x_1 和 x_2 的后继格子数相同。也就是说,可以把它们都当成序列长度为 \left\lfloor\dfrac{n}{x}\right\rfloor,初始时只有第一个格子是白色的游戏。

所以递归 + 整除分块做这个事情。

复杂度的话,同学给我讲的时候说是杜教筛,但是我没学杜教筛啊那咋办。就当它是对的吧。

#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