Problem B: Bachet's Game

Bachet's game is probably known to all but probably not by this name. Initially there are  n  stones on the table. There are two players Stan and Ollie, who move alternately. Stan always starts. The legal moves consist in removing at least one but not more than  k  stones from the table. The winner is the one to take the last stone.

Here we consider a variation of this game. The number of stones that can be removed in a single move must be a member of a certain set of m numbers. Among the m numbers there is always 1 and thus the game never stalls.

Input

The input consists of a number of lines. Each line describes one game by a sequence of positive numbers. The first number is  n  <= 1000000 the number of stones on the table; the second number is  m  <= 10 giving the number of numbers that follow; the last  m  numbers on the line specify how many stones can be removed from the table in a single move.

Input

For each line of input, output one line saying either  Stan wins  or  Ollie wins  assuming that both of them play perfectly.

Sample input

20 3 1 3 8
21 3 1 3 8
22 3 1 3 8
23 3 1 3 8
1000000 10 1 23 38 11 7 5 4 8 3 13
999996 10 1 23 38 11 7 5 4 8 3 13

Output for sample input

Stan wins
Stan wins
Ollie wins
Stan wins
Stan wins
Ollie wins

题意:给定n个石头,和m种去除石头的方式,每种方式可以去除一定量的石头, 现在Stan(简称S),Ollie(简称O),S先手,O后手,每次每个人能选择一种去除石头的方式,谁去除最后一堆谁就赢了。要求出必胜之人是谁。

思路:一开始没头绪,以为是博弈。没想出好的思路。由于n很大,去遍历状态肯定超时。之后看了别人的题解,才发现不错的dp思路:用一个dp数组记录,对于先手者能取到的记录为1,后手者为0,初始都为0,遍历1到n,如果dp[i]为0,说明上一手是后手取得,这样先手就能取,把dp[i]变为1,由于是从1 到 n,这样每个状态记录时,前面的都已经记录好了,所以是可行的。这样最后只需要判断dp[n]是1,还是0,就可以判断是先手胜还是后手胜了。

状态转移方程为:if (i - move[j] >= 0 && !dp[i - move[j]])  dp[i] = 1。

代码:

#include <stdio.h>
#include <string.h> int n, m, move[15], dp[1000005], i, j; int main() {
while (~scanf("%d", &n)) {
memset(dp, 0, sizeof(dp));
scanf("%d", &m);
for (i = 0; i < m; i ++) {
scanf("%d", &move[i]);
}
for (i = 1; i <= n; i ++)
for (j = 0; j < m; j ++) {
if (i - move[j] >= 0 && !dp[i - move[j]]) {
dp[i] = 1;
break;
}
}
if (dp[n])
printf("Stan wins\n");
else
printf("Ollie wins\n");
}
return 0;
}

UVA 10404 Bachet's Game(dp + 博弈?)的更多相关文章

  1. uva 10404 Bachet's Game(完全背包)

    题目连接:10404 - Bachet's Game 题目大意:由一堆石子, 给出石子的总数, 接下来由stan和ollie两个人玩游戏,给出n, 在给出n种取石子的方法(即为每次可取走石子的数量), ...

  2. UVa 12525 Boxes and Stones (dp 博弈)

    Boxes and Stones Paul and Carole like to play a game with S stones and B boxes numbered from 1 to B. ...

  3. codevs 1421 秋静叶&秋穣子(树上DP+博弈)

    1421 秋静叶&秋穣子   题目描述 Description 在幻想乡,秋姐妹是掌管秋天的神明,作为红叶之神的姐姐静叶和作为丰收之神的妹妹穰子.如果把红叶和果实联系在一 起,自然会想到烤红薯 ...

  4. hdu6199 gems gems gems dp+博弈

    /** 2017 ACM/ICPC Asia Regional Shenyang Online 解题报告 题目:hdu6199 gems gems gems 链接:http://acm.hdu.edu ...

  5. UVA.10066 The Twin Towers (DP LCS)

    UVA.10066 The Twin Towers (DP LCS) 题意分析 有2座塔,分别由不同长度的石块组成.现在要求移走一些石块,使得这2座塔的高度相同,求高度最大是多少. 问题的实质可以转化 ...

  6. POJ 2068 NIm (dp博弈,每个人都有特定的取最大值)

    题目大意: 有2n个人,从0开始编号,按编号奇偶分为两队,循环轮流取一堆有m个石子的石堆,偶数队先手,每个人至少取1个,至多取w[i]个,取走最后一个石子的队伍输.问偶数队是否能赢. 分析: 题目数据 ...

  7. UVA 10003 Cutting Sticks 区间DP+记忆化搜索

    UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的 ...

  8. UVA 10891 区间DP+博弈思想

    很明显带有博弈的味道.让A-B最大,由于双方都采用最佳策略,在博弈中有一个要求时,让一方的值尽量大.而且由于是序列,所以很容易想到状态dp[i][j],表示序列从i到j.结合博弈中的思想,表示初始状态 ...

  9. UVA - 1625 Color Length[序列DP 代价计算技巧]

    UVA - 1625 Color Length   白书 很明显f[i][j]表示第一个取到i第二个取到j的代价 问题在于代价的计算,并不知道每种颜色的开始和结束   和模拟赛那道环形DP很想,计算这 ...

随机推荐

  1. 安装Linux_[CentOS]系统

    Lunx操作系统安装 虚拟机:VirtualBox. 操作系统的选择:CentOS 6.3. (64位/32位都可). (CentOS:诞生于社区的企业级操作系统). Install Or Upgra ...

  2. gulp工作流

    小屁活动使用 gulp+less gulpfile.js var gulp = require('gulp'), cssmin = require('gulp-minify-css'), less = ...

  3. JSTL解析——001

    JSTL 全称jsp standard tag library ,即jsp标准标签库. 是不是想问标签是什么东西? 标签就是jsp专门用于显示数据的,可重复利用的类库: 是不是想问标签由那些部分组成的 ...

  4. 几款屏幕录制软件 ActivePresente

    几款屏幕录制软件,最强大是  ActivePresenter ,免费版, 足以应对我们日常需求.列表如下 支持系统:W-Windows,L-Linux,M-Mac 软件 格式 W L M 免费 说明 ...

  5. ARP欺骗,骗你没商量

    今天BOSS让我总结ARP欺骗的原理和防范策略,在这里把总结的结果贴出来吧.求人品,求速转正. ARP原理: 在局域网内部,各主机之间以MAC地址作为标识通信对象的标志.然而,有时通信发起的主机并不知 ...

  6. Api之Cors跨域以及其他跨域方式

    Web Api之Cors跨域以及其他跨域方式(三)   我们知道ajax不能跨域访问,但是有时我们确实需要跨域访问获取数据,所以JSONP就此诞生了,其本质使用的是Script标签,除JSONP以外还 ...

  7. frontend http 前端名字定义问题

    https://www.winfae.com/admin/api/menu haproxy 日志: Jun 24 13:04:49 localhost haproxy[14817]: 115.236. ...

  8. junit4同一时候測试多个測试类

    两个分别须要的測试类 TestSuit001 package com.test.junit; import org.junit.Test; public class TestSuit001 { @Te ...

  9. 为了树莓派IIraspberrypi安装emacs+ecb+cedet+session+color-theme+cscope+linum

    类似这篇文章写的不多,为了避免以后大家转来转去而忽略了写文章的时间,这些特别加上是2014年6月28日,省的对不上一些软件的版本号(下文中有些"最新"的说法就相应这个时间).假设转 ...

  10. Android开发周报:Flyme OS开源、经典开源项目解析

    Android开发周报:Flyme OS开源.经典开源项目解析 新闻 <魅族Flyme OS源码上线Github> :近日魅族正式发布了MX5,并且在发布会上,魅族还宣布Flyme OS开 ...