跳到正文

题解

题解:P6639 「JYLOI Round 1」让

lzm2025约 6 分钟
原文
文章信息与版本

文章信息

发布
2026年5月4日
收录
2026年5月4日
更新
2026年11月11日
篇幅
约 6 分钟
获赞
2
题目
P6639

版本

  1. v2当前
  2. v1

题意简述

定义一种游戏如下:给定常数 RR。初始有 kk 堆石子,第 ii 堆有 pip_i 个石子。双方轮流操作,每次可以选一堆有 xx 个石子的堆取 yy 个且满足 x+y≤Rx + y \le R。

要求支持动态修改 RR。每次询问给出 nn 对数 (li,ri)(l_i,r_i) 表示有 li,li+1,…,ril_i,l_i+1,\dots,r_i 这 ri−li+1r_i - l_i + 1 堆石子,问先手是否有必胜策略。

思路

考虑一堆的 SG 函数 SG(x)SG(x),根据定义有:

SG(x)=mex⁡1≤i≤x,i+x≤RSG(x−i)SG(x) = \operatorname{mex}_{1 \le i \le x,i + x \le R} SG(x-i)

设 Si={i−j∣1≤j≤i,i+j≤R}={j∣max⁡{0,2i−R}≤j<i}S_i = \{i - j | 1 \le j \le i,i + j \le R\} = \{j|\max\{0,2i-R\} \le j < i\}。

首先,我们先证明,∀i∈[0,⌊R2⌋]\forall i \in [0,\lfloor \frac{R}{2} \rfloor],SG(i)=iSG(i) = i。

证明

SG(0)=0SG(0) = 0 显然成立。

假设 SG(i)=iSG(i) = i 对 [0,n][0,n] 成立,则根据 n+1≤⌊R2⌋n + 1 \le \lfloor \frac{R}{2} \rfloor 有 Sn+1=[0,n]S_{n+1} = [0,n]。根据公式:

SG(n+1)=mex⁡0≤i≤ni=n+1\begin{aligned} SG(n+1) &= \operatorname{mex}_{0 \le i \le n} i \\ &= n + 1 \end{aligned}

根据数学归纳法得证。

设 ziz_i 表示从小到大第 ii 个数满足 SG(zi)=0SG(z_i) = 0。显然有 z1=0z_1 = 0。

由于 R−(⌊R2⌋+1)>⌊R2⌋R - (\lfloor \frac{R}{2} \rfloor + 1) > \lfloor \frac{R}{2} \rfloor,故 SG(⌊R2⌋+1)=0SG(\lfloor \frac{R}{2} \rfloor + 1) = 0,z2=⌊R2⌋+1z_2 = \lfloor \frac{R}{2} \rfloor + 1。

再往后,考虑 SxS_x 和 Sx+1S_{x+1} 的区别。发现只有最前面少了两个数,最后面多了一个数。如果少的数还没有包含 00,由于少的数较大,不会对 mex⁡\operatorname{mex} 产生影响,故最后多的数会使答案加 11。

如果少的数包含 00,后面的部分依次递增,根据 mex⁡\operatorname{mex} 的定义有 SG(x+1)=0SG(x+1) = 0。此时 zi=x+1z_i = x+1 满足 SG(2x−R)=0SG(2x - R) = 0 或 SG(2x−R+1)=0SG(2x - R + 1) = 0。我们就得到了递推式 zi=⌊zi−1+R2⌋+1z_i = \lfloor \frac {z_{i-1}+R}{2} \rfloor + 1。

考虑单个区间,我们要求 xor⁡i∈[l,r]SG(i)=(xor⁡i∈[0,l−1]SG(i))xor⁡(xor⁡i∈[0,r]SG(i))\operatorname{xor}_{i \in [l,r]} SG(i) = (\operatorname{xor}_{i \in [0,l-1]} SG(i)) \operatorname{xor} (\operatorname{xor}_{i \in [0,r]} SG(i))。由于 SG 函数每一段都是 0∼m0 \sim m 的形式,这一段异或起来就是 xor⁡i∈[0,m]i\operatorname{xor}_{i \in [0,m]}i。把所有段异或起来就得到了最终答案。

现在只需要考虑 xor⁡i∈[0,x]i\operatorname{xor}_{i \in [0,x]}i。由于 (4x)xor⁡(4x+1)xor⁡(4x+2)xor⁡(4x+3)=0(4x) \operatorname{xor} (4x+1) \operatorname{xor} (4x+2) \operatorname{xor} (4x+3) = 0,考虑以 44 为循环计算。故有:

xor⁡i∈[0,x]i={xx mod 4=0(x−1)xor⁡x=1x mod 4=11xor⁡xx mod 4=20x mod 4=3\operatorname{xor}_{i \in [0,x]}i = \begin{cases} x & x \bmod 4 = 0 \\ (x-1) \operatorname{xor} x = 1 & x \bmod 4 = 1 \\ 1 \operatorname{xor} x & x \bmod 4 = 2 \\ 0 & x \bmod 4 = 3\end{cases}

最后把所有的区间的 SG 异或值异或起来得到最终的 SG 值,如果不是 00 先手胜,否则后手胜。

因为 ziz_i 能比 zi−1z_{i-1} 逼近 RR 一半,故使用递推式枚举 ziz_i 的复杂度为 O(log⁡R)O(\log R)。区间异或和可以使用上述公式 O(1)O(1) 计算。总时间复杂度是 O(∑nlog⁡R)O(\sum n \log R),空间复杂度 O(1)O(1)。

如果预计算所有 ziz_i 应该可以做到时间复杂度 O(∑log⁡R+∑nlog⁡log⁡R)O(\sum \log R + \sum n \log \log R),空间复杂度 O(log⁡R)O(\log R)。不过我懒。

代码

实现起来并不难。

C++
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int t, n;
string op;
ll r, s, e, ans;
ll xorsum(ll x) { // 异或前缀和
  switch (x & 3) {
  case 0:
    return x;
  case 1:
    return 1;
  case 2:
    return x | 1;
  case 3:
    return 0;
  }
}
inline ll sumSG(ll n) { // SG 函数异或和
  ll res = 0, cur = 0, nxt; // 答案,z_i,z_{i+1}
  while (cur <= n) {
    nxt = ((cur + r) >> 1) + 1;
    if (nxt > n) { // 不完整的段
      res ^= xorsum(n - cur);
      break;
    }
    res ^= xorsum(nxt - cur - 1); // 完整段
    cur = nxt;
  }
  return res;
}
signed main() {
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios::sync_with_stdio(false);
  cin >> t;
  while (t--) {
    cin >> op;
    if (op == "change")
      cin >> r;
    else if (op == "query") {
      cin >> n;
      ans = 0;
      while (n--) {
        cin >> s >> e;
        ans ^= (sumSG(e) ^ sumSG(s - 1)); // 全部异或得到最终 SG 值。
      }
      cout << (ans != 0);
    }
  }
}
原文