【hdu 3389】Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 710 Accepted Submission(s): 501
Problem Description
Bob and Alice are playing a new game. There are n boxes which have been numbered from 1 to n. Each box is either empty or contains several cards. Bob and Alice move the cards in turn. In each turn the corresponding player should choose a non-empty box A and choose another box B that B< A && (A+B)%2=1 && (A+B)%3=0. Then, take an arbitrary number (but not zero) of cards from box A to box B. The last one who can do a legal move wins. Alice is the first player. Please predict who will win the game.
Input
The first line contains an integer T (T<=100) indicating the number of test cases. The first line of each test case contains an integer n (1<=n<=10000). The second line has n integers which will not be bigger than 100. The i-th integer indicates the number of cards in the i-th box.
Output
For each test case, print the case number and the winner’s name in a single line. Follow the format of the sample output.
Sample Input
2
2
1 2
7
1 3 3 2 2 1 2
Sample Output
Case 1: Alice
Case 2: Bob
【题目链接】:http://acm.hdu.edu.cn/showproblem.php?pid=3389
【题解】
/*
题目所给的条件其实可以简化成A+B == 6n+3;
也就是说(A+B)%6 == 3;
则对于可以互相传递纸牌的两个位置x,y(x< y)
(x+y)%6==3;
根据同余率;
(x%6+y%6)%6==3;
所以设x%6为a,y%6为b;
则可以传递纸牌的条件是
①a=0,b=3
②a=3,b=0
③a=1,b=2
④a=2,b=1
⑤a=4,b=5
⑥a=5,b=4
假设没有③..⑥这些能够传递纸牌的情况;只考虑①和②能传递纸牌;
则传递顺序必然是这样的(下面的数字都是下标从大到小%6后的结果)
0->3->0->3….->0->3
或
3->0->3->0….->0->3
最后一个传递到的数字一定是数字3(因为不能传递到数字0了);
这其实很像阶梯博弈了
【阶梯博弈】http://blog.csdn.net/harlow_cheng/article/details/54024055
我们只要对下标为%6为0的堆做NIM博弈就好了嘛
即把0传递给3的过程看作是把%6==0的下标的堆的“石头”扔掉;
(如果对方也一直在对0操作,则你的胜败肯定和0的那些堆的异或值决定的结果一样)
如果对方对3进行了一次操作->即传递给了0,那么你就把传递给0的牌等量地传递给3
这样又保持了0堆你是必胜的状态;且总是由你把0堆上的牌传递给3堆上的牌(或总是对方。。)
所以只要对0堆(i%6==0)的所有牌数异或就好了->作为第一个游戏的sg函数
③和④的情况
对应
2->1->2->1->2….->2->1 (1不能再变成2了!)
或
1->2->1->2->1….->2->1
则对2堆进行nim博弈(i%6==2的堆的异或值)->作为第二个游戏的sg函数
④和⑤
对应
5->4->5->4->5….->5->4 (1不能再变成2了!)
或
4->5->4->5->4….->5->4
则对5对进行nim博弈(i%6==5的堆的异或值)->作为第三个游戏的sg函数
把三个游戏的sg函数再异或一下;则为整个游戏的sg函数了;
为0则先手胜,否则先手输;
因为异或有交换律;
所以直接异或i%6==0,2,5的堆就好了;不必按顺序真的一个一个游戏搞sg函数;
*/
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
//const int MAXN = x;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
int main()
{
//freopen("F:\\rush.txt","r",stdin);
int T;
rei(T);
rep1(ii,1,T)
{
int n,ans = 0;
rei(n);
rep1(i,1,n)
{
int x;
rei(x);
if (i%6==0 || i%6==2 || i%6==5)
ans = ans ^ x;
}
if (ans==0)
printf("Case %d: Bob\n",ii);
else
printf("Case %d: Alice\n",ii);
}
return 0;
}
【hdu 3389】Game的更多相关文章
- 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题
[HDU 3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...
- 【HDU 5647】DZY Loves Connecting(树DP)
pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...
- -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】
[把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...
- 【HDU 2196】 Computer(树的直径)
[HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...
- 【HDU 2196】 Computer (树形DP)
[HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...
- 【HDU 5145】 NPY and girls(组合+莫队)
pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...
- 【hdu 1043】Eight
[题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1043 [题意] 会给你很多组数据; 让你输出这组数据到目标状态的具体步骤; [题解] 从12345 ...
- 【HDU 3068】 最长回文
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3068 [算法] Manacher算法求最长回文子串 [代码] #include<bits/s ...
- 【HDU 4699】 Editor
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4699 [算法] 维护两个栈,一个栈放光标之前的数,另外一个放光标之后的数 在维护栈的同时求最大前缀 ...
随机推荐
- 关于android主线程异常NetworkOnMainThread不能訪问网络
今天在学习的过程中遇到了NetworkOnMainThread的异常,关于这个异常问题在android sdk 4.0版本号上,这个问题可能比較常见,查了许些资料大多都是大概解说原因,可是没有解说到详 ...
- Android学习笔记进阶八之Matrix矩阵
Matrix,中文里叫矩阵,高等数学里有介绍,在图像处理方面,主要是用于平面的缩放.平移.旋转等操作. 在Android里面,Matrix由9个float值构成,是一个3*3的矩阵.最好记住.如下图: ...
- Flask项目之手机端租房网站的实战开发(九)
说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 接着上一篇博客继续往下写 :https://blog.csdn.net/qq_41782425/article/details/8 ...
- GO语言学习(十一)Go 语言循环语句
Go 语言提供了以下几种类型循环处理语句: 循环类型 描述 for 循环 重复执行语句块 循环嵌套 在 for 循环中嵌套一个或多个 for 循环 语法 Go语言的For循环有3中形式,只有其中的一种 ...
- 老李的菜园 mysql 自定义函数
新建: Create function function_name(参数列表)returns返回值类型 函数体 函数名,应该合法的标识符,并且不应该与已有的关键字冲突. 一个函数应该属于某个数据库,可 ...
- cURL(wget)—— 测试 RESTful 接口及模拟 GET/POST/PUT/DELETE/OPTIONS 请求
cURL 是一个简单的 http 命令行工具.与最优秀的 Unix 工具一样,在设计之时,cURL 是个小型程序,功能十分专一,而且是故意为之,仅用于访问 http 服务器.(在 Linux 中,可以 ...
- 8.5 Android灯光系统_源码分析_通知灯
参考文章(应用程序举例)how to use the LED with Android phonehttp://androidblogger.blogspot.jp/2009/09/tutorial- ...
- 6、修改应用程序数码相框以支持自动关闭LCD
1. 修改数码相框以自动关闭LCD关闭LCD : 在读取触摸屏的函数中判断:如果15S内无数据,执行: echo auto > /sys/devices/platform/mylcd/power ...
- POJ3171 Cleaning Shifts DP,区间覆盖最值
题目大意.N个区间覆盖[T1,T2]及相应的代价S,求从区间M到E的所有覆盖的最小代价是多少. (1 <= N <= 10,000).(0 <= M <= E <= 86 ...
- MVC模式编程演示样例-登录验证(静态)
好,上篇博客分享了本人总结的JSP-Servlet-JavaBean三层架构编程模式的实现思想和基本流程,接下来给大家分享一个MVC编程模式的实现演示样例-登录验证的过程,这里我仍然用的是静态的验证u ...