poj 1740 A New Stone Game(博弈)
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 5338 | Accepted: 2926 |
Description
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
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
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 ;
}
poj 1740 A New Stone Game(博弈)的更多相关文章
- POJ 1740 A New Stone Game(博弈)题解
题意:有n个石子堆,每一个都可以轮流做如下操作:选一个石堆,移除至少1个石子,然后可以把这堆石子随便拿几次,随便放到任意的其他石子数不为0的石子堆,也可以不拿.不能操作败. 思路:我们先来证明,如果某 ...
- POJ 1740 A New Stone Game(普通博弈)
A New Stone Game 题意: 对于n堆石子,每堆若干个,两人轮流操作,每次操作分两步,第一步从某堆中去掉至少一个,第二步(可省略)把该堆剩余石子的一部分分给其它的某些堆.最后谁无子可取即输 ...
- POJ 1740 A New Stone Game(多堆博弈找规律)
传送门 //有n堆,AB轮流从n堆的一堆中移任意个,可以扔掉,也可以移给其他堆中的一堆 //最先移完的胜 //如果n堆中两两堆数目相等,那肯定是B胜 //但只要有非两两相同的,如xyz,A先, //A ...
- POJ 1740 A New Stone Game 又是博弈论配对找规律orz 博弈论 规律
http://poj.org/problem?id=1740 这个博弈一眼看上去很厉害很高大上让人情不自禁觉得自己不会写,结果又是找规律…… 博弈一般后手胜都比较麻烦,但是主要就是找和先手的对应关系, ...
- POJ 1740 A New Stone Game
A New Stone Game Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5453 Accepted: 2989 ...
- [原博客] POJ 1740 A New Stone Game
题目链接题意:有n堆石子,两人轮流操作,每次每个人可以从一堆中拿走若干个扔掉(必须),并且可以从中拿走一些分到别的有石子的堆里(可选),当一个人不能拿时这个人输.给定状态,问是否先手必胜. 我们参考普 ...
- 博弈论(男人八题):POJ 1740 A New Stone Game
A New Stone Game Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5694 Accepted: 3119 ...
- poj 1704 Georgia and Bob(阶梯博弈)
Georgia and Bob Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9363 Accepted: 3055 D ...
- Something about 博弈(POJ 3922 A simple stone game)
先是题目,本来是第三次训练的题,在这特别提出来讲. 先是题目: E - A simple stone game Time Limit:1000MS Memory Limit:65536KB ...
随机推荐
- HTML中的API
在程序语言里面就使用API这个行为来讲,可拆解为两个操作:取得API接口和运行API功能 例如:书本具有传授知识的功能,这里就好比一个API,学生拿出某个课本学习,就相当于取得API,学习通过课本学习 ...
- SQL 2012的分页
今天看到一篇文章介绍2012中的分页,就想测试一下新的分页方法比原先的有多少性能的提升,下面是我的测试过程(2012的分页语法这里不在做多的说明,MSDN上一搜就有): 首先我们来构造测试数据: -- ...
- Delphi 类方法和普通方法的区别 .
//类声明 TMyClass = class public class procedure MyProc; //类方式 constructor Create; //Crea ...
- ios专题 - openSSL
1 概述 1.1 产生背景 基 于万维网的电子商务和网上银行等新兴应用,极大地方便了人们的日常生活,受到人们的青睐.由于这些应用都需要在网络上进行在线交易,它们对网络通信的安全 性提出了更高的要求 ...
- who am i
本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. 为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体谅. ...
- [Neural Networks] Dropout阅读笔记
多伦多大学Hinton组 http://www.cs.toronto.edu/~rsalakhu/papers/srivastava14a.pdf 一.目的 降低overfitting的风险 二.原理 ...
- [jQuery编程挑战]008 生成逗号分隔数字
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="utf-8&quo ...
- 我学C的那些年[ch02]:宏,结构体,typedef
c语言的编译过程: 预处理 编译 汇编 链接 而预处理中有三种情况: 文件包含( #include ) 条件编译(#if,#ifndef,#endif) 宏定义( #define ) 宏就是预处理中的 ...
- Laravel之路——file缓存修改为redis缓存
1.Session: 修改.evn文件: SESSION_DRIVER:redis (如果还不行的话,修改config/session.php的driver) 2.缓存修改为redis 注意:使用 L ...
- Linux相关命令
使用的是ubuntu 安装JDK 输入java 命令会有提示安装的软件列表 sudo apt-get install openjdk-6-jdk sudo rm file名 删除文件 sudo r ...