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先进行操作,他想问你他是否有必胜策略,如 ...
随机推荐
- python编程之操作系统基础
操作系统发展史: 最早的计算机没有操作系统的概念:是使用打孔机和打孔卡片来进行计算的.只能一个一个处理. 后来产生了批处理系统,可以批量处理,进而加快计算的速度. 批处理系统:串行 数据处理速度加快 ...
- 扶桑号战列舰 (单调栈+线段树区间更新懒惰标记 or 栈)
传送门 •题目描述 题目描述 众所周知,一战过后,在世界列强建造超无畏级战列舰的竞争之中,旧日本海军根据“个舰优越主义”,建造了扶桑级战列舰,完工时为当时世界上武装最为强大的舰只. 同时,扶桑号战列舰 ...
- 原 在windows上创建文件名以“.”开头的文件
在实际项目开发中,我们会经常遇到以”.”开头的配置文件.例如我们如果需要通过配置文件给npm更换源就需要更改.npmrc文件,然而还有很多友友还不清楚如何创建一个类似的文件,今天就和大家分享下这个小技 ...
- JavaScript数据类型总结
1. 六种简单数据类型:Undefined.Null.Boolean.Number.String.Symbol(新增): 一种复杂数据类型:Object: (1)基本数据类型保存在栈内存中,是按值传 ...
- H3C VLSM
- 机器学习——集成学习之Bagging
整理自: https://blog.csdn.net/woaidapaopao/article/details/77806273?locationnum=9&fps=1 随机森林 1.随机森林 ...
- 【37.48%】【hdu 2587】How far away ?(3篇文章,3种做法,LCA之ST算法(RMQ))
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...
- vue移动端图片上传压缩
上传压缩方法 import {api} from '../../api/api.js'; import axios from 'axios'; export function imgPreview ( ...
- 【Linux】tar压缩解压缩笔记
tar -c, --create create a new archive(建立压缩档案) -x, --extract, --get extract files from an archive(解压) ...
- 多线程之美8一 AbstractQueuedSynchronizer源码分析<二>
目录 AQS的源码分析 该篇主要分析AQS的ConditionObject,是AQS的内部类,实现等待通知机制. 1.条件队列 条件队列与AQS中的同步队列有所不同,结构图如下: 两者区别: 1.链表 ...