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 ...
随机推荐
- 常用的windows注册表大全
目录 使系统没有“运行”选项 1让操作系统无“关闭系统” 选项 2让操作系统无“注销”选项 ...
- SQL实现交,并,差操作
有的数据库不支持intersect,except,所以交集,和差集使用嵌套查询来做比较靠谱. a表和b表具有完全一样的结构 mysql> desc a; +-------+----------- ...
- Mysql技术内幕——InnoDB存储引擎
Mysql技术内幕——InnoDB存储引擎 http://jingyan.baidu.com/article/fedf07377c493f35ac89770c.html 一.mysql体系结构和存储引 ...
- 在系统启动时,Windows Vista 中、 在 Windows 7 中,Windows Server 2008 中和在 Windows Server 2008 R2 中的 497 天后未关闭 TIME_WAIT 状态的所有 TCP/IP 端口
在系统启动时,Windows Vista 中. 在 Windows 7 中,Windows Server 2008 中和在 Windows Server 2008 R2 中的 497 天后未关闭 TI ...
- 【深入理解javascript】闭包
1.作用域 “javascript没有块级作用域”.所谓“块”,就是大括号“{}”中间的语句.例如if语句: 再比如for语句: 所以,我们在编写代码的时候,不要在“块”里面声明变量,要在代码的一开始 ...
- vue中使用better-scroll实现滑动效果
1.安装:npm install better-scroll 2.引入:import BetterScrol from "better-scroll"; 1.滚动效果 better ...
- vue学习之webpack
本质上,Webpack是一个现代 JavaScript应用程序的静态模块打包器(module bundler).当 Webpack处理应用程序时,它会递归地构建一个依赖关系图(dependency g ...
- (转)Mysql 多表查询详解
MySQL 多表查询详解 一.前言 二.示例 三.注意事项 一.前言 上篇讲到mysql中关键字执行的顺序,只涉及了一张表:实际应用大部分情况下,查询语句都会涉及到多张表格 : 1.1 多表连接有 ...
- PAT Sign In and Sign Out[非常简单]
1006 Sign In and Sign Out (25)(25 分) At the beginning of every day, the first person who signs in th ...
- Leetcode: Merge k Sorted List
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 参 ...