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.

InputInput 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)OutputFor 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 题目分析:正常的nim游戏规则,但是操作的过程中可以不取出石子,但是可以将石子一分为二。
思路分析:打表找一下所有数的sg,发现是有规律的
代码示例: 打表:
void init(){
sg[0] = 0, sg[1] = 1; for(int i = 2; i <= 100; i++){
memset(s, 0, sizeof(s));
for(int j = 0; j < i; j++){
s[sg[j]] = 1;
}
for(int j = 1; j <= (i/2); j++){
s[sg[j]^sg[i-j]] = 1;
}
for(int j = 0; ;j++){
if(!s[j]) {sg[i] = j; break;}
}
}
}

AC代码:

int x;
int n; int sg(int x){
int k = x%4;
if (k == 1 || k == 2) return x;
else if (k == 3) return x+1;
else return x-1;
} int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int t; cin >>t;
while(t--){
scanf("%d", &n);
int s = 0;
for(int i = 1; i <= n; i++){
scanf("%d", &x);
s ^= sg(x);
} if (s) printf("Alice\n");
else printf("Bob\n");
}
return 0;
}

sg函数的变形 - 可以将一堆石子分开的更多相关文章

  1. ACdream 1112 Alice and Bob (sg函数的变形+素数筛)

    题意:有N个数,Alice 和 Bob 轮流对这些数进行操作,若一个数 n=a*b且a>1,b>1,可以将该数变成 a 和 b 两个数: 或者可以减少为a或b,Alice先,问谁能赢 思路 ...

  2. Light OJ 1199 - Partitioning Game (博弈sg函数)

    D - Partitioning Game Time Limit:4000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  3. [BZOJ 1874] [BeiJing2009 WinterCamp] 取石子游戏 【博弈论 | SG函数】

    题目链接:BZOJ - 1874 题目分析 这个是一种组合游戏,是许多单个SG游戏的和. 就是指,总的游戏由许多单个SG游戏组合而成,每个SG游戏(也就是每一堆石子)之间互不干扰,每次从所有的单个游戏 ...

  4. sg函数与博弈论

    这个标题是不是看起来很厉害呢... 我们首先来看一个最简单的游戏.比如我现在有一堆石子,有p个,每次可以取走若干个(不能不取),不能取的人就输了. 现在假设有两个人要玩这个游戏,一个人先手,一个人后手 ...

  5. sg函数和nim游戏的关系

    sg函数和nim游戏的关系 本人萌新,文章如有错漏请多多指教-- 我在前面发了关于nim游戏的内容,也就是说给n堆个数不同的石子,每次在某个堆中取任意个数石子,不能取了就输了.问你先手是否必胜.然后只 ...

  6. 新年第一发--HDU1848--Fibonacci again and again(SG函数)

    Problem Description 任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的:F(1)=1;F(2)=2;F(n)=F(n-1)+F(n-2 ...

  7. 博弈论进阶之SG函数

    SG函数 个人理解:SG函数是人们在研究博弈论的道路上迈出的重要一步,它把许多杂乱无章的博弈游戏通过某种规则结合在了一起,使得一类普遍的博弈问题得到了解决. 从SG函数开始,我们不再是单纯的同过找规律 ...

  8. bzoj 1874 取石子游戏 题解 &amp; SG函数初探

    [原题] 1874: [BeiJing2009 WinterCamp]取石子游戏 Time Limit: 5 Sec  Memory Limit: 162 MB Submit: 334  Solved ...

  9. BZOJ 1874: [BeiJing2009 WinterCamp]取石子游戏 [Nim游戏 SG函数]

    小H和小Z正在玩一个取石子游戏. 取石子游戏的规则是这样的,每个人每次可以从一堆石子中取出若干个石子,每次取石子的个数有限制,谁不能取石子时就会输掉游戏. 小H先进行操作,他想问你他是否有必胜策略,如 ...

随机推荐

  1. SVN常用命令之checkout

    官方解释,请参考:http://www.subversion.org.cn/svnbook/nightly/svn.ref.svn.c.checkout.html 常用检出命令: svn co htt ...

  2. P1096 4个数的全排列

    题目描述 输入4个有序的个位数.按照字典序输出它们的全排列. 输入格式 输入四个数字a,b,c,d.(0<=a,b,c,d<10) 输出格式 输出它们的全排列.每个排列占一行.而且每个排列 ...

  3. H3C IPv6邻居发现协议

  4. H3C 使用tracert命令

  5. H3C保存当前配置--用户图示(console)以上

    <H3C>save         //此种保存只默认保存为Startup.cfg ,系统默认是加载此文件 The current configuration will be writte ...

  6. 2018-8-10-win10-uwp-获得缩略图

    title author date CreateTime categories win10 uwp 获得缩略图 lindexi 2018-08-10 19:16:51 +0800 2018-2-13 ...

  7. Team Foundation Server 2015使用教程【7】:权限为读取器的团队成员连接tfs及checkin操作

  8. boostrap-非常好用但是容易让人忽略的地方【4】:Font Awesome

    font-awesome基本用法 官方代码传送门 font-awesome在bootstrap中的特殊用法(这个才是重点) 要点归纳1(官方) 官方代码传送门 要点归纳2(我的) <a href ...

  9. TestStand 界面重置【小技巧】

    有几种情况可能会使用到这个功能: (1)当界面调整的很乱的时候 (2)当界面突然消失的时候(但是软件进程还在)--快捷键 Alt+V 会弹出菜单,再点击Reset UI Configuration即可 ...

  10. Linux 创建网络会话

    Linux 创建网络会话 RHEL7系统支持网络会话功能,允许用户在多个配置文件中快速切换(非常类似于firewalld防火墙服务中的区域技术).如果我们在公司网络中使用笔记本电脑时需要手动指定网络的 ...