HDU2999 Stone Game, Why are you always there?SG函数

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU2999 Stone Game, Why are you always there?SG函数相关的知识,希望对你有一定的参考价值。

Stone Game, Why are you always there?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1672 Accepted Submission(s): 578

Problem Description
“Alice and Bob are playing stone game…”
“Err… Feel bored about the stone game? Don’t be so, because stone game changes all the time!”
“What the hell are they thinking for?”
“You know, whenever Alice is trying to make fun of Bob, she asked him to play stone game with him.”
“Poor Bob… What’s the rule today?”
“It seems Alice only allows some fixed numbers of continuous stones can be taken each time. And they begin with one string of stones.”
“A string? Formed as a circle or a line?”
“A line.”
“Well, I think I may help Bob with that.”
“How?”
“I may tell him to skip this round if he has no chance to win.”
“Good idea maybe, I mean, Alice always let Bob play first, because she think herself is smart enough to beat Bob no matter how.”
“Yes, she’s actually right about herself. Let me see if Bob has a chance to win…”

Input
There are multiple test cases, for each test case:
The first line has a positive integer N (1<=N<=100).
The second line contains N positive integers, a1, a2 … an, separated by spaces, which indicate the fixed numbers Alice gives.
The third line, a positive integer M. (M<=1000)
Following M lines, one positive integer K (K<=1000) each line. K means in this round, the length of the stone string.

Output
For each K, output “1” if Bob has a chance to win, output “2” if Bob has no chance, or “0” if it’s undeterminable.

Sample Input
3
1 5 1
1
1

Sample Output
1

Source
2009 Multi-University Training Contest 19 - Host by BNU

问题链接HDU2999 Stone Game, Why are you always there?
问题简述:(略)
问题分析:博弈的SG函数问题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* HDU2999 Stone Game, Why are you always there? */

#include <bits/stdc++.h>

using namespace std;

const int N = 100;
const int M = 1000;
int n, a[N], sg[M + 1], s[M + 1];

void getSG()
{
    for (int i = 0; i <= M; i++) {
        memset(s, 0, sizeof s);
        for (int j = 0; j < n && a[j] <= i; j++)
            for (int k = i - a[j]; k >= 0; k--)
                s[sg[k] ^ sg[i - a[j] - k]] = 1;
        for (int j = 0; j <= M; j++)
            if (s[j] == 0) {
                sg[i] = j;
                break;
            }
    }
}

int main()
{
    while (~scanf("%d", &n)) {
        for (int i = 0; i < n; i++) scanf("%d", &a[i]);

        sort(a, a + n);

        getSG();

        int m, k;
        scanf("%d", &m);
        while (m--) {
            scanf("%d", &k);

            puts(sg[k] ? "1" : "2");
        }
    }

    return 0;
}

以上是关于HDU2999 Stone Game, Why are you always there?SG函数的主要内容,如果未能解决你的问题,请参考以下文章

HDU5973 Game of Geting Stone(威佐夫博弈)

HDU 4388 Stone Game II {博弈||找规律}

hdu 1729 Stone Game

HDU - 4388 Stone Game II

URAL 1180. Stone Game (博弈 + 规律)

CodeForces - 1538A Stone Game博弈