hdu5795A Simple Nim(SG函数、打表找规律)

2 篇文章 0 订阅

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5795

题目大意:

给n堆石子,A和B两个人可以分别从任意一堆里面取任意石子。也可以选择将一堆石子分成新的三堆。谁最后拿不到石子谁输。

思路:

如果不分堆就是简单的Nim游戏。直接取异或和即可。现在分堆我们可以发现,如果一堆石子个数为7,那么分成1,2,4以后可以对局势不造成影响。所以我们可以通过打SG表发现规律。然后直接进行异或即可。

打表代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<set>
using namespace std;
int main()
{
	set<int>st;
	int sg[100];
	sg[0]=0;
	sg[1]=1;
	for(int i=2;i<=100;i++){
		st.clear();
		for(int j=0;j<i;j++){
			st.insert(sg[j]);
		}
		for(int a=i-2;a>=1;a--){
			for(int b=a-1;b>=1;b--){
				for(int c=a-1;c>=1;c--){
					if(a+b+c==i)st.insert(sg[a]^sg[b]^sg[c]);
				}
			}
		}
		int tt=0;
		while(st.count(tt))tt++;
		sg[i]=tt;
		printf("sg[%d]=%d\n",i,sg[i]);
	}
}

代码:

#include<stdio.h>
#include<math.h>
int main()
{
	int T,i,j,k,n,x;
	long long sum=0;
	scanf("%d",&T);
	while(T--){
		sum=0;
		scanf("%d",&n);
		for(i=1;i<=n;i++){
			scanf("%d",&x);
			if(x%8==0)sum^=(x-1);
			else if((x+1)%8==0)sum^=(x+1);
			else sum^=x;
		}
		if(sum==0)printf("Second player wins.\n");
		else printf("First player wins.\n");
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值