Light OJ 1199 - Partitioning Game (博弈sg函数)
Time Limit:4000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
Description
Alice and Bob are playing a strange game. The rules of the game are:
- Initially there are n piles.
- A pile is formed by some cells.
- Alice starts the game and they alternate turns.
- In each tern a player can pick any pile and divide it into two unequal piles.
- 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堆石子(1<=n<=100),每一堆分别有xi个石子(1<=xi<=10000),
一次操作可以使一堆石子变成两堆数目不相等的石子,
最后不能操作的算输,问先手胜还是后手胜。
思路:n堆石子相互独立,所以可以应用SG定理,只需要算出一堆石子的SG函数。
一堆石子(假设有x个)的后继状态可以枚举出来,分别是{1,x-1},{2,x-2},...,{(x-1)/2,x-(x-1)/2},
一堆石子分成的两堆石子又相互独立,再次应用SG定理。
所以SG(x) = mex{ SG(1)^SG(x-1), SG(2)^SG(x-2),..., SG((x-1)/2)^SG(x-(x-1)/2) },
最后的答案是SG(x1)^SG(x2)^...^SG(xn)
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int sg[]= {};
bool vis[];
int t,cas=;
void get()
{
memset(sg,,sizeof(sg));
for(int i=; i<=; i++) //i=0相当于空石子堆 不符合题意
{
memset(vis,,sizeof(vis));
for(int j=; j+j<i; j++) //j=0相当于没分 j=i/2 相当于分成相等的两堆 这两种情况都是不可以的
vis[sg[j]^sg[i-j]]=;
for(int x=; x<=; x++)
if(!vis[x])
{
sg[i]=x;
break; //一定别忘了break
}
}
}
void pra()
{
printf("Case %d: Alice\n",cas++);
}
void prb()
{
printf("Case %d: Bob\n",cas++);
}
int main()
{
get();
cin>>t;
while(t--)
{
int n,data,ans=;
cin>>n;
for(int i=; i<n; i++)
{
cin>>data;
ans^=sg[data]; //是sg[data]不是data 对于B来说每个都是0最好了
}
if(ans)
pra();
else
prb();
}
return ;
}
对sg函数的理解:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int sg[];
int hash[];
void getsg()
{
memset(sg,,sizeof(sg));
for(int i=;i<=;i++)
{
memset(hash,,sizeof(hash));
for(int j=;j+j<i;j++) //不相等的两堆
//注意是sg[j]不是sg[i]
hash[sg[j]^sg[i-j]]=; //sg[j]^sg[i-j] 是i的后继 i是从小到大跑的 所以不需要搜索直接用即可
//小的数是大的数的后继 sg[1]^sg[4]=0,sg[2]^sg[3]=1是sg[5]=2的后继
for(int x=;x<=;x++)//不是0的就是必胜点
//只要在上面的sg里能异或出来一个等于0的 就是后继里有一个是等于0的 这个点就是必胜点
//多个点异或和以前的思想一样 看成n个有向图
//i小的时候可以这么看
//根据题意 不能分的点比如1或者分了以后还能分一次的点比如4 就是必败点 sg值为0
//i的异或值就是i 分成两个数看他俩的异或值是多少 如果分成的两个数的异或值中存在0说明该点是胜的
if(!hash[x])
{
sg[i]=x;
break;
}
}
}
int main()
{
getsg();
for(int i=;i<;i++)
cout<<"i: "<<i<<" sg[i]: "<<sg[i]<<endl;
/*int t,cas=1;
cin>>t;
while(t--)
{
int n,data,ans=0;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>data;
ans^=sg[data];
}
if(ans)
printf("Case %d: Alice\n",cas++);
else
printf("Case %d: Bob\n",cas++);
}*/
return ;
}
Light OJ 1199 - Partitioning Game (博弈sg函数)的更多相关文章
- LIGHT OJ 1199 - Partitioning Game
传送门 1199 - Partitioning Game PDF (English) problem=1199" style="color:rgb(79,107,114)&q ...
- LightOJ 1199 Partitioning Game(sg函数)题解
题意:可以把一堆石子分成不相等的两堆,不能操作为败 思路:把一个石子拆成两个,变成了两个独立的游戏,mex里加上两者的sg异或.sg打表. 代码: #include<set> #inclu ...
- S-Nim HDU 1536 博弈 sg函数
S-Nim HDU 1536 博弈 sg函数 题意 首先输入K,表示一个集合的大小,之后输入集合,表示对于这对石子只能去这个集合中的元素的个数,之后输入 一个m表示接下来对于这个集合要进行m次询问,之 ...
- 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 1296 - Again Stone Game (博弈sg函数递推)
F - Again Stone Game Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- hdu 3032(博弈sg函数)
题意:与原来基本的尼姆博弈不同的是,可以将一堆石子分成两堆石子也算一步操作,其它的都是一样的. 分析:由于石子的堆数和每一堆石子的数量都很大,所以肯定不能用搜索去求sg函数,现在我们只能通过找规律的办 ...
- HDU-4678 Mine 博弈SG函数
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4678 题意就不说了,太长了... 这个应该算简单博弈吧.先求联通分量,把空白区域边上的数字个数全部求出 ...
- light oj 1007 Mathematically Hard (欧拉函数)
题目地址:light oj 1007 第一发欧拉函数. 欧拉函数重要性质: 设a为N的质因数.若(N % a == 0 && (N / a) % a == 0) 则有E(N)=E(N ...
- (转)博弈 SG函数
此文为以下博客做的摘要: https://blog.csdn.net/strangedbly/article/details/51137432 ---------------------------- ...
随机推荐
- poj2774 后缀数组 求最长公共子串
Reference:IOI2009论文 http://www.cnblogs.com/ziyi--caolu/p/3192731.html #include "stdio.h" # ...
- Java NIO、NIO.2学习笔记
相关学习资料 http://www.molotang.com/articles/903.html http://www.ibm.com/developerworks/cn/education/java ...
- java连接mysql(二)
模拟转账成功时的业务场景 import java.sql.*; public class TransactionDemo1 { public static void main(String[] arg ...
- React Native 开发之 (02) 用Sublime 3作为React Native的开发IDE
Sublime Text是一个代码编辑器.也是HTML和散文先进的文本编辑器.漂亮的用户界面和非凡的功能,例如:迷你地图,多选择Python插件,代码段等等.完全可自定义键绑定,菜单和工具栏等等.漂亮 ...
- Java_观察者模式(Observable和Observer)
http://blog.csdn.net/tianjf0514/article/details/7475164/ 一.观察者模式介绍 在Java中通过Observable类和Observer接口实现了 ...
- UVa OJ 140 - Bandwidth (带宽)
Time limit: 3.000 seconds限时3.000秒 Problem问题 Given a graph (V,E) where V is a set of nodes and E is a ...
- 织梦DedeCMS调用二级子栏目或者多级栏目解决方法
本文是关于织梦DedeCMS调用多级子栏目的,拿来测试分享下.DEDECMS v5.7,后台已建栏目目录,如下图1所示: 图1 DEDECMS后台栏目结构 现在,我们先来调用顶级栏目"站长新 ...
- vm虚拟机自定义安装centOS找不到网卡
问题:自定义简化安装后执行ifconfig无法找到eth0网卡 1.查看eth0网络配置: [root@minion1 ~]# cat /etc/sysconfig/network-scripts/i ...
- Java操作Excel(读、写、搜索关键字、插入图片)
import java.io.File; import java.io.IOException; import jxl.Cell; import jxl.Sheet; import jxl.Workb ...
- SilverLight 条码 扫描枪 MVVM(转载)
目前做SL项目时,遇到条码的问题. 客户需要通过条码来加快工作效率以及减少错误.有条码,自然便引入扫描枪这个东西.不解释. 关键点是:扫描枪每次扫描完毕会触发回车 这是解决问题的关键! 现有两种情景: ...