Day11 - M - Nim or not Nim? HDU - 3032
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 思路:Multi-SG问题,我们打表找规律,然后用SG函数即可
#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x)&(-x))
typedef long long LL; const int maxm = ; int sg[maxm];
bool vis[maxm]; void run_case() {
sg[] = ;
for(int i = ; i < maxm; ++i) {
memset(vis, , sizeof(vis));
for(int j = ; j <= i; ++j) {
vis[sg[j]] = true;
if(j != i && j != ) vis[sg[j]^sg[i-j]] = true;
} for(int j = ;;++j) {
if(!vis[j]) {
sg[i]=j;
break;
}
}
}
for(int i = ; i < maxm; ++i)
cout << i << " " << sg[i] << "\n";
} int main() {
ios::sync_with_stdio(false), cin.tie();
//int t; cin >> t;
//while(t--)
run_case();
cout.flush();
return ;
}
打表
打表后可以发现规律,每4个一个轮回,1234 就是1243 5678就是5687 规律很显然
#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x)&(-x))
typedef long long LL; int getsg(int x) {
if(x% == ) return x-;
else if(x% == ) return x+;
return x;
} void run_case() {
int n, ans = , val;
cin >> n;
while(n--) {
cin >> val;
ans ^= getsg(val);
}
if(!ans) cout << "Bob\n";
else cout << "Alice\n";
} int main() {
ios::sync_with_stdio(false), cin.tie();
int t; cin >> t;
while(t--)
run_case();
cout.flush();
return ;
}
Day11 - M - Nim or not Nim? HDU - 3032的更多相关文章
- hdu 3032 Nim or not Nim? (SG函数博弈+打表找规律)
Nim or not Nim? Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Sub ...
- hdu 3032 Nim or not Nim? sg函数 难度:0
Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- HDU 3032 Nim or not Nim? (sg函数求解)
Nim or not Nim? Problem Description Nim is a two-player mathematic game of strategy in which players ...
- HDU 3032 Nim or not Nim? (sg函数)
Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- HDU 3032 Nim or not Nim?(博弈,SG打表找规律)
Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- HDU 3032 Nim or not Nim?(Multi_SG,打表找规律)
Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- HDU 5795 A Simple Nim(简单Nim)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- HDU 3032 multi-sg 打表找规律
普通NIM规则加上一条可以分解为两堆,标准的Multi-SG游戏 一般Multi-SG就是根据拓扑图计算SG函数,这题打表后还能发现规律 sg(1)=1 sg(2)=2 sg(3)=mex{0,1,2 ...
- 【HDU3032】Nim or not Nim?(博弈论)
[HDU3032]Nim or not Nim?(博弈论) 题面 HDU 题解 \(Multi-SG\)模板题 #include<iostream> #include<cstdio& ...
- NIM游戏,NIM游戏变形,威佐夫博弈以及巴什博奕总结
NIM游戏,NIM游戏变形,威佐夫博弈以及巴什博奕总结 经典NIM游戏: 一共有N堆石子,编号1..n,第i堆中有个a[i]个石子. 每一次操作Alice和Bob可以从任意一堆石子中取出任意数量的石子 ...
随机推荐
- malloc实现机制
使用过c语言的都知道malloc是一个动态分配内存的函数,还可以通过free释放内存空间. 如果我们想分析一下malloc的源码,这其实不是一会就能看懂的,但是我们可以讨论一下malloc的简单实现. ...
- Nexus:hardware type changed to No-Transceiver
如下是相关的案例: 1.N5K & Cat3750 较新版本的NX-OS在N5K上支持1G或10G,但N5K不支持auto-speed sensing 如下是故障信息的体现:3750和N5K之 ...
- Spring_002 依赖注入方式实现
继续写我们的第一个Spring程序,这次我们使用依赖注入的方式实现程序 第一步,建立我们的Spring_002程序,并在程序中添加BookDao.java.BookDaoImpl.java.BookS ...
- 剑指offer系列——62.二叉搜索树的第k个结点
Q:给定一棵二叉搜索树,请找出其中的第k小的结点.例如, (5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小结点的值为4. T: 中序遍历,递归: int count = 0; public ...
- Robot Framework高级
一.Web自动化测试 二.C/S自动化测试 三.数据库自动化测试 四.接口自动化测试 五.RF内置测试库 六.持续集成内置测试库 七.移动自动化测试 八.自定义RF
- 【MySQL】外键的变种
" 目录 三种关系 多对一 多对多 一对一 因为有foreign key的约束,使得两张表形成了三种关系: 多对一 多对多 一对多 重点理解如何找出两张表之间的关系 现在有A.B两张表 分析 ...
- Linux - 删除文件的正确方式
mv <file> /tmp/ cp <file> /opt/file.bak rm
- PHP中数字转为百分位,千分位,万分位。。。
今天做项目中,需要将文章点击量显示在页面中,需求中给的是多少多少万,虽然不是什么难事,但做程序员这么久了,需要考虑的不再是简单的实现,而且有效率和快捷, 虽然PHP自带的函数有number_forma ...
- VS中使用C的一些函数报错的问题
VS建议采用带_s的函数,如scanf_s.strcpy_s,但这些并不是标准C函数. 要想继续使用此函数,需要在源文件中添加以下指令就可以避免这个错误提示: #define _CRT_SECURE_ ...
- 走过的K8S坑
基本的docker命令: docker 镜像 打包成文件 sudo docker save -o 打包后的文件名 {镜像ID}或者{镜像标签} docker 改名: docker tag ff2816 ...