#Multi-SG#HDU 3032 Nim or not Nim?
题目
有\(n\)堆石子,每次可以从一堆中取出若干个或是将一堆分成两堆非空的石子,
取完最后一颗石子获胜,问先手是否必胜
分析
它的后继还包含了分成两堆非空石子的SG函数,找规律可以发现
\[SG[x]=\begin{cases}0,x=0\\x-1,x=4k(k\in N^*)\\x+1,x=4k+3(k\in N)\\x,otherwise\end{cases}
\]
判断\(n\)堆石子SG函数异或和不为0则先手必胜
代码
#include <cstdio>
#include <cctype>
#define rr register
using namespace std;
typedef unsigned uit;
inline uit iut(){
	rr uit ans=0; rr char c=getchar();
	while (!isdigit(c)) c=getchar();
	while (isdigit(c)) ans=(ans<<3)+(ans<<1)+(c^48),c=getchar();
	return ans;
}
signed main(){
	for (rr uit T=iut();T;--T){
		rr uit ans=0;
		for (rr uit n=iut();n;--n){
			rr uit x=iut();
			switch (x&3){
				case 0:{
					ans^=x-1;
					break;
				}
				case 3:{
					ans^=x+1;
					break;
				}
				default:{
					ans^=x;
					break;
				}
			}
		}
		if (ans) puts("Alice");
		    else puts("Bob");
	}
	return 0;
}

                
            
        
浙公网安备 33010602011771号