10404 - Bachet's Game
Time limit: 6.666 seconds
博弈~
规则1:一个状态是必败状态当且仅当它所有后继是必败状态
规则2:一个状态是必胜状态当且仅当它至少有一个后继是必败状态
边界:这一题0是必败状态
思路:从下往上递推,don't you?
PS:递归会爆栈。
完整代码:
/*0.399s*/
#include<bits/stdc++.h>
using namespace std;
bool dp[1000005];
int a[11];
int main()
{
int n, m, i, j;
bool flag;
while (~scanf("%d%d", &n, &m))
{
for (i = 0; i < m; ++i)
scanf("%d", &a[i]);
sort(a, a + m);
for (i = 1; i <= n; ++i)
{
flag = false;
for (j = 0; j < m && i >= a[j]; ++j)
if (!dp[i - a[j]])
{
flag = true;
break;
}
dp[i] = flag;
}
puts(dp[n] ? "Stan wins" : "Ollie wins");
}
return 0;
}