# hdu 2999 Stone Game, Why are you always there? (sg函数，线性串取石子)

Posted by 111qqz on Saturday, July 23, 2016

## TOC

hdu2999题目链接

``````/* ***********************************************
Author :111qqz
Created Time :2016年07月23日 星期六 15时02分17秒
File Name :code/hdu/2999.cpp
************************************************ */

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define fst first
#define sec second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ms(a,x) memset(a,x,sizeof(a))
typedef long long LL;
#define pi pair < int ,int >
#define MP make_pair

using namespace std;
const double eps = 1E-8;
const int dx4[4]={1,0,0,-1};
const int dy4[4]={0,-1,1,0};
const int inf = 0x3f3f3f3f;
const int N=2005;
int sg[N];
bool vis[N];
set<int>ok;
set<int>::iterator it ;
int n;
int q;

void sg_init()
{
ms(sg,0);

for ( int i = 1 ; i < N ; i++)
{
ms(vis,false);

for ( it = ok.begin() ; it!=ok.end() ; it++)
{
int tmp=*it;
tmp = i-tmp;
for ( int j = 0 ; tmp-j>= j ; j++)
vis[sg[j]^sg[tmp-j]] = true;
}

for ( int k = 0 ; ; k++)
if (!vis[k])
{
sg[i] = k;
break;
}
}
}
int main()
{
#ifndef  ONLINE_JUDGE
freopen("code/in.txt","r",stdin);
#endif

while (~scanf("%d",&n))
{
ok.clear();
//	    int mn = inf;
for ( int i =  1 ; i  <=  n;  i++)
{
int x;
scanf("%d",&x);
ok.insert(x);
//	mn = min(x,mn);

}
sg_init();
//	    for ( int i = 1; i <= 30 ; i++) cout<<"i:"<<i<<" sg[i]:"<<sg[i]<<endl;
scanf("%d",&q);
while (q--)
{
int x;
scanf("%d",&x);
if (sg[x])
{
puts("1");
}else
{
puts("2");
}
}
}

#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0;
}
``````

「真诚赞赏，手留余香」