Stone Game, Why are you always there? HDU - 2999(sg定理)

题意:给你n个数的集合,表示你每次取石子只能为集合里的数,然后给你一排石子,编号为1~n,每次你可以取相邻位置的连续石子(数量只能为集合里的数),注意石子的位置时不变的,比如把2拿走了,1和3还是不相邻的。问先手有没有机会赢。

思路:如果我们取靠边的x个石子那么就是转移成sg[i-x],如果我们取中间的石子,就变成的不相邻的两排,也就是把单一的游戏拆成了两个,然后用sg就好了

原文地址:https://blog.csdn.net/chy20142109/article/details/52145607

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 10010, INF = 0x7fffffff;
int sg[maxn], vis[maxn];
int a[maxn];
int main()
{
    int n, m;
    while(cin>> n)
    {
        for(int i=0; i<n; i++)
            cin>> a[i];
        sort(a, a+n);
        sg[0] = 0;
        for(int i=1; i<=1000; i++)
        {
            mem(vis, 0);
            for(int j=0; j<n; j++)
            {
                if(a[j] > i) break;
                vis[sg[i-a[j]]] = 1;
                if(i - a[j] > 1)
                {
                    for(int k=1; k<= i-a[j]-1; k++)
                        vis[sg[k] ^ sg[i-a[j]-k]] = 1;
                }
            }
            for(int j=0; ; j++)
                if(!vis[j])
                {
                    sg[i] = j;
                    break;
                }
        }
        cin>> m;
        for(int i=0; i<m; i++)
        {
            int temp;
            cin>> temp;
            if(sg[temp])
                cout<< 1 <<endl;
            else
                cout<< 2 <<endl;


        }


    }


    return 0;
}

 

posted @ 2018-07-19 15:39  WTSRUVF  阅读(153)  评论(0编辑  收藏  举报