UVa 10404. Bachet's Game

题意为给出总石子数n,和m堆石子,两个人轮着取石子,每次只能从总石子中取m堆中的一堆的个数的石子,取走最后一个石子的胜。S先取,O后取。

用博弈的输赢观念+dp就可以了,由于记忆化搜索会爆栈,那就递推了,索性还好推……

不然就不会了……

#include<iostream>
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
int dp[1000010];
int a[20];
int n,m;
int main()
{
	int i,j;
	while(cin>>n>>m)
	{
		for(i=0;i<m;i++)
			cin>>a[i];
		for(i=0;i<=n;i++)
		{
			dp[i]=0;
			for(j=0;j<m;j++)
				if(i-a[j]>-1&&dp[i-a[j]]==0)
				{
					dp[i]=1;
					continue;
				}
		}
		if(dp[n]==1)
			cout<<"Stan wins"<<endl;
		else
			cout<<"Ollie wins"<<endl;
	}
}

Bachet‘s Game

Time Limit:6666MS   Memory Limit:Unknown   64bit IO Format:%lld & %llu

SubmitStatus

Description

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

Problem Setter: Piotr Rudnicki

Source

Root :: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: Mathematics :: Game Theory ::
Standard

Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 5. Dynamic Programming

Root :: Competitive Programming 2: This increases the lower bound of Programming Contests. Again (Steven & Felix Halim) :: Mathematics ::
Game Theory - Standard

Root :: Prominent Problemsetters :: Piotr Rudnicki

UVa 10404. Bachet's Game

时间: 2024-10-17 16:02:29

UVa 10404. Bachet's Game的相关文章

UVa 10404 Bachet&#39;s Game(DP)

题意   给你n个小石头  和一个数组a[m]   然后两个人轮流取石头  stan先取  olive后取  他们都完美发挥    谁取完最后一个石头谁就是赢家    感觉不是很容易看出来是dp题  令d[i]表示只有i个石子时谁赢   1表示stan赢  0表示olive赢 i-a[j]表示从i个石子一次取走a[j]个还剩下的    所以有  当(i-a[j]>0&&d[i-a[j]]=0)时  d[i]=1; #include<cstdio> #include<

【UVA】10404-Bachet&#39;s Game(动态规划)

如果d[i]是必胜态,那么d[i + V[j]]一定是必败态,反之亦然. 用d[i]代表棋子为i个是否为必胜态. 边界条件是d[i] = 1; 14139291 10404 Bachet's Game Accepted C++ 0.662 2014-09-03 09:44:48 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vecto

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

算法入门经典大赛 Dynamic Programming

111 - History Grading LCS 103 - Stacking Boxes 最多能叠多少个box DAG最长路 10405 - Longest Common Subsequence LCS 674 - Coin Change 全然背包求方案数 10003  - Cutting Sticks 区间DP dp[l][r]代表分割l到r的最小费用 116 - Unidirectional TSP 简单递推 输出字典序最小解 从后往前推 10131 - Is Bigger Smarte

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te

UVA 11014 - Make a Crystal(容斥原理)

UVA 11014 - Make a Crystal 题目链接 题意:给定一个NxNxN的正方体,求出最多能选几个整数点.使得随意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, y, z)三点都为k的倍数的点的个数(要扣掉一个原点O).那么全部点就是f(1),之后要去除掉共线的,就是扣掉f(2), f(3), f(5)..f(n).n为素数.由于这些素数中包括了合数的情况,而且这些点必定与f(1)除去这些点以外的点共线,所以扣掉.可是扣掉后会扣掉一些反复的.比方f(6)在f

[UVa] Palindromes(401)

UVA - 401 Palindromes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDED

uva 401.Palindromes

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=342 题目意思:给出一段字符串(大写字母+数字组成).判断是否为回文串 or 镜像串 or 回文镜像串 or 什么都不是.每个字母的镜像表格如下 Character Reverse Character Reverse Character Reverse A A M M Y Y B