ZOJ 2083 Win the Game

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2083

A and B play a game. There are n lines. A and B color them by turns (A first). One can only color a segment of a line, with length 2, which has not be colored, and the distance between a segment end point and the line’s end point is a interger. When one can not color, he loses.

Input
There are several test cases. The first line of a case is a positive interger n, the next line contain n positive intergers, represent the lengths of the n lines. The sum of the lengths do not exceed 50.

Output
If A can win output a “Yes”, else output a “No”.

Note: you should consider A and B a clever enough.

Sample Input
2
2 4
1
9

Sample Output
Yes
No

这个题目很有趣,有博弈知识,又有一点智商题的感觉。

题意为给你一段长为n的的线段。

两个游戏者轮流在一段长为2,未被染色的线段上涂色。

无法涂色的游戏者输。

题目有点灵活。关键在于怎么得到Sg函数值呢?

其实是这样的,对于一个长度为n的线段,我们枚举中间可能的连续的两个单位长度进行染色,然后判断是否存在一种染色使得染色后的状态为必败态就可以了。

不过要注意哦,染色后,原来的一段会变为两端(如果染色在端点,可以视其中一段为0),这样接下来的两端就需要分别染色了,所以根据博弈论的只是我们就知道,接下来的Sg函数值就是这两个长度所对应的Sg函数值的异或哦。然后用传统的Sg函数的Mex求法就可以了。

#include<iostream>
#include <algorithm>
#include <cstdio>
#include <string.h>
#include <cstring>
#define N 54
using namespace std; 
int  b[N];
bool flag[N];
void init()
{
    int i, j;
    b[0] = b[1] = 0;
    b[2] = 1;
    for (i = 3; i < N; i++)
    {
        memset(flag, 0, sizeof(flag));
        for (j = 1; j < i; j++)
        {
            flag[b[j - 1]^b[i - j - 1]] = true;
        }
        for (j = 0;; j++)
        {
            if (!flag[j])
            {
                b[i] = j;
                break;
            }
        }
    }
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("1.txt","r",stdin);  
#endif
    int n, i, temp, ans;
    init(); 
    while(~scanf("%d", &n))
    {
        ans = 0;
        for (i = 0; i < n; i++)
        {
            scanf("%d", &temp);
            ans ^= b[temp];
        }
        if (ans)
        {
            cout << "Yes\n";
        }
        else 
        {
            cout << "No\n";
        }
    }
    return 0;
} 

你可能感兴趣的:(ZOJ)