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先进行操作,他想问你他是否有必胜策略,如 ...
随机推荐
- H5 移动端获取当前位置
3种方法:1.H5自带的方法,获取经纬度2.通过地图提供的JS.获取位置3.通过微信的API(这个需要公众号 / 小程序) 1.通过H5自带的获取经纬度的方法 优点: 需要引用的资源较少,H5自带的方 ...
- 【codeforces 766C】Mahmoud and a Message
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Java发送邮件Demo
就是个Demo,有使用Spring的东西 package xxxxxxx.common.utils; import org.springframework.mail.javamail.JavaMail ...
- CF241B Friends
CF241B Friends 和Tree and Xor思路一样CF1055F Tree and XOR 直接找到第k大val,可以直接建出trie,然后按位贪心 考虑比val大的数的和 还是用b[i ...
- vue 使用webpack打包后路径报错以及 alias 的使用
一.vue 使用webpack打包后路径报错(两步解决) 1. config文件夹 ==> index.js ==> 把assetsPublicPath的 '/ '改为 './' 2. b ...
- linux 延后执行
设备驱动常常需要延后一段时间执行一个特定片段的代码, 常常允许硬件完成某个任务. 在这一节我们涉及许多不同的技术来获得延后. 每种情况的环境决定了使用哪种技术最好; 我们全都仔细检查它们, 并且指出每 ...
- C# 通过 probing 指定 dll 寻找文件夹
在很大的项目开发,会发现项目引用的 dll 会很多,我想要按照不同的功能,将不同的 dll 放在不同的文件夹 简单的方法是通过修改 App.config 文件指定文件夹,如将文件移动到 abc\12 ...
- 2018.11.25 齐鲁工业大学ACM-ICPC迎新赛正式赛题解
整理人:周翔 A 约数个数(难) 解法1:苗学林 解法2:刘少瑞 解法3:刘凯 解法4:董海峥 B Alice And Bob(易) 解法1:周翔 解法2:苗学林 解法3:刘少瑞 C 黑白 ...
- 计算机网络OSI模型
一. 理想模型-OSI 7层模型 那么数据是如何传输的呢?看下图:上层的Header+DATA作为新的Data再次被封装,到达目的地后就像剥洋葱一样掐头(去尾)最终留下你所要传的数据. 各层功能详细阐 ...
- 防火墙TCP的单向控制
网络拓扑: 如上图所示,防火墙分为external/internal/dmz三个安全zone,其中10.133.1.100/32为dmz区的一台squid缓存服务器,10.158.1.10/32,10 ...