LightOJ 1296 Again Stone Game(sg函数)题解
题意:每次必须拿且只能拿不超过一半的石头,不能拿为败
思路:显然算出每个的sg函数,但是范围1e9显然不能直接打表。所以先打表找规律,发现偶数一直是自己的一半,奇数好像没规律。偶数x的sg函数值是x/2,说明前x/2~x-1的sg函数值涵盖了所有0~x/2集合的值,那么比他大1的奇数x+1少了x/2的sg函数值,那么x+1的sg函数值就是x/2的sg函数值,然后不断递归。
代码:
#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
typedef long long ll;
const int maxn = + ;
const int seed = ;
const ll MOD = 1e9 + ;
const int INF = 0x3f3f3f3f;
using namespace std;
int main(){
int T, Case = ;
scanf("%d", &T);
while(T--){
ll ans = , n, a;
scanf("%lld", &n);
while(n--){
scanf("%lld", &a);
if(a & ){
while(a & )
a >>= ;
ans ^= a / ;
}
else ans ^= a / ;
}
if(ans)
printf("Case %d: Alice\n", Case++);
else
printf("Case %d: Bob\n", Case++);
}
return ;
}
打表代码:
#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
typedef long long ll;
const int maxn = + ;
const int seed = ;
const ll MOD = 1e9 + ;
const int INF = 0x3f3f3f3f;
using namespace std;
int sg[maxn], s[maxn];
void getSG(){
sg[] = ;
for(int i = ; i < maxn; i++){
memset(s, , sizeof(s));
for(int j = ; j <= i / ; j++){
s[sg[i - j]] = ;
}
for(int j = ; j < maxn; j++){
if(!s[j]){
sg[i] = j;
break;
}
}
}
}
int main(){
getSG();
for(int i = ; i < maxn; i++)
cout << i << " " << sg[i] << endl;
int T, Case = ;
scanf("%d", &T);
while(T--){
ll ans = , n, a;
scanf("%lld", &n);
while(n--){
scanf("%lld", &a);
ans ^= sg[a];
}
if(ans)
printf("Case %d: Alice\n", Case++);
else
printf("Case %d: Bob\n", Case++);
}
return ;
}
LightOJ 1296 Again Stone Game(sg函数)题解的更多相关文章
- LightOJ 1199 Partitioning Game(sg函数)题解
题意:可以把一堆石子分成不相等的两堆,不能操作为败 思路:把一个石子拆成两个,变成了两个独立的游戏,mex里加上两者的sg异或.sg打表. 代码: #include<set> #inclu ...
- lightoj 1296 - Again Stone Game 博弈论
思路:由于数据很大,先通过打表找规律可以知道, 当n为偶数的时候其SG值为n/2; 当n为奇数的时候一直除2,直到为偶数m,则SG值为m/2; 代码如下: #include<stdio.h> ...
- Light OJ 1296 - Again Stone Game (博弈sg函数递推)
F - Again Stone Game Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- LightOJ 1229 Treblecross(SG函数打表 + 遍历)题解
题意:给你一串含“.”和“X”的字串,每次一个玩家可以把‘."变成“X”,谁先弄到三个XXX就赢.假如先手必赢,输出所有能必赢的第一步,否则输出0. 思路:显然如果一个X周围两格有X那么肯定 ...
- hdoj 1729 Stone Games(SG函数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1729 看了题目感觉像Nim,但是有范围限制,有点不知道SG函数该怎么写 看了题解,最后才明白该怎么去理 ...
- 题解——牛客网Wannafly挑战赛23 B-游戏 (SG函数)
前言 比赛的时候没学过SG函数的蒟蒻以为是道结论题,但是不是QwQ 和dummyummy巨佬一起推了快三个小时的规律 最后去问了真正的巨佬__stdcall __stdcall面带微笑的告诉我们,这是 ...
- LightOJ 1315 - Game of Hyper Knights(博弈sg函数)
G - Game of Hyper Knights Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & ...
- POJ 2425 A Chess Game(有向图SG函数)题解
题意:给一个有向图,然后个m颗石头放在图上的几个点上,每次只能移动一步,如果不能移动者败 思路:dfs打表sg函数,然后求异或和 代码: #include<queue> #include& ...
- ZOJ 2083 Win the Game(SG函数)题解
题意:给一端n块的板,两人玩,每次能涂相邻两块没涂过的板,不能涂的人为输,先手赢输出yes 思路:sg函数打表,练习题 代码: #include<queue> #include<cs ...
随机推荐
- AndroidStudio自定义TODO
1.增加自定义TODO标记 Preferences -> Editor -> TODO,然后点击左下角的加号,输入想要自定义的TODO的正则 输入\bX\b.*(X为TODO标签的名字), ...
- 关于JS call apply 对象、对象实例、prototype、Constructor、__proto__
关于call与apply的理解容易让人凌乱,这里有个方法可供参考 tiger.call(fox,arg1,arg2...) tiger.apply(fox,[arg1,arg2...]) 理解为 fo ...
- express 默认模板引擎
使用express -t ejs microblog创建出来的居然不是ejs项目,而是jade项目.现在的版本已经没有-t这个命令了,改为express -e microblog.运行完之后,根据提示 ...
- sql优化实例(用左连接)
改为 也就是说用左连接代替where条件,这样的话效率会提高很多.
- ListView and gridview常用属性
刷新:notifyDataSetChanged 1.gridview常用属性 GridView的一些特殊属性: 1.Android:numColumns=”auto_fit” //GridVi ...
- 12.预处理数据的方法总结(使用sklearn-preprocessing)
https://blog.csdn.net/sinat_33761963/article/details/53433799
- [LeetCode] 122. Best Time to Buy and Sell Stock II_Easy tag: Dynamic Programming
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 在Win7系统下, 使用VS2015 打开带有日文注释程序出现乱码的解决方案
在Win7系统下, 使用VS2015 打开带有日文注释程序出现乱码的解决方案 下载: apploc.msi (下载地址:http://microsoft-applocale.software.info ...
- boostrap小图标
- Pointofix 1.7 Portable试用
Pointofix 1.7 Portable简体中文单文件便携版 软件大小:347K软件语言:简体中文软件类别:国外软件/桌面工具/教育教学运行环境:windows XP/Vista/Win7开 发 ...