Partitioning Game (SG函数)
来源:互联网 发布:中英文在线翻译软件 编辑:程序博客网 时间:2024/05/20 01:34
Partitioning Game :http://acm.hust.edu.cn/vjudge/contest/view.action?cid=112620#problem/D 传送门:nefu
题面描述:
Description
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
3
1
4
3
1 2 3
1
7
Sample Output
Case 1: Bob
Case 2: Alice
Case 3: Bob
题目大意:
n堆物品,每堆物品有多个组成,Alice和Bob轮流依次对这些物品进行操作,每次都要把其中一堆物品分成不同的个数,最后一个没办法再进行操作的人输掉比赛。
题目分析:
由于每一堆物品的分割都是相互独立的,所以我们可以用SG函数来处理,先计算一堆的,对于一个由a件物品组成的堆,可以分割的后继方法有(1,a-1),(2,a-2),(3,a-3),……,((a-1)/2,a-(a-2)/2);
由于由同一堆分割好的两堆也是相互独立的,所以再用SG定理,得到:SG(x)=mex{SG(1)^SG(a-1), SG(2)^SG(a-2), SG(3)^SG(a-3), ... ,SG((a-1)/2)^SG(a-(a-1)/2)};
则最后的答案是:SG(a1)^SG(a2)^SG(a3)^...^SG(an);
代码实现:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;int hashsz[10010],sg[10010];void getsg(){ memset(sg,0,sizeof(sg)); for(int i=1;i<=10000;i++) ///sg[0]=0; { memset(hashsz,0,sizeof(hashsz)); for(int j=1;j+j<i;j++) { hashsz[sg[j]^sg[i-j]]++; } for(int j=0;j<=10000;j++) if(!hashsz[j]) { sg[i]=j; break; } }}int main(){ getsg(); int t,n,a,casenum=0; int ans; scanf("%d",&t); while(t--) { scanf("%d",&n); ans=0; for(int i=0;i<n;i++) { scanf("%d",&a); ans=ans^sg[a]; } if(ans) printf("Case %d: Alice\n",++casenum); else printf("Case %d: Bob\n",++casenum); } return 0;}
- Partitioning Game (SG函数)
- 【lightOJ】 Partitioning Game (博弈,sg函数)
- Light OJ 1199 - Partitioning Game (博弈sg函数)
- hdu Stone Game SG函数
- hdu1730 Northcott Game SG函数
- HDU1729 Stone Game (SG函数)
- hdu1729 Stone Game-sg函数
- POJ2311 Cutting Game SG函数
- HDU1079 Calendar Game SG函数
- pku 2425 A Chess Game(SG函数)
- poj 2311 Cutting Game(sg函数)
- poj 2425 A Chess Game (sg函数)
- HDU 1729 Stone Game (SG函数)
- POJ 2311 Cutting Game (SG函数)
- HDOJ 1524 A Chess Game SG函数
- HDU 1729 Stone Game 【博弈论,SG函数】
- hdu1851_A Simple Game博弈—SG函数
- poj 2311 Cutting Game (sg函数)
- dp【划分数】
- 网页前端的异常处理方法、异常处理系统及异常处理服务器
- TCP/IP三次握手与四次挥手
- 找寻路径从根root到节点p的路径函数
- LeetCode #6 ZigZag Conversion C# Solution
- Partitioning Game (SG函数)
- ZOJ 3932 Handshakes
- bfs
- 键盘事件
- 在php中json_encode与json_decode的使用方法
- 用JS获取地址栏参数的方法(超级简单)
- Dubbo高级篇_12_dubbo服务只注册
- poj1328 Radar Installation 贪心
- 最短路