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 ...
随机推荐
- 蔡勒(Zeller)公式--黑色星期五
求某年某月某日是周几; 蔡勒(Zeller)公式: w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1 ;y是年的后两位:c是世纪数-1(年的前两位):m是月份,大于等于3,小于等 ...
- IDEA的快捷键和相关设置
快捷键 Shift + Shift: 查找一切 Alt + /: 代码提示(需要修改) Ctrl + Alt + F12: 打开文件所在磁盘位置 Alt + F12: 打开终端 Alt + Ins ...
- 【Fiddler】杂乱基础学习
1.过滤fiddler筛选 打开fiddler>Tools>Fiddler Options>HTTPS>...from remote clients only,勾选这个选项就可 ...
- requests库的小技巧
#coding:utf-8 import requests # url = 'http://www.baidu.com' # response = requests.get(url) # print ...
- Git Gui 查看分支历史的时候中文显示乱码
如图所示 解决方案1 在Git Gui工具栏上选择-编辑-选项: 选择:Default File Contents Encoding, change为UTF-8 成功: 解决方案2 C:\Users ...
- [adt]python实现栈-体验数据结构
经常使用py的一些数据结构,如list,及list的一些方法. 还有hash表等. 各类数据结构方法用的很6,然而不知道是底层是怎么实现的. 基于此,就开始研究一下py实现一些数据结构, 以便于对计算 ...
- hduPiggy-Bank(完全背包)
http://acm.hdu.edu.cn/showproblem.php?pid=1114 此题就是最简单的完全背包,顺序!!! for i=1..N for v=0..V f[v]=max{f[v ...
- poj2063 Investment
http://poj.org/problem?id=2063 首先总结一下:总的来说通过这题我深深感觉到了自己的不足,比赛时思维很受限,...面对超时,没有想到好的解决方案. 题意:给出初始资金,还有 ...
- C#集合中的Add与AddRange方法
C#.NET的集合主要位于System.Collections和System.Collections.Generic(泛型)这两个namespace中. 1.System.Collections 比如 ...
- MongoDB和pymongo自用手册
[*] 本文出处:http://b1u3buf4.xyz/ [*] 本文作者:B1u3Buf4 [*] 本文授权:禁止转载 从自己的另一处博客移动过来.长期维护,不定期添加新内容. 前述和安装 mon ...