Bachet's Game

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 ofm 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
乍一看是道博弈问题,其实可以转化为dp
从0开始倒推回去,如果当前是先手必败,那么推出去的任何一步都是先手必胜。而只有之前的所有可以推出当前状态的状态都是先手必胜时,那么当前状态时先手必败。
#include<iostream> #include<algorithm> #include<math.h> #include<cstdio> #include<string> #include<string.h> using namespace std; const int maxn = 1000010; int n, m, i, j, f[maxn], a[10]; int main(){ while (cin >> n >> m) { memset(f, 0, sizeof(f)); for (i = 0; i < m; i++) cin >> a[i]; for (i = 0; i <= n; i++) for (j = 0; j < m; j++) if (i - a[j] >= 0 && !f[i - a[j]]) f[i] = 1; if (f[n]) printf("Stan wins\n"); else printf("Ollie wins\n"); } return 0; }