您的位置:首页 > 其它

HDU 1536 SG函数模板

2016-07-30 19:23 555 查看
题目:

多堆石子,先手胜为L,后手胜为W,

思路:

SG函数:

SG[i]=0表示先手必败,否则必胜

有多堆石子,每次对SG[k]的值亦或(^)最终为0则必败,否则必胜

#include<cstring>
#include<string>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#include<climits>
#include<set>
using namespace std;
#define mod 1000000007
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
typedef long long LL;

const int N = 10008;
int s[108],t;
int sg
;
bool Hash
;
void sg_solve(int *s,int t,int n)   //N求解范围 S[]数组是可以每次取的值,t是s的长度。
{
int i,j;
memset(sg,0,sizeof(sg));
for(i=1;i<=n;i++)
{
memset(Hash,0,sizeof(Hash));
for(j=0;j<t;j++)
if(i - s[j] >= 0)
Hash[sg[i-s[j]]] = 1;
for(j=0;j<=N;j++)
if(!Hash[j])
break;
sg[i] = j;
}
}

int main()
{
int i,j,n,m,h;
while(scanf("%d",&t),t)
{
string ans="";
for(i=0;i<t;i++)
scanf("%d",&s[i]);
sg_solve(s,t,N);
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&m);
int res = 0;
for(j=0;j<m;j++)
{
scanf("%d",&h);
res ^= sg[h];
}
ans+=res?'W':'L';
}
cout<<ans<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: