poj1740 A New Stone Game(博弈)

A New Stone Game

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 5028   Accepted: 2753

Description

Alice and Bob decide to play a new stone game.At the beginning of the game they pick n(1<=n<=10) piles of stones in a line. Alice and Bob move the stones in turn.

At each step of the game,the player choose a pile,remove at least one stones,then freely move stones from this pile to any other pile that still has stones.

For example:n=4 and the piles have (3,1,4,2) stones.If the player chose the first pile and remove one.Then it can reach the follow states.

2 1 4 2

1 2 4 2(move one stone to Pile 2)

1 1 5 2(move one stone to Pile 3)

1 1 4 3(move one stone to Pile 4)

0 2 5 2(move one stone to Pile 2 and another one to Pile 3)

0 2 4 3(move one stone to Pile 2 and another one to Pile 4)

0 1 5 3(move one stone to Pile 3 and another one to Pile 4)

0 3 4 2(move two stones to Pile 2)

0 1 6 2(move two stones to Pile 3)

0 1 4 4(move two stones to Pile 4)

Alice always moves first. Suppose that both Alice and Bob do their best in the game.

You are to write a program to determine who will finally win the game.

Input

The input contains several test cases. The first line of each test case contains an integer number n, denoting the number of piles. The following n integers describe the number of stones in each pile at the beginning of the game, you may assume the number of
stones in each pile will not exceed 100.

The last test case is followed by one zero.

Output

For each test case, if Alice win the game,output 1,otherwise output 0.

Sample Input

3
2 1 3
2
1 1
0

Sample Output

1
0

Source

[email protected]

此博客一语道破天机

http://www.cnblogs.com/rainydays/archive/2011/07/09/2101918.html

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
typedef __int64 ll;

using namespace std;

#define N 15
int a[N];

int main()
{
	int i,j,n;
	while(scanf("%d",&n),n)
	{
		for(i=0;i<n;i++)
			scanf("%d",&a[i]);

		if(i&1)
		{
			printf("1\n");
			continue;
		}

		sort(a,a+n);

		for(i=0;i<n;i+=2)
			if(a[i]!=a[i+1])
			{
				printf("1\n");
				break;
			}

		if(i>=n)
			printf("0\n");
	}
	return 0;
}
时间: 2024-11-10 01:02:26

poj1740 A New Stone Game(博弈)的相关文章

UVA 1378 - A Funny Stone Game(博弈)

UVA 1378 - A Funny Stone Game 题目链接 题意:给定n堆石头,然后每次能选i, j, k,3堆(i < j <= k),然后从i中哦功能拿一堆出来,往另外两堆放一个进去,最后不能取的输,问先手能否必胜,如果能,输出开始选的3堆 思路:组合游戏,需要转化,把石子一字排开,最后肯定都归到n堆上,n堆是不能取的,所以假设每个石子代表一堆,从左往右分别是n - 1, n - 2, n - 3 ... 2, 1, 0,然后每次取一个加两个,就相当于取掉一堆,多上两堆,这样就转

POJ1740|A NEW STONE GAME|博弈论

DescriptionAlice and Bob decide to play a new stone game.At the beginning of the game they pick n(1<=n<=10) piles of stones in a line. Alice and Bob move the stones in turn.At each step of the game,the player choose a pile,remove at least one stones

Light OJ 1296 - Again Stone Game (博弈sg函数递推)

F - Again Stone Game Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Description Alice and Bob are playing a stone game. Initially there are n piles of stones and each pile contains some stone. Alice stars the

poj1740 A New Stone Game

题意:对于n堆石子,每堆若干个,两人轮流操作,每次操作分两步,第一步从某堆中去掉至少一个,第二步(可省略)把该堆剩余石子的一部分分给其它的某些堆. 真是好♂题,代码不长就是好♂题. 首先考虑两堆相同的石子,先手一定必输,因为若是我操作第一堆,则后手也可以对第二堆做对称决策. 其实,其他情况,一定是先手必胜. 第一种情况:奇数堆. 我们可以将最大堆的石子分配给其他堆让他们两两配对,如下图所示: 显然,红色部分绝壁不会超过第五个 石子的高度. 第二种情况:偶数情况 我们可以把最大堆和最小堆先配对,剩

HDU 4387 Stone Game (博弈)

题目:传送门. 题意:长度为N的格子,Alice和Bob各占了最左边以及最右边K个格子,每回合每人可以选择一个棋子往对面最近的一个空格移动.最先不能移动的人获得胜利. 题解: k=1时 很容易看出,n为奇数则后手获胜,n为偶数则先手获胜 k>1时 如果n=2*k+1,则棋 盘中只有一个空白的格子,每次移动必须移动到当前的空白格子上.先手方可以先随意选择一颗棋子占据中间的位置,然后双方互有移动,移动过程中双方肯定都会 选择一颗在己方半场的棋子移动到对方半场里.直到后手方还剩下一颗己方半场的棋子时,

博弈论类题目小结——转载

出处http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 首先当然要献上一些非常好的学习资料: 基础博弈的小结:http://blog.csdn.net/acm_cxlove/article/details/7854530 经典翻硬币游戏小结:http://blog.csdn.net/acm_cxlove/article/details/7854534 经典的删边游戏小结:http://blog.csdn.net/acm

博弈论之Nim

博弈论(一):Nim游戏 重点结论:对于一个Nim游戏的局面(a1,a2,...,an),它是P-position当且仅当a1^a2^...^an=0,其中^表示位异或(xor)运算. Nim游戏是博弈论中最经典的模型(之一?),它又有着十分简单的规则和无比优美的结论,由这个游戏开始了解博弈论恐怕是最合适不过了. Nim游戏是组合游戏(Combinatorial Games)的一种,准确来说,属于“Impartial Combinatorial Games”(以下简称ICG).满足以下条件的游戏

poj1737~poj1744——ltc男人八题

准备开刷这充满神秘感的八道题,虽然我不是男的... 完成度:3/8 2016.7.~?   poj1742 Coins 题意:给你n种面值的硬币和每种硬币的数量.求1~m中有多少个可以用这些硬币组成. 算法:dp 解析:dp[i][j]表示用前i中硬币组成j时第i种硬币剩余的枚数(若不能组成j则为-1) 考虑以下四种情况: (1)若dp[i-1][j]>=0,则无需第i种硬币,为c[i] (2)若val[i]>j,面值太大,则为-1 (3)若dp[i][j-val[i]]<=0,无法组成

2018.12.1 Test

目录 2018.12.1 Test A 串string(思路) B 变量variable(最小割ISAP) C 取石子stone(思路 博弈) 考试代码 B C 2018.12.1 Test 题目为2018.1.2雅礼集训. 时间:3.5h 期望得分:100+30+10 实际得分:100(0)+0+10 A 串string(思路) 如果一个串不是回文串,答案是1(我竟然漏了QAQ). 否则,除了以下三种情况无解外,都能两次消掉: aaaaa aabaa ababa 判一下就OK了. #inclu