简单的博弈DP
爆搜改成记忆化搜索会爆栈,改成递推就好了。稍微用位运算优化了一下。。不过还是很慢
#include <cstdio> #include <cstring> #include <algorithm> #define Win 0 #define Lose 1 using namespace std; int S[11],N,M; char note[100001]; inline char dp() { int n,p,i,j; for(i = 0;i < S[0];i++) note[i] = note[i] = Lose; for(i = S[0];i <= N;i++) { note[i] = 0; for(j = 0;j < M && S[j] <= i && !note[i];j++) note[i] |= note[i - S[j]]; note[i] ^= 1; } return note[N]; } int main() { int c = 1; while(scanf("%d%d",&N,&M),M || N) { for(int i = 0;i < M;i++) scanf("%d",S + i); sort(S,S + M); char ret = dp(); printf("Case %d: ",c++); if(ret == Win) printf("Yes "); else printf("No "); } return 0; }