1. 程式人生 > >洛谷 #2197. Nim遊戲

洛谷 #2197. Nim遊戲

題意

有n堆石子,每人必須從任意一堆石子取任意多的石子(/not = 0),不能行動者輸

題解

SG函式

可以發現,對於第i堆石子a[i],可以達到0 ~ a[i] - 1這n個狀態,故SG(a[i]) = a[i](SG值從0開始)

然後用SG定理,求亦或和確定勝負

除錯記錄

#include <cstdio>

using namespace std;

int T, n;

int main(){
	scanf("%d", &T);
	
	while (T--){
		scanf("%d", &n);
		int ans =
0; for (int x, i = 1; i <= n; i++){ scanf("%d", &x); ans ^= x; } if (ans) printf("Yes\n"); else printf("No\n"); } return 0; }