题解:P7490 「Stoi2031」蒲公英的约定(vol.1)

· · 题解

这里发一篇比较像人话的题解。

Solution

先考虑如何求一丛蒲公英的 \text{SG} 函数。

令一丛蒲公英为 (a_1,\dots,a_n,0),可以发现:

\text{SG}(a_1,\dots,a_n,0)= \begin{cases} 0&n=0\\ a_1-1&n>0\land a_1\le \text{SG}(a_2,\dots,a_n,0)\\ a_1&n>0\land a_1>\text{SG}(a_2,\dots,a_n,0)\\ \end{cases}

证明是简单的,用脚归纳一下即可。

但是这样还是不够优雅,注意到 a 单调不增,我们令 l 为 a 序列最长相等前缀,则:

\begin{aligned} \text{SG}(a_l,\dots,a_n,0)&=a_1\\ \text{SG}(a_{l-1},\dots,a_n,0)&=a_1-1\\ \text{SG}(a_{l-2},\dots,a_n)&=a_1\\ \vdots\\ \text{SG}(a_1,\dots,a_n)=\begin{cases}a_1-1&2\mid l\\a_1&2\nmid l\end{cases} \end{aligned}

因此我们只需要求出 l 就能求出 \text{SG} 函数。

求 l 就是解不等式:令当前这从蒲公英的个数为 n,t=\lfloor\sigma n\rfloor,我们要找到一个最小的 l 满足 \sigma(n-lt)<t,移项可以得到 l=\lfloor\dfrac{\sigma n}{(\sigma+1)t}\rfloor+1,于是我们就可以 O(1) 求出一个数的 \text{SG} 函数了。

接下来考虑整道题怎么做。

令 sum=\oplus_{i=1}^n \text{SG}(s_i),先判掉 sum=0 的情况,此时答案为 0,然后枚举 x 计算吹 s_x 丛先手获胜的概率,注意到 s_x 及其后继状态恰好取遍 0,\dots,t 且互不重复,那么相当于我们要计算 \sum\limits_{0\le i\le t,i\neq\text{SG}(s_x)}[sum\oplus a_x\oplus i=0],又因为 sum\neq 0,所以不等于 \text{SG}(s_x) 的限制的概率可以去掉,获胜的概率就为 \dfrac{[sum\oplus a_x\le t]}t,于是我们就做完了。

总时间复杂度为 O(n)。

Code

//when you use vector or deque,pay attention to the size of it.
//by OldDriverTree
#include<bits/stdc++.h>
//#include<atcoder/all>
#define P pair<int,int>
#define int long long
#define mid (l+r>>1)
using namespace std;
//using namespace atcoder;
//using mint=modint998244353;
const int N=3e5+1,mod=20190816170251;
int _,n,p,q,ans,sum,cnt,a[N],b[N];

struct custom_hash
{
    static uint64_t splitmix64(uint64_t x) {
        x+=0x9e3779b97f4a7c15;
        x=(x^(x>>30) )*0xbf58476d1ce4e5b9;
        x=(x^(x>>27) )*0x94d049bb133111eb;
        return x^(x>>31);
    }
    size_t operator() (uint64_t x) const {
        static const uint64_t FIXED_RANDOM=chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x+FIXED_RANDOM);
    }
};
int read() {
    int x=0; bool _=true; char c=0;
    while (!isdigit(c) ) _&=(c!='-'),c=getchar();
    while (isdigit(c) ) x=x*10+(c&15),c=getchar();
    return _?x:-x;
}
int mul(int x,int y) {
    return (__int128)x*y%mod;
}
int power(int a,int b=mod-2)
{
    int res=1;
    while (b) {
        if (b&1) res=mul(res,a);
        a=mul(a,a),b>>=1;
    }
    return res;
}
int SG(int x) {
    int t=(__int128)x*p/q;
    if (!t) return 0;
    int l=(__int128)x*p/(p+q)/t;
    return t-(l&1);
}
main()
{
    _=read(),n=read();
    for (int i=1;i<=n;i++) a[i]=read();
    if (_==1) p=8047378541243,q=1e13;
    else if(_==2) p=5464101615137,q=1e13;
    else if(_==3) p=61803398874989,q=1e14;
    else p=read(),q=read();
    for (int i=1;i<=n;i++) sum^=SG(a[i]);
    if (!sum) return !putchar('0');
    for (int i=1;i<=n;i++) {
        int t=(__int128)p*a[i]/q;
        if (!t) continue; cnt++;
        if ( (sum^SG(a[i]) )<=t)
        ans=(ans+power(t) )%mod;
    }
    printf("%lld",mul(ans,power(cnt) ) );
    return 0;
}