hdu 5795 A Simple Nim(nim博弈)


可以看出,当x%8==0,是sg(x)=x-1 ,x%8==7 时,sg(x)=x+1

sg打表程序

#include <iostream> 
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
int main() {
	int i, j, k;
	int sg[1010], mex[1010];
	sg[0] = 0, sg[1] = 1;
	for(i = 2; i <= 1000; i++) {
		memset(mex, 0, sizeof mex);
		for(j = 1; j <= i; j++) {
			for(k = 1; k <= i; k++) {
				if(j + k < i) {
					mex[sg[j]^sg[k]^sg[i - j - k]] = 1;
				}
			}	
		}
		for(j = 1; j <= i; j++) {
			mex[sg[i - j]] = 1;
		}
		for(j = 0; ; j++) {
			if(mex[j] == 0) {
				sg[i] = j;
				break;
			}
		}
	}
	for(i = 0; i <= 100; i++) {
		printf("sg[%d] : %d\n", i, sg[i]);
	}
	return 0;
}
#include<stdio.h>
using namespace std;

int sg(int x){
	if(x%8==0)
	return x-1;
	if(x%8==7)
	return x+1;
	return x;
}
int main(){
	int	t;
	scanf("%d",&t);
	while(t--){
		int n;
		scanf("%d",&n);
		int tem=0;
		for(int i=0;i<n;i++){
			int x;
			scanf("%d",&x);
			tem^=sg(x);	
		}
		if(tem)
		puts("First player wins.");
		else puts("Second player wins.") ;
	} 
	return 0;
}


猜你喜欢

转载自blog.csdn.net/l2533636371/article/details/80182592