(HDU 3032) Nim or not Nim(SG函数、博弈)

思路

SG函数的值为所有后继状态的SG函数值中没有出现过的最小自然数。
根据nim博弈中SG函数的求法,如果被拆成了2堆,那SG值就是2堆SG值的异或(算作一个后继)

SG[0]=SG[1]=1

  • 石子数为 2 ,每次可取 0 ,1,个,还可以分解为(1,1)
    SG[2]=mex( 0 , 1 ) = 2;

  • 石子数为 3 ,每次可取 0 , 1 , 2 个,还可以分解成 (1,2),结果为SG [ 1 ]^SG [ 2 ] = 3
    SG[3]=mex ( 0 , 1 ,2 ,3 ) = 4;

  • 石子数为 4 ,每次可取 0 ,1 , 2 ,3(SG[3]=4) 个,还可以分解为 (1,3),结果为SG [ 1 ]^SG [ 3 ]= 2
    SG[4]=mex( 0,1,2,4 ) = 3;

打表代码

打表发现是以4位单位循环的

#include <bits/stdc++.h>
using namespace std;
int g[1010];
void init() {
    memset(g, -1, sizeof(g));
}
int getSG(int x) {
    if(g[x] != -1) return g[x];
    if(x == 0) return 0;
    if(x == 1) return 1;
    if(x == 2) return 2;
    int vis[1010];
    memset(vis, 0, sizeof(vis));
    for(int i = 1; i < x; i++) {
            int t = 0;
            int a = getSG(i), b = getSG(x-i);
            t ^= a;
            t ^= b;
            vis[t] = 1;
    }
    for(int i = 0; i < x; i++) vis[getSG(i)] = 1;
    for(int i = 0; ; i++) if(!vis[i])
        return i;
}
int main() {
    int n;
    init();
    for(int i = 1; i <= 100; i++) {
        g[i] = getSG(i);
        printf("%d %d %d\n", i, i % 8, g[i]);
    }
}

代码

#include <bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
const int INF=0x3f3f3f3f;
const int maxn=3e3+50;
const int Mod=1e9+7;
typedef long long ll;
using namespace std;

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
    #endif

    int n,cs;
    scanf("%d",&cs);
    while(cs--){
        scanf("%d",&n);
        int sg=0,t;
        for (int i=0;i<n;i++){
            scanf("%d",&t);
            int m=t%4;
            if (m==0) sg^=(t-1);
            else if(m==3) sg^=t+1;
            else sg^=t;
        }
        if (sg) puts("Alice");
        else puts("Bob");
    }


    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值