这里发一篇比较像人话的题解。
Solution
先考虑如何求一丛蒲公英的 函数。
令一丛蒲公英为 ,可以发现:
证明是简单的,用脚归纳一下即可。
但是这样还是不够优雅,注意到 单调不增,我们令 为 序列最长相等前缀,则:
因此我们只需要求出 就能求出 函数。
求 就是解不等式:令当前这从蒲公英的个数为 ,,我们要找到一个最小的 满足 ,移项可以得到 ,于是我们就可以 求出一个数的 函数了。
接下来考虑整道题怎么做。
令 ,先判掉 的情况,此时答案为 ,然后枚举 计算吹 丛先手获胜的概率,注意到 及其后继状态恰好取遍 且互不重复,那么相当于我们要计算 ,又因为 ,所以不等于 的限制的概率可以去掉,获胜的概率就为 ,于是我们就做完了。
总时间复杂度为 。
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;
}