sg函数的变形 - 可以将一堆石子分开
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.
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函数的变形 - 可以将一堆石子分开的更多相关文章
- ACdream 1112 Alice and Bob (sg函数的变形+素数筛)
题意:有N个数,Alice 和 Bob 轮流对这些数进行操作,若一个数 n=a*b且a>1,b>1,可以将该数变成 a 和 b 两个数: 或者可以减少为a或b,Alice先,问谁能赢 思路 ...
- Light OJ 1199 - Partitioning Game (博弈sg函数)
D - Partitioning Game Time Limit:4000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- [BZOJ 1874] [BeiJing2009 WinterCamp] 取石子游戏 【博弈论 | SG函数】
题目链接:BZOJ - 1874 题目分析 这个是一种组合游戏,是许多单个SG游戏的和. 就是指,总的游戏由许多单个SG游戏组合而成,每个SG游戏(也就是每一堆石子)之间互不干扰,每次从所有的单个游戏 ...
- sg函数与博弈论
这个标题是不是看起来很厉害呢... 我们首先来看一个最简单的游戏.比如我现在有一堆石子,有p个,每次可以取走若干个(不能不取),不能取的人就输了. 现在假设有两个人要玩这个游戏,一个人先手,一个人后手 ...
- sg函数和nim游戏的关系
sg函数和nim游戏的关系 本人萌新,文章如有错漏请多多指教-- 我在前面发了关于nim游戏的内容,也就是说给n堆个数不同的石子,每次在某个堆中取任意个数石子,不能取了就输了.问你先手是否必胜.然后只 ...
- 新年第一发--HDU1848--Fibonacci again and again(SG函数)
Problem Description 任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的:F(1)=1;F(2)=2;F(n)=F(n-1)+F(n-2 ...
- 博弈论进阶之SG函数
SG函数 个人理解:SG函数是人们在研究博弈论的道路上迈出的重要一步,它把许多杂乱无章的博弈游戏通过某种规则结合在了一起,使得一类普遍的博弈问题得到了解决. 从SG函数开始,我们不再是单纯的同过找规律 ...
- bzoj 1874 取石子游戏 题解 & SG函数初探
[原题] 1874: [BeiJing2009 WinterCamp]取石子游戏 Time Limit: 5 Sec Memory Limit: 162 MB Submit: 334 Solved ...
- BZOJ 1874: [BeiJing2009 WinterCamp]取石子游戏 [Nim游戏 SG函数]
小H和小Z正在玩一个取石子游戏. 取石子游戏的规则是这样的,每个人每次可以从一堆石子中取出若干个石子,每次取石子的个数有限制,谁不能取石子时就会输掉游戏. 小H先进行操作,他想问你他是否有必胜策略,如 ...
随机推荐
- H3C 路由器单跳操作
- javascript异步编程 Async/await
Async/await Async/await 在学习他之前应当补充一定的 promise 知识 它是一种与 promise 相配合的特殊语法,目前被认为是异步编程的终级解决方案 值得我们每一个人学习 ...
- 2019-8-31-C#-获取-PC-序列号
title author date CreateTime categories C# 获取 PC 序列号 lindexi 2019-08-31 16:55:58 +0800 2018-7-30 10: ...
- 2019-8-31-win10-uwp-使用-WinDbg-调试
title author date CreateTime categories win10 uwp 使用 WinDbg 调试 lindexi 2019-08-31 10:30:35 +0800 201 ...
- CF1208
CF1208 打的话貌似能够涨分的样子? A 水题 B 枚举左端点,看看右端点的最近位置 开一个类似于桶的东西维护一下上一次出现位置 左端点左边就删掉,否则就要将上一次出现的位置删掉 时间复杂度\(O ...
- Curator源码阅读 - ConnectionState的管理与监听
看看Curator框架 为实现对 连接状态ConnectionState的监听,都是怎么构造框架的.后面我们也可以应用到业务的各种监听中. Curator2.13实现 接口 Listener List ...
- Flutter TextField设置默认值默认值和光标位置
主要通过controller 实现,具体代码如下 TextField( //输入键盘类型 keyboardType: TextInputType.text, autofocus: true, deco ...
- UTF-8、UTF-16、UTF-32编码的相互转换(不使用现成的函数)
最近在考虑写一个可以跨平台的通用字符串类,首先需要搞定的就是编码转换问题. vs默认保存代码文件,使用的是本地code(中文即GBK,日文即Shift-JIS),也可以使用带BOM的UTF-8.gcc ...
- Python4_数据库相关操作
====================================================== 参考链接: PyCharm IDE 链接sqlite.建表.添加.查询数据:https:/ ...
- MySQL Server逻辑架构
1.MySQL基础 MySQL是一个开放源代码的关系数据库管理系统.原开发者为瑞典的MySQL AB公司,最早是在2001年MySQL3.23进入到管理员的视野并在之后获得广泛的应用. 2008年My ...