HDU - 3032 Nim or not Nim?

本文探讨了一种涉及石子堆的游戏策略问题,通过使用Sprague-Grundy定理(简称SG函数)来寻找最优解的方法。游戏由两名玩家轮流进行,目标是在轮到自己时拿走最后一颗石子。文章提供了具体的实现代码,并通过分析得出,当最终的异或和大于0时,先手玩家Alice获胜;反之,则后手玩家Bob获胜。

题目:有n堆石子,Alice和Bob轮流操作,Alice先手,每次游戏者有2种选择,要么拿走其中的任意数目的石子,要么将石子堆分成2堆,谁拿走最后的石子谁胜

思路:sg打表找规律

代码:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<algorithm>
#include<ctime>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<numeric>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define INF 0x3f3f3f3fn
#define mm(a,b) memset(a,b,sizeof(a))
#define PP puts("*********************");
template<class T> T f_abs(T a){ return a > 0 ? a : -a; }
template<class T> T gcd(T a, T b){ return b ? gcd(b, a%b) : a; }
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
// 0x3f3f3f3f3f3f3f3f
//0x3f3f3f3f

int sg[10]={0,1,1,2};
int arr[10]={1,0,0,-1};
int get(int x){
    if(x<=3) return sg[x];
    return x-arr[x%4];
}
int main(){

    int T,n,x;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        int ans=0;
        for(int i=1;i<=n;i++){
            scanf("%d",&x);
            ans=(ans^get(x));
        }
        if(ans>0) printf("Alice\n");
        else printf("Bob\n");
    }
    return 0;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值