【POJ2975】Nim 博弈

转载请注明出处:http://blog.csdn.net/vmurder/article/details/42610939

其实我就是觉得原创的访问量比未授权盗版多有点不爽233。。。

题意:多组数据。

问先手有多少种取法使自己必胜。

题解:

首先Nim游戏的异或思想就不说了。

然后我们发现对于某个数,如果其它数的异或和比它小,那么先手拿掉这个数的一部分就可以使所有数异或为0,

先求出所有数异或和,然后再枚举每个数,异或一下就相当于其它所有数的异或和了,

然后可以进行判断出解。

长姿势:

^的优先级竟然比< 小233……

以后但凡涉及位运算都要怒写()啊!

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 1050
using namespace std;
int a[N],n;
int main()
{
//	freopen("test.in","r",stdin);
	int i,j,k,ans,ret;
	while(scanf("%d",&n),n)
	{
		for(ret=ans=0,i=1;i<=n;i++)scanf("%d",&a[i]),ans^=a[i];
		for(i=1;i<=n;i++)
		{
			if((ans^a[i])<a[i])ret++;
		}
		printf("%d\n",ret);
	}
	return 0;
}
时间: 2024-07-30 00:06:04

【POJ2975】Nim 博弈的相关文章

poj2975 Nim 博弈

      自从省赛结束了,好久都做过博弈题了,感觉都快忘了.今天找了几题练练手,在做过程中,感觉这道题挺有意思的.题目的意思是说,在Nim游戏中,先手有几种方式让 Nim 和变为0.(不知道Nim游戏的,请参考:这里) 其实我觉得这道题就是披着博弈的外衣,然后来考查你异或运算符(^)的使用的.在做题之前,我们想要了解异或运算符(^)的一个重要的性质: 现在我们有三个整数a, b, c: 我们假设c = a ^ b, 那么我们可以得到 b = c ^ a, a = b ^ c(交换律对异或运

ACM学习历程—HDU 3915 Game(Nim博弈 &amp;&amp; xor高斯消元)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3915 题目大意是给了n个堆,然后去掉一些堆,使得先手变成必败局势. 首先这是个Nim博弈,必败局势是所有xor和为0. 那么自然变成了n个数里面取出一些数,使得xor和为0,求取法数. 首先由xor高斯消元得到一组向量基,但是这些向量基是无法表示0的. 所以要表示0,必须有若干0来表示,所以n-row就是消元结束后0的个数,那么2^(n-row)就是能组成0的种数. 对n==row特判一下. 代码:

UVA 11859 Division Game (Nim博弈)

题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=32746 题意:有一个n*m(1<=n,m<=50)矩阵,每个元素均为2~10000之间的正整数,两个游戏者轮流操作.每次可以选一行中的1个或者大于1的整数,把他们中的每个数都变成它的某个真因子,比如12可以边长1,2,3,4或者6,不能操作的输. 分析:考虑每个数包含的素因子个数(比如12=2*2*3包含3个素因子),则让一个数"变成它的素因子"

hdu 5011 (nim博弈模版)

//nim博弈 //有n堆石头,两人轮流每次从一堆中拿至少1,之多全部的石头,没有石头可拿为lose //判断先手是win还是lose # include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int main() { int n,i; __int64 a,sum; while(~scanf("%d",&n)) { sum=0; fo

HDU 1849 Rabbit and Grass(nim博弈)

题目地址:HDU 1849 初次接触nim博弈,感觉好神奇的说...居然可以跟异或运算扯上关系....给人类的智商跪了...作为地球人我感到很自豪.. 具体证明什么的看这篇博客被.传送门 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #inclu

HDU 1907 Nim博弈变形

1.HDU 1907 2.题意:n堆糖,两人轮流,每次从任意一堆中至少取一个,最后取光者输. 3.总结:有点变形的Nim,还是不太明白,盗用一下学长的分析吧 传送门 分析:经典的Nim博弈的一点变形.设糖果数为1的叫孤独堆,糖果数大于1的叫充裕堆,设状态S0:a1^a2^..an!=0&&充裕堆=0,则先手必败(奇数个为1的堆,先手必败).S1:充裕堆=1,则先手必胜(若剩下的n-1个孤独堆个数为奇数个,那么将那个充裕堆全部拿掉,否则将那个充裕堆拿得只剩一个,这样的话先手必胜).T0:a1

UVA 1559 - Nim(博弈dp)

UVA 1559 - Nim 题目链接 题意:一开始有s个石子,2n个人轮流取石子,每个人有个最大能取数目,2n个人奇数一队,偶数一队,取到最后一个石子的队输,问谁赢 思路:记忆化搜索,每个人取的时候对应的后继状态如果有一个必败态,则该状态为必胜态,如果都是必胜态,则该状态为必败态 代码: #include <stdio.h> #include <string.h> int n, s, m[25], dp[25][10005]; int dfs(int now, int state

ZOJ 3529 A Game Between Alice and Bob (分解质因数+Nim博弈)

A Game Between Alice and Bob Time Limit: 5 Seconds      Memory Limit: 262144 KB Alice and Bob play the following game. A series of numbers is written on the blackboard. Alice and Bob take turns choosing one of the numbers, and replace it with one of

[Nim博弈]hdu 1850 Being a Good Boy in Spring Festival

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1850 Being a Good Boy in Spring Festival Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4053    Accepted Submission(s): 2394 Problem Description

POJ 2234 Matches Game(Nim博弈裸题)

Description Here is a simple game. In this game, there are several piles of matches and two players. The two player play in turn. In each turn, one can choose a pile and take away arbitrary number of matches from the pile (Of course the number of mat