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. vue通信、传值的方式

    原文博主地址:https://blog.csdn.net/qq_35430000/article/details/79291287 看完还是受益匪浅,讲得很详细..感谢!

  2. 一次接口压力测试qps极低原因分析及解决过程

    一次接口压力测试qps极低原因分析及解决过程 9-2日在做内部的性能测试相关培训时,发现注册接口压力测试qps极低(20左右),这个性能指标远不能达到上线标准 ,经过一系列调试,最后定位 98%的时间 ...

  3. js快速替换json里的key值

    需求是将b根据a的值替换对象中的key值 let a = ["code","name","date","font"]; ...

  4. JavaScript 判断对象中是否有某属性的常用方法

    一.点(.)或方框号([ ]) var obj = { test: '123' } obj.test obj['test'] 二.in 运算符 var obj = { test: '123' } te ...

  5. C# 线程参数

    . class ThreadSample { private readonly int _iterations; public ThreadSample(int iterations) { _iter ...

  6. Android 隐藏顶部菜单栏

    Android 隐藏状态栏 在Activity中: getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 在fragmen ...

  7. Java数据库操作学习

    JDBC是java和数据库的连接,是一种规范,提供java程序与数据库的连接接口,使用户不用在意具体的数据库.JDBC类型:类型1-JDBC-ODBC桥类型2-本地API驱动类型3-网络协议驱动类型4 ...

  8. Helm Chart 一键部署 Jenkins

    Jenkins Jenkins是一款开源 CI&CD 软件,用于自动化各种任务,包括构建.测试和部署软件.目前提供超过1000个插件来支持构建.部署.自动化, 满足任何项目的需要. Jenki ...

  9. 【一起学源码-微服务】Nexflix Eureka 源码十:服务下线及实例摘除,一个client下线到底多久才会被其他实例感知?

    前言 前情回顾 上一讲我们讲了 client端向server端发送心跳检查,也是默认每30钟发送一次,server端接收后会更新注册表的一个时间戳属性,然后一次心跳(续约)也就完成了. 本讲目录 这一 ...

  10. 如何使用JMX来管理程序?

    什么是JMX JMX,全称Java Management Extensions,用于我们管理和监控java应用程序.JMX有以下用途: 监控应用程序的运行状态和相关统计信息. 修改应用程序的配置(无需 ...