[LLH邀请赛]巧克力棒
题目
题解
复习 \(\text{NIM}\) 游戏的规则——如果盒子中球的个数的异或和为 \(0\),那么先手必胜。而这道题我们能改变异或和的,就是从包(盒子)中取出盒子(巧克力棒),这样会改变所有盒子的异或和。
那么,我们只需要让先手把所有的能够使得异或和为 \(0\) 的方案全部取出来,这样后手将没有任何方式使得异或和为 \(0\),即使再取出额外的巧克力棒。
那么我们最后的答案判断即为——判断初始时包中的巧克力棒有没有一组异或和为 \(0\) 的方案,如果有,先手将所有异或和为 \(0\) 的方案取出,先手必胜输出 NO
(奇怪的输出),反之,先手面临的无论如何都是异或和不等于 \(0\) 的局面(先手必败),输出 YES
(奇怪的输出)。
#include<bits/stdc++.h>
using namespace std;
#define rep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i>=i##_end_;--i)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
#define LL long long
#define ull unsigned long long
#define uint unsigned int
#define pii pair< int,int >
#define Endl putchar('\n')
// #define int long long
// #define int unsigned
// #define int unsigned long long
#define cg (c=getchar())
template<class T>inline void qread(T& x){
char c;bool f=0;
while(cg<'0'||'9'<c)f|=(c=='-');
for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
if(f)x=-x;
}
template<class T>inline T qread(const T sample){
T x=0;char c;bool f=0;
while(cg<'0'||'9'<c)f|=(c=='-');
for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
return f?-x:x;
}
#undef cg
// template<class T,class... Args>inline void qread(T& x,Args&... args){qread(x),qread(args...);}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
template<class T>void fwrit(const T x){//just short,int and long long
if(x<0)return (void)(putchar('-'),fwrit(-x));
if(x>9)fwrit(x/10);
putchar(x%10^48);
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}
template<class T>struct Basic{
#define MAXSIZE 62
T f[MAXSIZE+5],g[MAXSIZE+5];
int flg,siz;
inline void clear(){
memset(f,0,sizeof f);flg=siz=0;
}
inline void insert(T num){
fep(i,MAXSIZE,0){
if(!(num>>i))continue;
if(!f[i]){f[i]=num;++siz;return;}
num^=f[i];
}
++flg;
}
inline T queryMax(T ret){
fep(i,MAXSIZE,0)if((ret^f[i])>ret)
ret^=f[i];
return ret;
}
inline T queryMin(){
if(flg)return 0;
T ret=(1ll<<MAXSIZE);
rep(i,0,MAXSIZE)ret=Min(ret,f[i]);
}
inline void buildg(){
rep(i,0,MAXSIZE)g[i]=f[i];
rep(i,0,MAXSIZE)rep(j,0,i-1)if(g[i]&(1ll<<j))
g[i]^=g[j];
}
inline T queryNum(T k){
//must build g before use it
if(flg)--k;
if(k==0)return 0;
T ret=0;
rep(i,0,MAXSIZE)if(g[i]){
if(k&1)ret^=g[i];
k>>=1;
}
return k?-1:ret;
}
};
Basic<LL>G;
int n;
signed main(){
while(cin>>n){
G.clear();
rep(i,1,n)G.insert(qread(1ll));
if(G.flg)puts("NO");
else puts("YES");
}
return 0;
}
推荐题目 [CQOI2013]新Nim游戏
思路十分相似,直接给代码了。
#include<bits/stdc++.h>
using namespace std;
#define rep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i>=i##_end_;--i)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
#define LL long long
#define ull unsigned long long
#define uint unsigned int
#define pii pair< int,int >
#define Endl putchar('\n')
// #define int long long
// #define int unsigned
// #define int unsigned long long
#define cg (c=getchar())
template<class T>inline void qread(T& x){
char c;bool f=0;
while(cg<'0'||'9'<c)f|=(c=='-');
for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
if(f)x=-x;
}
template<class T>inline T qread(const T sample){
T x=0;char c;bool f=0;
while(cg<'0'||'9'<c)f|=(c=='-');
for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
return f?-x:x;
}
#undef cg
// template<class T,class... Args>inline void qread(T& x,Args&... args){qread(x),qread(args...);}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
template<class T>void fwrit(const T x){//just short,int and long long
if(x<0)return (void)(putchar('-'),fwrit(-x));
if(x>9)fwrit(x/10);
putchar(x%10^48);
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}
LL ans;
template<class T>struct Basic{
#define MAXSIZE 62
T f[MAXSIZE+5],g[MAXSIZE+5];
int flg,siz;
inline void clear(){
memset(f,0,sizeof f);flg=siz=0;
}
inline void insert(T num){
T tmp=num;
fep(i,MAXSIZE,0){
if(!(num>>i))continue;
if(!f[i]){f[i]=num;++siz;return;}
num^=f[i];
}
ans=ans+tmp;
++flg;
}
inline T queryMax(T ret){
fep(i,MAXSIZE,0)if((ret^f[i])>ret)
ret^=f[i];
return ret;
}
inline T queryMin(){
if(flg)return 0;
T ret=(1ll<<MAXSIZE);
rep(i,0,MAXSIZE)ret=Min(ret,f[i]);
}
inline void buildg(){
rep(i,0,MAXSIZE)g[i]=f[i];
rep(i,0,MAXSIZE)rep(j,0,i-1)if(g[i]&(1ll<<j))
g[i]^=g[j];
}
inline T queryNum(T k){
//must build g before use it
if(flg)--k;
if(k==0)return 0;
T ret=0;
rep(i,0,MAXSIZE)if(g[i]){
if(k&1)ret^=g[i];
k>>=1;
}
return k?-1:ret;
}
};
Basic<int>G;
const int MAXK=100;
int k,a[MAXK+5];
signed main(){
cin>>k;
rep(i,1,k)cin>>a[i];
sort(a+1,a+k+1);
fep(i,k,1)G.insert(a[i]);
cout<<ans<<endl;
return 0;
}