ZOJ 2083 Win the Game

6 篇文章 0 订阅

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;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值