http://acm.timus.ru/problem.aspx?space=1&num=1087
简单博弈 + dp(记忆化搜索)
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<vector> #include<queue> #include<map> #include<stack> #include<algorithm> #include<cmath> using namespace std; //#pragma comment(linker,"/STACK:1000000000,1000000000") #define LL long long const int INF=0x3f3f3f3f; const int N=10005; const int M=55; int win[N]; int choose[M]; int n,m; int dp(int x) { if(win[x]!=-1) return win[x]; if(x==0) { win[x]=1; return win[x]; } for(int i=0;i<m;++i) { if(x-choose[i]>=0&&dp(x-choose[i])==2) {win[x]=1;break;} } if(win[x]==-1) win[x]=2; return win[x]; } int main() { //freopen("data.txt","r",stdin); while(scanf("%d %d",&n,&m)!=EOF) { for(int i=0;i<m;++i) cin>>choose[i]; memset(win,-1,sizeof(win)); cout<<dp(n); } return 0; }