跳到正文

题解

题解:P16258 [DSTOI Round 0] 万分之一的光

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

文章信息

发布
2026年8月16日
收录
2026年8月18日
更新
2026年9月12日
篇幅
约 6 分钟
获赞
9
题目
P16258

版本

  1. v3当前
  2. v2
  3. v1

考虑过程 n\boldsymbol n 产生的序列为 PnP_n,设 SnS_n 为 PnP_n 两项之和的异或和。

由

Pn=[n]+Pn−1+Pn−2P_n=[n]+P_{n-1}+P_{n-2}

以及 Pn−1P_{n-1} 的首项为 n−1n-1、末项恒为 11,可得

Sn=Sn−1⊕Sn−2⊕(2n−1)⊕(n−1)⊕(n−2)⊕Sn−2=Sn−1⊕(2n−1)⊕(n−1)⊕(n−2).\begin{aligned} S_n &=S_{n-1}\oplus S_{n-2} \oplus(2n-1)\oplus(n-1)\oplus(n-2)\oplus S_{n-2}\\ &=S_{n-1}\oplus(2n-1)\oplus(n-1)\oplus(n-2). \end{aligned}

展开

Sn=Sn−3⊕(2n−1)⊕(2n−3)⊕(n−1)⊕(n−2).S_n =S_{n-3}\oplus(2n-1)\oplus(2n-3)\oplus(n-1)\oplus(n-2).

令

Ck=(2k−1)⊕(k−1),C_k=(2k-1)\oplus(k-1),

则

Sn=Sn−3⊕Cn⊕Cn−1.S_n=S_{n-3}\oplus C_n\oplus C_{n-1}.

最终可得

Sn=(⨁k=3n(Ck⋅[k≢n+1(mod3)]))  ⊕  (3⋅[n≢1(mod3)]),S_n = \left(\bigoplus_{k=3}^{n} \left(C_k \cdot [k\not\equiv n+1\pmod 3]\right)\right) \;\oplus\; \left(3\cdot [n\not\equiv 1\pmod 3]\right),

其中 [P][P] 为艾弗森记号,[P]=1[P]=1 若 PP 为真,否则为 00。

最终

ansn=n  ⊕  (3⋅[n≢1(mod3)])  ⊕  (⨁k=3n(((2k−1)⊕(k−1))⋅[k≢n+1(mod3)]))\boxed{ \text{ans}_n = n \;\oplus\; \left(3\cdot [n\not\equiv 1\pmod 3]\right) \;\oplus\; \left( \bigoplus_{k=3}^{n} \left( \left((2k-1)\oplus(k-1)\right) \cdot [k\not\equiv n+1\pmod 3] \right) \right) }

接下来只需计算一个贡献。

令

k=k0+3j,k=k_0+3j,

则

2k−1=6j+2k0−1,k−1=3j+k0−1.2k-1=6j+2k_0-1,\qquad k-1=3j+k_0-1.

问题变成

⨁j=0c−1(aj+b).\bigoplus_{j=0}^{c-1}(aj+b).

考虑第 ii 位,令 m=2i+1m=2^{i+1}。第 ii 位为 11 的个数为

∑j=0c−1(⌊aj+b+2im⌋−⌊aj+bm⌋).\sum_{j=0}^{c-1} \left( \left\lfloor\frac{aj+b+2^i}{m}\right\rfloor - \left\lfloor\frac{aj+b}{m}\right\rfloor \right).

记

F(n,m,a,b)=∑j=0n−1⌊aj+bm⌋,F(n,m,a,b) = \sum_{j=0}^{n-1} \left\lfloor\frac{aj+b}{m}\right\rfloor,

那么第 ii 位是否出现,只需判断

F(c,m,a,b+2i)−F(c,m,a,b)F(c,m,a,b+2^i)-F(c,m,a,b)

的奇偶性即可。

对于不同的余数类 r∈{0,1,2}r\in\{0,1,2\},取 k0(r)=(3⋅[r=0])+(4⋅[r=1])+(5⋅[r=2])k_0(r)=\left(3\cdot[r=0]\right)+\left(4\cdot[r=1]\right)+\left(5\cdot[r=2]\right),则对应的贡献为

get(r,n)=⨁k≥k0(r)k≡r(mod3)k≤n((2k−1)⊕(k−1)).\text{get}(r,n)= \bigoplus_{\substack{k\ge k_0(r)\\ k\equiv r\pmod 3\\ k\le n}} \left((2k-1)\oplus(k-1)\right).

最终答案为

ansn={1,n≤2,n  ⊕  (3⋅[n≢1(mod3)])  ⊕  (⨁r=0r≠(n+1)mod32get(r,n)),n≥3.\boxed{ \text{ans}_n= \begin{cases} 1,& n\le 2,\\[10pt] \displaystyle n \;\oplus\; \left(3\cdot [n\not\equiv 1\pmod 3]\right) \;\oplus\; \left( \bigoplus_{\substack{r=0\\ r\ne (n+1)\bmod 3}}^{2} \text{get}(r,n) \right), & n\ge 3. \end{cases} }
Code
C++
#include <bits/stdc++.h>
using namespace std;

using i128 = __int128_t;
using u64 = unsigned long long;

i128 fs(i128 n, i128 m, i128 a, i128 b) {
    i128 ans = 0;
    while (1) {
        if (a >= m) {
            ans += (n - 1) * n / 2 * (a / m);
            a %= m;
        }
        if (b >= m) {
            ans += n * (b / m);
            b %= m;
        }
        i128 y = a * n + b;
        if (y < m) break;
        n = y / m;
        b = y % m;
        swap(m, a);
    }
    return ans;
}

u64 xap(i128 a, i128 b, i128 n) {
    u64 res = 0;
    for (int i = 0; i <= 60; ++i) {
        i128 m = (i128)1 << (i + 1);
        i128 h = (i128)1 << i;
        i128 c = fs(n, m, a, b + h) - fs(n, m, a, b);
        if (c & 1) res |= 1ULL << i;
    }
    return res;
}

u64 get(int r, u64 n) {
    u64 k = r == 0 ? 3 : (r == 1 ? 4 : 5);
    if (n < k) return 0;
    i128 cnt = (n - k) / 3 + 1;
    return xap(6, 2 * (i128)k - 1, cnt) ^ xap(3, (i128)k - 1, cnt);
}

u64 solve(u64 n) {
    if (n <= 2) return 1;
    int ban = (n + 1) % 3;
    u64 x = 0;
    for (int r = 0; r < 3; ++r)
        if (r != ban) x ^= get(r, n);
    if ((n - 1) % 3) x ^= 3;
    return x ^ n;
}

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

    int T;
    cin >> T;
    while (T--) {
        u64 n;
        cin >> n;
        cout << solve(n) << '\n';
    }
    return 0;
}
原文
存档评论7 条抓取于