Alice and Bob HDU - 4111 (SG函数)
The rule of the new game is quite simple. At the beginning of the game, they write down N random positive integers, then they take turns (Alice first) to either:
1. Decrease a number by one.
2. Erase any two numbers and write down their sum.
Whenever a number is decreased to 0, it will be erased automatically. The game ends when all numbers are finally erased, and the one who cannot play in his(her) turn loses the game.
Here's the problem: Who will win the game if both use the best strategy? Find it out quickly, before they get bored of the game again!
InputThe first line contains an integer T(1 <= T <= 4000), indicating the number of test cases.
Each test case contains several lines.
The first line contains an integer N(1 <= N <= 50).
The next line contains N positive integers A 1 ....A N(1 <= A i <= 1000), represents the numbers they write down at the beginning of the game.OutputFor each test case in the input, print one line: "Case #X: Y", where X is the test case number (starting with 1) and Y is either "Alice" or "Bob".
Sample Input
3
3
1 1 2
2
3 4
3
2 3 5
Sample Output
Case #1: Alice
Case #2: Bob
Case #3: Bob 给出 n 个数,每次有两种操作
1、任意取一个数字,让他减一
2、把两个数字合并起来
最后不能操作的人失败 考虑全部不合并和全部合并的情况,操作次数的范围是[∑a, ∑a + n - 1]。
如果每一堆石子全部大于1, ∑a + n - 1 就是先手能否赢。 因为如果这是一个奇数,后手想赢必须让其中一堆没有参与到合并,那么后手一定要让这一堆进行操作1,由于全部大于1,那么不可能一步完成,先手只要每次把这堆合并起来就一定能赢。
反之同理,偶数时后手用同样的方法限制先手,后手一定能赢。
那么考虑有 1 的时候,如果这时候一方想要减少合并次数,只需要把 1 进行操作1,另一方无法阻止。则把 1 单独拿出来。
用 sg[i][j] 表示有 i 个 1 ,除了 1 以外剩余堆可以进行的最多的操作次数。然后开始操作
(1)、在 i 里面进行操作1
(2)、在 j 里面进行操作1
(3)、让 i 和 j 进行操作2
(4)、让 i 和 i 进行操作2
然后进行转移就可以了
然后其中的细节差不多有如下
1、当 i == 0时,j 的奇偶进就决定了能否赢
2、在(2)中,如果 j==2,会转化一个到 i,自己变成0,变成sg[i+1][0]; 如果j>2,正常转化,sg[i+1][j-1];
3、在(4)中,如果 j==0,转化出来为sg[i-2][2]; 如果 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;;;;;;;;;!%$$$$$@#|.
;&$!;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;%#####|.
|##$|!;;;;;;::'':;;;;;;;;;;;;;!%$$$@#@;
;@&|;;;;;;;::'''''':;;;;;;;|$&@###@|`
.%##@|;;;;:::''''''''''::;!%&##$'
`$##@$$@@&|!!;;;:'''''::::;;;;;|&#%.
;&@##&$%!;;;;;;::''''''''::;!|%$@#@&@@:
.%@&$$|;;;;;;;;;;:'''':''''::;;;%@#@@#%.
:@##@###@$$$$$|;;:'''':;;!!;;;;;;!$#@@#$;`
`%@$$|;;;;;;;;:'''''''::;;;;|%$$|!!&###&'
|##&%!;;;;;::''''''''''''::;;;;;;;!$@&:`!'
:;!@$|;;;;;;;::''''''''''':;;;;;;;;!%&@$: !@#$'
|##@@&%;;;;;::''''''''':;;;;;;;!%&@#@$%: '%%!%&;
|&%!;;;;;;;%$!:''''''':|%!;;;;;;;;|&@%||` '%$|!%&;
|@%!;;!!;;;||;:'''''':;%$!;;;;!%%%&#&%$&: .|%;:!&%`
!@&%;;;;;;;||;;;:''::;;%$!;;;;;;;|&@%;!$; `%&%!!$&:
'$$|;!!!!;;||;;;;;;;;;;%%;;;;;;;|@@|!$##; !$!;:!$&:
|#&|;;;;;;!||;;;;;;;;!%|;;;;!$##$;;;;|%' `%$|%%;|&$'
|&%!;;;;;;|%;;;;;;;;$$;;;;;;|&&|!|%&&; .:%&$!;;;:!$@!
`%#&%!!;;;;||;;;;;!$&|;;;!%%%@&!;;;!!;;;|%!;;%@$!%@!
!&!;;;;;;;;;||;;%&!;;;;;;;;;%@&!;;!&$;;;|&%;;;%@%`
'%|;;;;;;;;!!|$|%&%;;;;;;;;;;|&#&|!!||!!|%$@@|'
.!%%&%'`|$; :|$#%|@#&;%#%.
*/
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pii pair<int, int>
#define INOPEN freopen("in.txt", "r", stdin)
#define OUTOPEN freopen("out.txt", "w", stdout) typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 5e4 + ;
const int maxm = 1e5 + ;
const ll mod = 1e9 + ;
const ll INF = 1e18 + ;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-;
using namespace std; int n, m;
int cas, tol, T; int sg[][maxn]; void dfs(int x, int y) {
if(sg[x][y] != -) return ;
if(x == ) {
sg[x][y] = y&;
return ;
}
if(x) {
dfs(x-, y);
if(sg[x-][y] == ) {
sg[x][y] = ;
return ;
}
}
if(y) {
dfs(x-, y+);
if(sg[x-][y+] == ) {
sg[x][y] = ;
return ;
}
}
if(x > ) {
if(y) {
dfs(x-, y+);
if(sg[x-][y+] == ) {
sg[x][y] = ;
return ;
}
} else {
dfs(x-, );
if(sg[x-][] == ) {
sg[x][y] = ;
return ;
}
}
}
if(y > ) {
if(y > ) {
dfs(x, y-);
if(sg[x][y-] == ) {
sg[x][y] = ;
return ;
}
} else {
dfs(x+, );
if(sg[x+][] == ) {
sg[x][y] = ;
return ;
}
}
}
sg[x][y] = ;
return ;
} int main() {
mes(sg, -);
sg[][] = ;
cas = ;
scanf("%d", &T);
while(T--) {
int ans = , cnt = ;
scanf("%d", &n);
for(int i=, x; i<=n; i++) {
scanf("%d", &x);
if(x > )
ans += x+;
else
cnt++;
}
if(ans)
ans--;
if(sg[cnt][ans] == -)
dfs(cnt, ans);
printf("Case #%d: %s\n", cas++, sg[cnt][ans] ? "Alice" : "Bob");
}
return ;
}
Alice and Bob HDU - 4111 (SG函数)的更多相关文章
- ZOJ 3529 A Game Between Alice and Bob(博弈论-sg函数)
ZOJ 3529 - A Game Between Alice and Bob Time Limit:5000MS Memory Limit:262144KB 64bit IO For ...
- hdu 2147 SG函数打表(手写也可以) 找规律
kiki's game Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 40000/1000 K (Java/Others) Total ...
- hdu 2999 sg函数(简单博弈)
Stone Game, Why are you always there? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/ ...
- HDU 1536 sg函数
S-Nim Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- hdu 1536 SG函数模板题
S-Nim Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- Alice and Bob HDU - 4268
Alice and Bob's game never ends. Today, they introduce a new game. In this game, both of them have N ...
- hdu 1848(SG函数)
Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- hdu 1847(SG函数,巴什博弈)
Good Luck in CET-4 Everybody! Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU 1848 SG函数博弈
Fibonacci again and again Problem Description 任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的:F(1 ...
随机推荐
- Vue + WebApi 小项目:构造自己的在线 Markdown 笔记本应用
Vue + WebApi 小项目:构造自己的在线 Markdown 笔记本应用 目录 概要 知识点 完整示例图 代码与资源文件 流程步骤 概要 基于 MVP 最小可行性产品设计理念,我们先完成一个可以 ...
- c#封装DBHelper类
public enum EffentNextType { /// <summary> /// 对其他语句无任何影响 /// </summary> None, /// <s ...
- 解决mysql时区与系统时区不一致问题。异常:The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone.
异常信息:The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone ...
- C++系列总结——多态
前言 封装隐藏了类内部细节,通过继承加虚函数的方式,我们还可以做到隐藏类之间的差异,这就是多态(运行时多态).多态意味一个接口有多种行为,今天就来说说C++的多态是怎么实现的. 编译时多态感觉没什么好 ...
- Snapde和常用的CSV文件编辑器对比
Snapde,一个专门为编辑超大型数据量CSV文件而设计的单机版电子表格软件:它运行的速度非常快,反应非常灵敏. CSV是一种用逗号分隔列.回车分割行的文本文件,市面上常用的CSV编辑软件有:Snap ...
- (七) Keras 绘制网络结构和cpu,gpu切换
视频学习来源 https://www.bilibili.com/video/av40787141?from=search&seid=17003307842787199553 笔记 首先安装py ...
- No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android"
安装完NDK的时候出现了这个错误,网上的办法是下载旧版的NDK,将其中的toolchain复制到新版的NDK中. 但其实不用这么麻烦. 经过对新版NDK的研究,发现NDK的更新记录里有一段话 This ...
- adb 查看 android手机的CPU架构
adb shell cat /proc/cpuinfo 当然要下载adb并配置好环境变量
- 数据结构java(一)数组链表
链表是数据结构中最基础的内容,链表在存储结构上分成两种:数组形式储存,链式存储. 相比c语言需要的结构体,在java中由于有了面向对象编程,将指针‘藏’了起来,不需要分配内存. 所以只需要创建一个对象 ...
- DVWA-命令执行学习笔记
DVWA-命令执行 原理: web服务器没有对用户提交的数据进行严格的过滤,造成调用操作系统的命令或者在操作系统恶意拼接拼接命令,以达到攻击者的目的. 1.将DVWA的级别设置为low 1.2查看源代 ...