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

· · 题解

何意味?数位 DP?蓝?T6?代码量超过 0.5KB?真的吗?

先不考虑边界(也就是默认 n \ge 4)。记 [n] 为对空序列做「过程 n」得到的序列,S_n 为 [n] 任意相邻两项之和的异或和。下面空格表示序列拼接。

[n]=n\ [n-1]\ [n-2]=n\ n-1\ [n-2]\ [n-3]\ [n-2]

而我们通过归纳知道 \forall k \in \mathbb{Z^+},[k] 以 k 开头,以 1 结尾。

于是:

S_n&=(2n-1) \oplus (2n-3) \oplus S_{n-2} \oplus (n-2) \oplus S_{n-3} \oplus (n-1) \oplus S_{n-2}\\ &=S_{n-3} \oplus (2n-1) \oplus (2n-3) \oplus (n-1) \oplus (n-2)\\ &=S_{n-3} \oplus 2(n-1) \oplus 2(n-2) \oplus (n-1) \oplus (n-2) \end{aligned}

边界为 S_1=0,S_2=3,S_3=4。

于是 S_n=\left(\bigoplus\limits_{\substack{3 \mid n-k\\2 \le k \le n}}(2(k-1) \oplus 2(k-2) \oplus (k-1) \oplus (k-2))\right) \oplus [3 \mid n]。

而我们知道 \mathrm{ans}_n=S_n \oplus n(因为 a 最前面有个 0),所以只要能快速处理中间的那坨就行。

于是 @MoLing_qwq 写了数位 DP,@_fewq 写了类欧,其他人写了神秘的一坨,想复杂了但是太牛了。

考虑 f(x)=x \oplus (x-1) 究竟是什么。容易发现其实就是 x 在二进制下的最低位及后面的位均为 1,也就是 2\mathrm{lowbit}(x)-1。

然后再考虑 g(x)=2x \oplus 2(x-1) \oplus x \oplus (x-1) 究竟是什么。

g(x)&=2f(x) \oplus f(x)\\ &=2(2\mathrm{lowbit}(x)-1) \oplus (2\mathrm{lowbit}(x)-1)\\ &=2\mathrm{lowbit}(x)+1 \end{aligned}

所以要求的实际上就是:

\mathrm{ans}_n&=n \oplus [3|n] \oplus \left(\bigoplus\limits_{\substack{3 \mid n-k\\2 \le k \le n}}(2\mathrm{lowbit}(k-1)+1)\right)\\ &=n \oplus [3|n] \oplus \left(\bigoplus\limits_{\substack{3 \mid (n-1)-k\\1 \le k \le n-1}}(2\mathrm{lowbit}(k)+1)\right) \end{aligned}

记 w=n-1,则你相当于要对每个 2^i 统计满足 3 \mid w-k,1 \le k \le w 且 \mathrm{lowbit}(k)=2^i 的 k 数量,记作 h_i。

容易发现,\mathrm{lowbit}(k)=2^i \iff 2^i \mid k 且 2^{i+1} \nmid k,于是记 r_i 为满足 3 \mid w-k,1 \le k \le w,2^i \mid k 的 k 数量。

于是 $h_i=r_i-r_{i+1}$。带回 $\mathrm{ans}_n$ 中。 $\begin{aligned} \mathrm{ans}_n&=n \oplus [3|n] \oplus \left(\bigoplus\limits_{\substack{3 \mid (n-1)-k\\1 \le k \le n-1}}(2\mathrm{lowbit}(k)+1)\right)\\ &=n \oplus [3|n] \oplus \left(\bigoplus\limits_{i \in \mathbb{N}}[2 \nmid h_i](2^{i+1}+1)\right)\\ &=n \oplus [3|n] \oplus \left(\bigoplus\limits_{i \in \mathbb{N}}[r_i \not\equiv r_{i+1}\!\!\!\!\!\!\pmod 2](2^{i+1}+1)\right)\\ &=n \oplus [3|n] \oplus \left(\bigoplus\limits_{i \in \mathbb{N}}[2 \nmid r_i](2^{i+1} \oplus 2^i)\right) \end{aligned}

其中第三行到第四行的边界 i=0 无需特判,因为 2^{0+1}+1=2^{0+1} \oplus 2^0。

然后就结束了,直接算就好了。r_i 不会算的可以看代码。

其实最后这段不推也可以,只是多算个 h_i 代码略微难写一点罢了。

:::info[不推最后一段的 AC 记录] AC Code:

//x^(x-1)=2lowbit(x)-1,x^(x-1)^2x^2(x-1)=2lowbit(x)+1
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int t;
ll n,w,o,e,r,ans;
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>t;
    while(t--){
        cin>>n,ans=0;
        w=n-1,o=0;
        for(ll i=1ll<<59,j=59;i;i>>=1,--j) e=(w&(i-1))%3,e&&!(j&1)&&(e^=3),r=((w>>j)-e+2)/3,r<0&&(r=0),((r^o)&1)&&(ans^=i<<1|1),o=r;
        ans^=n,!(n%3)&&(ans^=1);
        cout<<ans<<"\n";
    }
    return 0;
}

:::

:::info[推了最后一段的 AC 记录] AC Code:

//x^(x-1)=2lowbit(x)-1,x^(x-1)^2x^2(x-1)=2lowbit(x)+1
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int t;
ll n,w,o,e,r,ans;
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>t;
    while(t--){
        cin>>n,ans=0;
        w=n-1,o=0;
        for(ll i=1ll<<59,j=59;i;i>>=1,--j) e=(w&(i-1))%3,e&&!(j&1)&&(e^=3),r=((w>>j)-e+2)/3,r&1&&(ans^=i<<1|i);
        ans^=n,!(n%3)&&(ans^=1);
        cout<<ans<<"\n";
    }
    return 0;
}

:::

然后推最后一段的最大用处是 Code Golf(

代码长度 176B,目前最短解。

#import<ios>
long n,e,A,i,j;main(){while(~scanf("%ld",&n))if(j--){for(j=60;j--;)i=1l<<j,e=(n-1&i-1)%3,!e|j&1||(e^=3),A^=(((n-1)/i-e+2)/3&1)*i*3;A=!printf("%ld\n",A^n^!(n%3));}}

为何标程是数位 DP?评价是玩原神玩的。