【CodeVS 3153】取石子游戏
http://codevs.cn/problem/3153/
对于这道题,直觉告诉我每一个状态一定是必胜或必败的
然后设定操作次数t,t为取完些石子最多需要多少步。
如果\(a_i\)不为1,\(t=\sum a_i+n-1\)。因为每次只让操作数减一。
但因为有\(a_i\)为1的情况,可以直接让操作数减二,所以我们把石子数为1的堆数和剩下的石子堆的操作数作为状态进行记忆化搜索。
正如xiaoyimi所说,细节确实很多QaQ
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 53;
const int M = 50003;
int in() {
int k = 0, fh = 1; char c = getchar();
for(; c < '0' || c > '9'; c = getchar())
if (c == '-') fh = -1;
for(; c >= '0' && c <= '9'; c = getchar())
k = k * 10 + c - 48;
return k * fh;
}
int n, a, cnt, sum;
bool vis[M][N], f[M][N];
bool pd(int tot, int num) {
if (vis[tot][num]) return f[tot][num];
vis[tot][num] = true;
if (tot == 1) return f[tot][num] = pd(0, num + 1);
if (num >= 1 && !pd(tot, num - 1)) return f[tot][num] = true;
if (num >= 2 && !pd(tot + 2 + (tot > 0), num - 2)) return f[tot][num] = true;
if (tot >= 2 && !pd(tot - 1, num)) return f[tot][num] = true;
if (num >= 1 && tot != 0 && !pd(tot + 1, num - 1)) return f[tot][num] = true;
return false;
}
int main() {
int T; T = in();
while (T--) {
n = in(); sum = cnt = 0;
for(int i = 1; i <= n; ++i) {
a = in();
if (a == 1) ++cnt;
else sum += a + 1;
}
if (sum) --sum;
puts(pd(sum, cnt) ? "YES" : "NO");
}
return 0;
}
【CodeVS 3153】取石子游戏的更多相关文章
- Games:取石子游戏(POJ 1067)
取石子游戏 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37662 Accepted: 12594 Descripti ...
- HDU 2516 取石子游戏(斐波那契博弈)
取石子游戏 Time Limit: 2000/1000 MS(Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...
- hdu 1527 取石子游戏(Wythoff Game)
题意:Wythoff Game 思路:Wythoff Game #include<iostream> #include<stdio.h> #include<math.h& ...
- HDU 2516 取石子游戏(FIB博弈)
取石子游戏 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- 1874: [BeiJing2009 WinterCamp]取石子游戏 - BZOJ
Description小H和小Z正在玩一个取石子游戏. 取石子游戏的规则是这样的,每个人每次可以从一堆石子中取出若干个石子,每次取石子的个数有限制,谁不能取石子时就会输掉游戏. 小H先进行操作,他想问 ...
- HDU-1527 取石子游戏
http://acm.hdu.edu.cn/showproblem.php?pid=1527 交换 :可实现. if( n < m ) { n^=m; m^=n; n^=m; } (三)尼姆博 ...
- bzoj 1874 取石子游戏 题解 & SG函数初探
[原题] 1874: [BeiJing2009 WinterCamp]取石子游戏 Time Limit: 5 Sec Memory Limit: 162 MB Submit: 334 Solved ...
- HDU 2516 取石子游戏 (博弈论)
取石子游戏 Problem Description 1堆石子有n个,两人轮流取.先取者第1次能够取随意多个,但不能所有取完.以后每次取的石子数不能超过上次取子数的2倍.取完者胜.先取者负输出" ...
- 【POJ】1067 取石子游戏(博弈论)
Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...
- vijos 1557:bzoj:1413: [ZJOI2009]取石子游戏
Description 在研究过Nim游戏及各种变种之后,Orez又发现了一种全新的取石子游戏,这个游戏是这样的: 有n堆石子,将这n堆石子摆成一排.游戏由两个人进行,两人轮流操作,每次操作者都可以从 ...
随机推荐
- 查找代码错误.java
class Other{ public int i; } class Something{ public static oivd main(String[]args){ Other o = new O ...
- [No000029]程序员的那些事儿 -- 皆大欢喜的加薪
我的朋友A君是个典型的.NET开发人员,技术不错,人品也不错,在一家小公司(姑且称为甲公司)做项目开发,是技术骨干. 3个月前,他找到我说想跳槽,让我帮忙介绍工作.我说为什么想跳了? 1. 为什么想离 ...
- Tree
//Header.h #ifndef _HEAD_ #define _HEAD_ #include <queue> #include <iostream> using name ...
- ExcelHelper
NPOI下载:http://pan.baidu.com/s/1JNAGm 最新封装类: 2016-03-14 1.添加对OleConn的读 private static string connstri ...
- 封装第三方jquery插件
需要自己编写 directives 的情况通常是当你使用了第三方的 jQuery 插件.因为插件在 AngularJS 之外对表单值进行更改,并不能即时反应到 Model 中.例如我们用得比较多的 j ...
- History 对象
History 对象 History 对象包含用户(在浏览器窗口中)访问过的 URL. History 对象是 window 对象的一部分,可通过 window.history 属性对其进行访问. 注 ...
- 深入理解Linux修改hostname(转载)
http://www.cnblogs.com/kerrycode/p/3595724.html http://www.centoscn.com/CentOS/config/2014/1031/4039 ...
- http协议(八)请求首部字段
请求首部字段 定义:请求首部字段是从客户端到服务器发送请求报文中所使用的字段,里面包含了附加信息.客户端信息以及对响应内容相关的优先级等内容 1.Accept 通知服务器用户代理可处理的媒体类型及媒体 ...
- 见鬼了,swiper
1.今天不知怎么swiper的onInit函数不起作用,怎么弄都不行: 把以前能行的案例的包都导进去还是不行,但是onSlideChangeEnd可以触发,晕死了.... 不,它触发了一次onInit ...
- 表单 - Form - EasyUI提供的表单异步提交
方案一 被提交的表单 <form id="loginForm" method="post"> <table align="cente ...