A New Stone Game
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 5338   Accepted: 2926

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

【思路】

博弈

  当n=1时,先手必胜;

  当n=2时,如果两堆相等,先手取后只剩一堆则局面必胜,先手取后剩两堆,则后手可以始终维持两堆相等直到(1,1),此时必由先手完成第一种情况。因此先手必败。

  当n=3时,若a1<=a2<=a3,则先手可以通过操纵a3使得剩下两堆为(a2,a2),此时局面必败。因此先手必胜。

  推广:

  当n为奇数时,若a1<=a2…<=an-1<=an,则操作an使得a1->a2,a3->a4….an-2->an-1,因为项为正,所以an>=a2-a1+…an-1-an-2。因此先手必胜。

  当n为偶数时:若a1=a2,a3=a4…an-2=an-1,则先手必败;否则总可以操作an使得an=a1,a2=a3…an-2=an-1,an-a1>=a2-a2+…an-1-an-2,此时先手必胜。

【代码】

 #include<cstdio>
#include<algorithm>
using namespace std; int n,a[]; int main() {
while(scanf("%d",&n)== && n) {
for(int i=;i<=n;i++) scanf("%d",&a[i]);
if(n&) puts("");
else {
sort(a+,a+n+);
int flag=;
for(int i=;i<=n;i+=)
if(a[i]!=a[i+]) { flag=; break; }
if(flag) puts(""); else puts("");
}
}
return ;
}

最新文章

  1. Windows程序设计_19_测试Windows应用程序加载函数
  2. Webform(五)——内置对象(Response、Request)和Repeater中的数据增删改
  3. TinkPad E40 CentOS 6.5 无线网卡驱动 RTL8191SEvB 安装
  4. 通过group by和having去除重复
  5. Google Analytics Premium VS Adobe Analytics
  6. SqlTransaction的解析
  7. CUDA C
  8. 扒一扒: Java 中的枚举
  9. 步步为营-87-imageAreaSelect插件使用(图片剪切)
  10. [Python] dict对象的keys()和values()返回的值,是否总是保证一一对应?
  11. C#6.0语言规范(十三) 接口
  12. Android Studio常用设置
  13. Find–atime –ctime –mtime的用法与区别总结
  14. VS2015编译OpenSSL
  15. leetcode 无重复字符的最长子串 python实现
  16. [LeetCode] Pacific Atlantic Water Flow 题解
  17. 30、java中递归算法
  18. Chapter 6. Names
  19. Android-获取网络图片设置壁纸
  20. 快速了解“云原生”(Cloud Native)和前端开发的技术结合点

热门文章

  1. python基础知识八
  2. oracle数据库误删恢复方法
  3. jQuery 事件 方法
  4. java.net.ServerSocket和java.net.Socket
  5. Linux 安装配置 JDK 8
  6. jquery中mouseout和mouseleave 事件的区别
  7. 利用jquery进行ajax提交表单和附带的数据
  8. git推送失败的问题
  9. (MVC)验证用户是否登录 登录认证
  10. CoreProfiler/NanoProfiler