LIGHT OJ 1199 - Partitioning Game
![]() ![]() |
PDF (English) | problem=1199" style="color:rgb(79,107,114)">Statistics |
problem=1199" style="color:rgb(79,107,114)">Forum |
Time Limit: 4 second(s) | Memory Limit: 32 MB |
Alice and Bob are playing a strange game. The rules of the game are:
1. Initially there are n piles.
2. A pile is formed by some cells.
3. Alice starts the game and they alternate turns.
4. In each tern a player can pick any pile and divide it into two unequal piles.
5. If a player cannot do so, he/she loses the game.
Now you are given the number of cells in each of the piles, you have to find the winner of the game if both of them play optimally.
Input
Input starts with an integer T (≤ 1000), denoting the number of test cases.
Each case starts with a line containing an integer n (1 ≤ n ≤ 100). The next line contains n integers, where the ith integer denotes the number of cells in the ith pile. You can assume that the number
of cells in each pile is between 1 and 10000.
Output
For each case, print the case number and 'Alice' or 'Bob' depending on the winner of the game.
Sample Input |
Output for Sample Input |
3 1 4 3 1 2 3 1 7 |
Case 1: Bob Case 2: Alice Case 3: Bob |
Explanation
In case 1, Alice has only 1 move, she divides the pile with 4 cells into two unequal piles, where one pile has 1 cell and the other pile has 3 cells. Now it's Bob's turn. Bob divides the pile with 3 cells into two piles, where one pile has 1 cell and another
pile has 2 cells. So, now there are three piles having cells 1, 1, 2. And Alice loses, since she doesn't have any moves now.
题目大意:
有n堆石子(1<=n<=100),每一堆分别有ai个石子(1<=ai<=10000),一次操作能够使一堆石子变成两堆数目不相等(注意是不相等)的石子,最后不能操作就算输,问先手赢还是后手赢。
解题思路:
就是一个SG函数,提到SG函数这个就是求一下 当前状态的下一个状态,又由于 这 n 堆石子是相互独立的,没有影响 所以说 能够开用SG函数,
依据SG定理,如果 当前堆中有 m块石子 那么他的下一状态就可能有 {1,m-1},{2,n-2},...,{(m-1)/2,m-(m-1)/2}(把每一种情况都想到
而且分析出来)。
然后分完的那些 a和b块石子又能够进行分,以此类推。那么SG(x) = mex{ SG(1)^SG(x-1), SG(2)^SG(x-2),...,
SG((x-1)/2)^SG(x-(x-1)/2) },
然后我们要求的就是 SG[a[0]]^SG[a[1]]^...^SG[a[n-1]],假设结果是0就是 后手赢,否则 先手赢
My Code:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 10000+5;
int sg[MAXN];
int hash[MAXN];
void Get_sg()///模板
{
memset(sg, 0, sizeof(sg));
for(int i=1; i<MAXN; i++)
{
memset(hash, 0, sizeof(hash));
for(int j=1; j*2<i; j++)
{
hash[sg[j]^sg[i-j]] = 1;
}
int j;
for(j=0; j<MAXN; j++)
if(!hash[j])
break;
sg[i] = j;
}
}
int main()
{
Get_sg();
int T;
scanf("%d",&T);
for(int cas=1; cas<=T; cas++)
{
int m, sum = 0;
scanf("%d",&m);
for(int i=0; i<m; i++)
{
int x;
scanf("%d",&x);
sum ^= sg[x];
}
if(sum)
printf("Case %d: Alice\n",cas);
else
printf("Case %d: Bob\n",cas);
}
return 0;
}
LIGHT OJ 1199 - Partitioning Game的更多相关文章
- Light OJ 1199 - Partitioning Game (博弈sg函数)
D - Partitioning Game Time Limit:4000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- Light OJ 1199:Partitioning Game(SG函数模板)
Alice and Bob are playing a strange game. The rules of the game are: 1. Initially there are n p ...
- Light OJ 1114 Easily Readable 字典树
题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 仅仅要满足每一个单词的首尾字符一样 中间顺序能够变化 思路:每一个单词除了首尾 中间的字符排序 ...
- Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖
题目来源:Light OJ 1429 Assassin`s Creed (II) 题意:最少几个人走全然图 能够反复走 有向图 思路:假设是DAG图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...
- Light OJ 1406 Assassin`s Creed 减少国家DP+支撑点甚至通缩+最小路径覆盖
标题来源:problem=1406">Light OJ 1406 Assassin`s Creed 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路: ...
- Light OJ 1316 A Wedding Party 最短路+状态压缩DP
题目来源:Light OJ 1316 1316 - A Wedding Party 题意:和HDU 4284 差点儿相同 有一些商店 从起点到终点在走过尽量多商店的情况下求最短路 思路:首先预处理每两 ...
- light oj 1007 Mathematically Hard (欧拉函数)
题目地址:light oj 1007 第一发欧拉函数. 欧拉函数重要性质: 设a为N的质因数.若(N % a == 0 && (N / a) % a == 0) 则有E(N)=E(N ...
- Light OJ 1406 Assassin`s Creed 状态压缩DP+强连通缩点+最小路径覆盖
题目来源:Light OJ 1406 Assassin`s Creed 题意:有向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路:最少的的人能够走全然图 明显是最小路径覆盖问题 ...
- Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩
题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是全然平方数 求有多少种方案 思路:每一个数分解因子 每隔 ...
随机推荐
- HTTP 响应实体主体:XML 及 XML parser
本文内容 HTTP 响应实体主体:XML XML parser 总结 各编程语言实现的 XML parser HTTP 响应实体主体:XML 实体主体(entity-body)通常是HTTP响应里 ...
- STL 笔记(四) 迭代器 iterator
stl 中迭代器能够理解为面向对象版本号的广义指针,提供了对容器中的对象的訪问方法,能够遍历容器全部元素.也能够訪问随意元素.stl 迭代器有下面五种: Input iterators 仅仅读,输 ...
- Python脚本生成sitemap
项目须要用脚本生成sitemap,中间学习了一下sitemap的格式和lxml库的使用方法.把结果记录一下,方便以后须要直接拿来用. 来自Python脚本生成sitemap 安装lxml 首先须要pi ...
- python网络爬虫 - 设定重试次数内反复抓取
import urllib.request def download(url, num_retries=2): print('Downloading:', url) try: html = urlli ...
- LintCode: Count and Say
C++ class Solution { public: /** * @param n the nth * @return the nth sequence */ string countAndSay ...
- 图解VC++ opengl环境配置和几个入门样例
VC6下载 http://blog.csdn.net/bcbobo21cn/article/details/44200205 demoproject和glut库下载 http://pan.baidu. ...
- Android基础新手教程——1.10 反编译APK获代替码&资源
Android基础新手教程--1.10 反编译APK获代替码&资源 标签(空格分隔): Android基础新手教程 本节引言: "反编译Apk".看上去好像好像非常高端的样 ...
- Read from socket failed: Connection reset by peer.
复制密钥另一台主机时,出现了错误: Read from socket failed: Connection reset by peer. 到被登录主机的/var/log/auth.log查看日志: M ...
- CentOS静默安装Oracle 11gR2(x64)
环境 OS: CentOS 7.4; hosts: L134; IP: 192.168.1.134 DB: linux.x64_11gR2_database 安装依赖包 yum install -y ...
- BIEE Demo(RPD创建 + 分析 +仪表盘 )
说明:此Demo步骤简略,详细Demo可以参照下面的 天善视频:BIEE 11G Rpd模型设计 天善视频:BIEE 11G 报表开发 Oracle BIEE (Business Intelligen ...