24h購物| | PChome| 登入
2012-05-30 06:39:21| 人氣2,474| 回應0 | 上一篇 | 下一篇

[UVA][DP] 10404 - Bachet's Game

推薦 0 收藏 0 轉貼0 訂閱站台

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


狀態轉移的方程並不難理解, 贏會變輸, 輸會變成贏

#include <stdio.h>
char dp[1000005];
int main() {
int n, m, s[15], i, j;
while(scanf("%d %d", &n, &m) == 2) {
for(i = 0; i < m; i++)
scanf("%d", &s[i]);
dp[0] = 0;
for(i = 1; i <= n; i++) {
dp[i] = 0;
for(j = 0; j < m; j++) {
if(i-s[j] >= 0) {
if(dp[i-s[j]] == 0) {
dp[i] = 1;
break;
}
}
}
}
if(dp[n])
puts("Stan wins");
else
puts("Ollie wins");
}
return 0;
}
 


台長: Morris
人氣(2,474) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][Greedy] 10718 - Bit Mask
此分類上一篇:[UVA] 10700 - Camel trading

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文