LIGHT OJ 1199 - Partitioning Game
来源:互联网 发布:windows xp. 编辑:程序博客网 时间:2024/06/05 20:34
传送门
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函数)
- light oj 1043 Triangle Partitioning
- Light oj 1044 - Palindrome Partitioning(记忆化)
- light oj 1043 - Triangle Partitioning【数学】
- Light OJ 1044 Palindrome Partitioning (hash+DP)
- Light Oj 1044 - Palindrome Partitioning(回文串)
- 【Light-oj】-1043 - Triangle Partitioning(数学)
- light oj 1043 - Triangle Partitioning【简单计算几何】
- Light OJ:1043 Triangle Partitioning(数学+几何)
- Light OJ 1031 Easy Game (区间DP)
- Light OJ 1296 - Again Stone Game
- Light OJ 1315 - Game of Hyper Knights
- Light OJ 1031 Easy Game 区间DP
- light oj 1020 - A Childhood Game (博弈)
- Light oj 1031 Easy Game(记忆化)
- 【Light】[1043]Triangle Partitioning
- Light oj 1044 - Palindrome Partitioning (多校连萌,求最少回文子串的个数)
- linux select 多路复用机制
- C#下的winform编程相关知识
- 限时抢计时器
- Android - TabHost 选项卡功能用法详解
- Android listview与adapter用法详解
- LIGHT OJ 1199 - Partitioning Game
- QT编译./configure参数的详细解释[新手充电必读]
- 解释Array拷贝区别
- android隐藏软键盘
- 编程中常用的单词缩写
- 导航栏使用不透明的图片时,控制器view的y值
- linux下安装zookeeper
- LAMP(Linux+Apache+Mysql+PHP)环境搭建
- 解决listview更新数据时,屏幕闪烁的问题