Being a Good Boy in Spring Festival HDU-1850 Game theory, using the nature of XOR

  • Since any card can be taken in a pile, the number of cards in a pile is xxx , there issg [x] = x sg[x]=xsg[x]=x
  • First find the total game SG SGS Gans = sg [a 1] ⊕ sg [a 2] ⊕. . . ⊕ sg [an] ans = sg [a_1] \ oplus sg [a_2] \ oplus ... \ oplus sg [a_n]a n s=s g [ a1]s g [ a2]...s g [ an]
  • years = 0 years = 0a n s=0 means the first move is defeated.
  • On the contrary, the first mover will win. Then the path corresponding to the first game: Suppose there are two piles of cards x, yx, yx,y , assumingyyy in the card, is taken aftery 'y'Y , It must bePPP point, which satisfiesx ⊕ y ′ = 0 x\oplus y'=0xY=0 . According to the nature of XOR, for a non-zero numberxxx ,有 x ⊕ x = 0 x\oplus x=0 xx=0
  • It is assumed to be the first iiTo operate the i heap, first setans ′ = ans ⊕ sg [ai] ans'=ans\oplus sg[a_i]a n s=a n ss g [ ai] The result is in addition to theiiThe totalSG of theremaining piles outside the i pileSGS G value, if theans ′ ans’a n s a i a_i aiIt means that ans ′ ans’ can be taken from the pilea n s' Cards, making the game’s newSG SGThe S G value is 0, which meansPPwhen it corresponds to the backhand stateP point.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e2+10;
int a[N];
int main(){
    
    
	int m;
	while(scanf("%d",&m),m){
    
    
		int ans=0;
		for(int i=1;i<=m;++i){
    
    
			scanf("%d",&a[i]);
			ans^=a[i];
		}
		if(!ans) puts("0");
		else{
    
    
			int cnt=0;
			for(int i=1;i<=m;++i){
    
    
				if((ans^a[i])<=a[i]) ++cnt;
			}
			printf("%d\n",cnt);
		}
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/bloom_er/article/details/113775241