思路
先给一个结论,所有的白色格子 SG 函数值独立,也就是可以对每个白色各自单独考虑其它格子都为黑的 SG 函数值,然后异或起来。
证明
如下图,上面是原本的情况,下面是独立计算 SG 函数的情况。对 和 翻转后,原序列上从两个白变为了两个黑。而对于拆开后的情况,从两个位置上各有一个白色变为了第一个全都是黑色,第二个有两个白色,而同一个位置上多两个一样的状态,最后得到的 SG 函数值不变,因为两种状态可以做出的转移是一模一样的。
定义 为只有 一个白点的 SG 函数值。
容易发现对于两个点 和 ,如果 ,那么 ,因为 和 的后继个数相同,且对于 和 的 倍后继,有 ,即所有后继的后继个数也相同,所以 和 所有后继 SG 函数值相同。
然后就可以预处理 SG 函数值,暴力做法是枚举所有的数,然后枚举所有倍数。
类似的,可以用数论分块枚举数 ,把 相同的 SG 值存到一起。(由于数组开不下,受到数论分块复杂度证明的启发,对于 ,下标为 ,否则下标为 。)
枚举倍数时,设倍数为 ,则可以把 相等的 再用一层数论分块处理,复杂度为 ,证明参考杜教筛复杂度证明。
Code
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int n,sg1[N],sg2[N],limit,cnt[N];
#define pii pair<int,int>
#define pb push_back
#define mk make_pair
inline int get_sg(int x){
if(x<=limit)return sg1[x];
else return sg2[n/x];
}
inline void push_sg(int l,int r,int x){
if(r<=limit){
for(int i=l;i<=r;++i)
sg1[i]=x;
}
else sg2[n/l]=x;
}
inline void solve(){
int len,x,ans=0;
cin>>len;
while(len--){
cin>>x;
ans^=get_sg(x);
}
if(ans)cout<<"Yes\n";
else cout<<"No\n";
}
signed main(){
// freopen("color.in","r",stdin);
// freopen("color.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
limit=sqrt(n);
vector<pii>q;
for(int l=1;l<=n;++l){
int r=n/(n/l);
q.pb(mk(l,r));
l=r;
}
int len=q.size();
cnt[0]=1;
for(int i=len-1;i>=0;--i){
int now=0;
for(int l=2;l<=(n/q[i].second);++l){
int r=(n/q[i].second)/((n/q[i].second)/l);
now^=get_sg(l*q[i].second);
++cnt[now];
if(!((r-l+1)&1))now^=get_sg(l*q[i].second);
l=r;
}
int sg=0;
while(cnt[sg])++sg;
now=0;
for(int l=2;l<=(n/q[i].second);++l){
int r=(n/q[i].second)/((n/q[i].second)/l);
now^=get_sg(l*q[i].second);
--cnt[now];
if(!((r-l+1)&1))now^=get_sg(l*q[i].second);
l=r;
}
push_sg(q[i].first,q[i].second,sg);
}
int T;
cin>>T;
while(T--)solve();
return 0;
}