题解:P3179 [HAOI2015] 数组游戏
题目传送门
思路
先给一个结论,所有的白色格子 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;
}