跳到正文

题解

题解:UVA1482 Playing With Stones

chenwenmo约 3 分钟
原文
文章信息与版本

文章信息

发布
2025年6月23日
收录
2025年12月12日
更新
2026年11月9日
篇幅
约 3 分钟
获赞
1
题目
UVA1482

版本

  1. v1当前

这是一个有向图博弈,直接考虑 SG 函数。

先设最终状态,即 SG(0)=SG(1)=0\mathrm{SG}(0)=\mathrm{SG}(1)=0,

那么 SG(i)=mexj=1⌊i2⌋SG(i−j)\mathrm{SG}(i) = \mathrm{mex}_{j=1}^{\lfloor\frac{i}{2}\rfloor} \mathrm{SG}(i-j),

赛时可以通过暴力打表发现规律,也可以考虑数学证明。

结论:SG(x)={x2x mod 2=0SG(x−12)x mod 2=1\mathrm{SG}(x)=\begin{cases}\frac{x}{2} & x\bmod 2=0 \\ \mathrm{SG}(\frac{x-1}{2}) & x\bmod 2=1\end{cases}

证明:

当 x≤2x\le 2 时结论成立,

当 x>2x > 2 时,假设 ∀y<x\forall y<x,结论成立,

  • x mod 2=0x\bmod 2=0:SG(x)=mex{SG(x2),…,SG(x−1)}\mathrm{SG}(x)=\mathrm{mex}\{\mathrm{SG}(\frac{x}{2}),\dots,\mathrm{SG}(x-1)\},

    设 SS 为 xx 的转移点集合,

    • 对于 y∈S,y mod 2=0y\in S,y\bmod 2=0,SG(y)\mathrm{SG}(y) 覆盖 [⌈x4⌉,x2][\lceil\frac{x}{4}\rceil,\frac{x}{2}];

    • 对于 y∈S,y mod 2=1y\in S,y\bmod 2=1,发现这些 yy 对应的 y−12\frac{y-1}{2} 构成 zz 的转移点,其中 z={x2+1x2 mod 2=1x2x2 mod 2=0z=\begin{cases}\frac{x}{2}+1 & \frac{x}{2}\bmod 2=1 \\ \frac{x}{2} & \frac{x}{2}\bmod 2=0\end{cases},(特别的,如果 x2\frac{x}{2} 是奇数,那么 x2−12\frac{\frac{x}{2}-1}{2} 不是 zz 的转移点,那么 zz 会缺一个转移点 z−1z-1,但由于 SG(x2−12)=SG(z−1)\mathrm{SG}(\frac{\frac{x}{2}-1}{2})=\mathrm{SG}(z-1),因此不影响)。

    因此 x mod 2x\bmod 2 时结论成立。

  • x mod 2=1x\bmod 2=1:这时 xx 相对于 x−1x-1 的转移点来说,少了一个 x−12\frac{x-1}{2},多了一个 x−1x-1,因此 SG(x)=SG(x−12)\mathrm{SG}(x)=\mathrm{SG}(\frac{x-1}{2})。

综上所述,结论成立。

C++
#include <bits/stdc++.h>
using namespace std;
using LL = long long;

const int N = 1e2 + 5;

int n;

LL SG(LL a) {
    while (a & 1) {
        a = ((a - 1) >> 1);
    }
    return (a >> 1);
}

void solve() {
    cin >> n;
    LL a, sum = 0;
    for (int i = 1; i <= n; i++) {
        cin >> a;
        sum ^= SG(a);
    }
    cout << (sum ? "YES\n" : "NO\n");
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    int T;
    cin >> T;
    while (T--) {
        solve();
    }

    return 0;
}
原文