原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3032

Nim or not Nim?

Problem Description

Nim is a two-player mathematic game of strategy in which players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects provided they all come from the same heap.

Nim is usually played as a misere game, in which the player to take the last object loses. Nim can also be played as a normal play game, which means that the person who makes the last move (i.e., who takes the last object) wins. This is called normal play because most games follow this convention, even though Nim usually does not.

Alice and Bob is tired of playing Nim under the standard rule, so they make a difference by also allowing the player to separate one of the heaps into two smaller ones. That is, each turn the player may either remove any number of objects from a heap or separate a heap into two smaller ones, and the one who takes the last object wins.

Input

Input contains multiple test cases. The first line is an integer 1 ≤ T ≤ 100, the number of test cases. Each case begins with an integer N, indicating the number of the heaps, the next line contains N integers s[0], s[1], …., s[N-1], representing heaps with s[0], s[1], …, s[N-1] objects respectively.(1 ≤ N ≤ 10^6, 1 ≤ S[i] ≤ 2^31 - 1)

Output

For each test case, output a line which contains either “Alice” or “Bob”, which is the winner of this game. Alice will play first. You may asume they never make mistakes.

Sample Input

2
3
2 2 3
2
3 3

Sample Output

Alice
Bob

题解

标准的Lasker’s博弈

将一堆石子分成若干堆就相当于重开了两个游戏,原游戏的SGSG\mathcal{SG}值等于分裂出去的两个游戏的SGSG\mathcal{SG}值得异或和,我们可以通过将各个SGSG\mathcal{SG}值异或在一起来判定胜负,SGSG\mathcal{SG}为0先手必败,反之先手必胜。

然而事实是直接求SGSG\mathcal{SG}的复杂度爆炸,我们选择打表找规律:

sg[0]=0
sg[1]=1
sg[2]=2
sg[3]=4
sg[4]=3
sg[5]=5
sg[6]=6
sg[7]=8
sg[8]=7
sg[9]=9
sg[10]=10
sg[11]=12
sg[12]=11
sg[13]=13
sg[14]=14
sg[15]=16
sg[16]=15
sg[17]=17
sg[18]=18
sg[19]=20
sg[20]=19
sg[21]=21
sg[22]=22
sg[23]=24
sg[24]=23
sg[25]=25
sg[26]=26
sg[27]=28
sg[28]=27
sg[29]=29
sg[30]=30
sg[31]=32
sg[32]=31
sg[33]=33
sg[34]=34
sg[35]=36
sg[36]=35
sg[37]=37
sg[38]=38
sg[39]=40
sg[40]=39
sg[41]=41
sg[42]=42
sg[43]=44
sg[44]=43
sg[45]=45
sg[46]=46
sg[47]=48
sg[48]=47
sg[49]=49
sg[50]=50
sg[51]=52
sg[52]=51
sg[53]=53
sg[54]=54
sg[55]=56
sg[56]=55
sg[57]=57
sg[58]=58
sg[59]=60
sg[60]=59
sg[61]=61
sg[62]=62
sg[63]=64
sg[64]=63
sg[65]=65
sg[66]=66
sg[67]=68
sg[68]=67
sg[69]=69
sg[70]=70
sg[71]=72
sg[72]=71
sg[73]=73
sg[74]=74
sg[75]=76
sg[76]=75
sg[77]=77
sg[78]=78
sg[79]=80
sg[80]=79
sg[81]=81
sg[82]=82
sg[83]=84
sg[84]=83
sg[85]=85
sg[86]=86
sg[87]=88
sg[88]=87
sg[89]=89
sg[90]=90
sg[91]=92
sg[92]=91
sg[93]=93
sg[94]=94
sg[95]=96
sg[96]=95
sg[97]=97
sg[98]=98
sg[99]=100
sg[100]=99

发现当nnn为4的倍数时SG[n]=n−1" role="presentation" style="position: relative;">SG[n]=n−1SG[n]=n−1\mathcal{SG}[n]=n-1,当n mod 4=3nmod4=3n\ mod\ 4=3时,SG[n]=n+1SG[n]=n+1\mathcal{SG}[n]=n+1。

愉快AC

代码

打表:

#include<bits/stdc++.h>
using namespace std;
const int M=1e5;
int sg[M];
int g(int x)
{if(sg[x]>=0)return sg[x];bool mex[105];int hh=0;memset(mex,0,sizeof(mex));for(int i=0;i<x;++i)mex[g(i)]=1;for(int i=x/2;i>=1;--i){hh=g(i);hh^=g(x-i);mex[hh]=1;}for(int i=0;;++i)if(!mex[i])return sg[x]=i;
}
int main()
{memset(sg,-1,sizeof(sg));for(int i=0;i<=100;++i)printf("sg[%d]=%d\n",i,g(i));
}

正解:

#include<bits/stdc++.h>
using namespace std;
int n,x;
void ac()
{int ans=0;scanf("%d",&n);for(int i=1;i<=n;++i){scanf("%d",&x);if(x%4==0)ans^=(x-1);else if(x%4==3)ans^=(x+1);else ans^=x;}if(ans)printf("Alice\n");else printf("Bob\n");
}
int main()
{int T;scanf("%d",&T);for(int i=1;i<=T;++i)ac();return 0;
}

HDU3032 Nim or not Nim?相关推荐

  1. Nim问题和阶梯Nim(staircase nim)

    Nim问题和阶梯Nim(staircase nim) Nim问题: 有若干堆石子,每堆石子的数量都是有限的,合法的移动是"选择一堆石子并拿走若干颗(不能不拿)",如果轮到某个人时所 ...

  2. LeetCode 292 Nim Game(Nim游戏)

    翻译 你正在和你的朋友们玩下面这个Nim游戏:桌子上有一堆石头,每次你从中去掉1-3个.谁消除掉最后一个石头即为赢家.你在取出石头的第一轮.你们中的每一个人都有着聪明的头脑和绝佳的策略.写一个函数来确 ...

  3. HDU 3032 Nim or not Nim?

    /* g(0)=0,g(1)=1. 状态2的后继有0,1和(1,1),它们的SG函数值分别是0,1和0, 所以g(2)=2.状态3的后继有0,1,2和(1,2),它们的SG函数 值分别是0,1,2和3 ...

  4. leetcode 292. Nim Game | 292. Nim 游戏(DP->数学推理)

    题目 https://leetcode-cn.com/problems/nim-game/ 题解 本题实际上是一个需要分析的数学题.如果第一时间没有发现规律的话,可以尝试先用递归法,暴力输出前几个,观 ...

  5. AcWing 892. 台阶-Nim游戏(nim博弈变种)

    题目链接 https://www.acwing.com/problem/content/description/894/ 思路 先说结论:我们将奇数位置上的石子数异或起来如果不为0则先手必胜 原理: ...

  6. AcWing 891. Nim游戏(nim博弈)

    题目链接 https://www.acwing.com/problem/content/893/ 思路 这个题目需要清楚一个概念: 必胜态:我们能通过一个操作使得局面变成必败态 必败态:无论如何操作都 ...

  7. 【BZOJ2819】Nim 树状数组+LCA

    [BZOJ2819]Nim Description 著名游戏设计师vfleaking,最近迷上了Nim.普通的Nim游戏为:两个人进行游戏,N堆石子,每回合可以取其中某一堆的任意多个,可以取完,但不可 ...

  8. 博弈——Nim博弈(hdu2176,1850,1851,1907,1849)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2176 http://acm.hdu.edu.cn/showproblem.php?pid=1850 ...

  9. Nim博弈和威佐夫博弈 Return of the Nim

    Nim博弈 Nim游戏的概述: 还记得这个游戏吗? 给出n列珍珠,两人轮流取珍珠,每次在某一列中取至少1颗珍珠,但不能在两列中取.最后拿光珍珠的人输. 后来,在一份资料上看到,这种游戏称为" ...

  10. 博弈论(Nim游戏、有向图游戏之SG函数)

    这里写目录标题 经典NIM游戏 Nim游戏属于公平组合游戏ICG 有向图游戏(SG函数) Mex运算 SG函数 单个有向图(一堆石子) 求SG值(记忆化递归) 有向图游戏的和 ,(多个有向图(多堆石子 ...

最新文章

  1. wpa_supplicant学习
  2. uva 11978 Fukushima Nuclear Blast (二分+多边形与圆交)
  3. windows系统中hosts文件位置
  4. CentOS 7安装教程
  5. 持续提高 Android 应用的安全性与性能
  6. EntityFramework进阶——事务
  7. enter 默认搜索
  8. 21天Jmeter打卡Day10线程用户之setUp和tearDown
  9. oracle中触发器的语法,Oracle 触发器语法及实例
  10. 有生成的日志的监控电脑性能的软件吗_全链路监控:方案概述与对比,看完你就懂...
  11. linux音频声卡 pulseaudio服务
  12. 从零教新手开始学做游戏辅助系列(一)
  13. Java基础知识和进阶
  14. python+selenium之元素、下拉列表的定位
  15. oracle查看某个分区的数据,查看oracle表的分区信息
  16. C语言-03-基本数据类型及输入输出函数
  17. pyboard呼吸灯代码分享
  18. c++语言 构造函数,C++中构造函数的写法
  19. 管理学院人工智能蹭课一
  20. 前端下载excel文件功能的三种方法

热门文章

  1. 导入es数据_有道精品课实时数据中台建设实践
  2. 对象三大特性:封装、继承、多态。通俗易懂!!看完还不懂来打我!!!超详细!!涉及各种重要基础
  3. 《Web漏洞防护》读书笔记——第9章,XSS防护
  4. php对象在内存中的分配
  5. 使用FileUpload控件上传文件时对文件大小的限制
  6. 【apicloud问题解决记录】键盘弹出监听处理以及头部底部的黑色闪屏现象
  7. C# 控制台如何播放音频文件
  8. shell command cat/find/tr/mkdir
  9. c++ 接口继承和实现继承
  10. nginx/windows: nginx多虚拟主机配置