hdu 1536 (S-Nim) SG解法

来源:互联网 发布:网络扫描工具有什么用 编辑:程序博客网 时间:2024/05/16 07:58

这题折腾了好久,一直不知道怎么求解SG,感觉用递归的方法会超时,最后看代码,采用记忆某些已知的SG的方式,递归求解。其中还爆内存两次,是因为递归中的数组空间开辟过大,递归层次多了,很容易爆的。

code:

#include <iostream>#include <algorithm>using namespace std;int SG[10001];int setNum;int set[101];int M,N;int getSG(int value){int i =0 ;bool mex[101]={0};for (i = 0 ; i < setNum ; i++){int temp = value - set[i];if (temp < 0)break;if (SG[temp] == -1)SG[temp] = getSG(temp);mex[SG[temp]] = true;}for (i = 0 ; ; i++)if (!mex[i])break;return i;}int main(){int temp ;while (cin>>setNum && setNum){   memset(SG,-1,sizeof(SG));SG[0]=0;for(int i =0 ; i < setNum ; i++){cin>>set[i];}        sort(set,set+setNum);cin>>M;while(M--){int ans =0 ;cin>>N;for (int i =0 ; i < N ; i++){   cin>>temp;if(SG[temp] == -1)SG[temp] = getSG(temp);ans^=SG[temp];}if (ans)cout<<"W";else cout<<"L";}cout<<endl;}return 0;}


原创粉丝点击