Bachet's Game - UVa 10404 dp博弈论

来源:互联网 发布:关于网络暴力的名言 编辑:程序博客网 时间:2024/06/04 23:23

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 821 3 1 3 822 3 1 3 823 3 1 3 81000000 10 1 23 38 11 7 5 4 8 3 13999996 10 1 23 38 11 7 5 4 8 3 13

Output for sample input

Stan winsStan winsOllie winsStan winsStan winsOllie wins

题意:一共有n个石子,每人每次可以取规定中的一种数量的石子,问先手的胜负。

思路:对于i个石子,如果i-num[j]都是必胜的情况,那么i是必败的,如果其中有一种是必败的,那么i是必胜的,然后从小到大递推。

AC代码如下:

#include<cstdio>#include<cstring>using namespace std;int dp[1000010],num[15];int main(){ int n,m,i,j,k;  while(~scanf("%d%d",&n,&m))  { for(i=1;i<=m;i++)     scanf("%d",&num[i]);    dp[0]=1;    for(i=1;i<=n;i++)    { k=0;      for(j=1;j<=m;j++)       if(num[j]<=i)        k+=dp[i-num[j]];      if(k==0)       dp[i]=1;      else       dp[i]=0;    }    if(dp[n]==0)     printf("Stan wins\n");    else     printf("Ollie wins\n");  }}



0 0
原创粉丝点击