poj 2234基础Nim博弈||sg博弈

poj 终于突破200大关了,mark一下。hdu也快要300了。加油

http://poj.org/problem?id=2234

题意:

有n堆石子,每人每次可以从一堆中拿走任意多个,两人轮流操作,谁无子可取谁输。输入n堆石子各自的数量,输出先手是否能赢。

分析:NP问题,必胜态N(next player wins),必败态P(previous player wins)

如果某状态的直接后继中有必败态那么它一定是必胜态,否则为必败态。

SG函数。设函数g(x)。我们先把所有的最终局面(最终局面均为必败P局面)g(x)赋值为0。然后所有其他局面g(x)等于其直接后继状态中没有出现过的最小自然数。这样一来所有是g(x)=0的状态就是必败态,其他为必胜态。

根据定理:有这样一个游戏,是多个游戏共同进行,每个游戏都执行到底时才算整个游戏结束,每次一个选手可以把一个游戏进行一步。对于这样的游戏它的某状态的g(x)值,为每个子游戏的现在所处的状态的g(x)值抑或起来的结果。

所以对于本题,我们只需要研究一堆石子的g(x)的规律即可得出若干堆石子共同进行的胜败。

对于一个有n个石子的堆,其开始状态(有n个石子,没有被取过)g(x)=n;

View Code
// I'm lanjiangzhou
//C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
//C++
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <cctype>
#include <stack>
#include <string>
#include <list>
#include <queue>
#include <map>
#include <vector>
#include <deque>
#include <set>
using namespace std;

//*************************OUTPUT*************************
#ifdef WIN32
#define INT64 "%I64d"
#define UINT64 "%I64u"
#else
#define INT64 "%lld"
#define UINT64 "%llu"
#endif

//**************************CONSTANT***********************
#define INF 0x3f3f3f3f

// aply for the memory of the stack
//#pragma comment (linker, "/STACK:1024000000,1024000000")
//end


const int maxn =100+10;
int a[maxn];
int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        int t=0;
        int flag=0;
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
            t=(t^a[i]);
            if(a[i]>1){
                flag++;
            }
        }
        if(t==0){
            if(flag) printf("No\n");
            else printf("Yes\n");
        }
        else if(t){
            if(flag) printf("Yes\n");
            else printf("No\n");
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值