UVA1482 Playing With Stones —— SG博弈
题目链接:https://vjudge.net/problem/UVA-1482
题意:
有n堆石子, 每堆石子有ai(ai<=1e18)。两个人轮流取石子,要求每次只能从一堆石子中抽取不多于一半的石子,最后不能取的为输家。
题解:
典型的SG博弈,由于ai的范围很大,所以不能直接求SG值,那么就打表SG值找规律,如下:
发现,当x为偶数时, SG[x] = x/2; 当x为奇数时, SG[x] = SG[x/2],即如下:
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; int SG[MAXN], vis[MAXN];
void table()
{
SG[] = SG[] = ;
for(int i = ; i<=; i++)
{
memset(vis, , sizeof(vis));
for(int j = ; j<=i/; j++) vis[SG[i-j]] = ;
for(int j = ;;j++) if(!vis[j]) {
SG[i] = j;
break;
}
} for(int i = ; i<=; i++) printf("%-2d ",i); putchar('\n');
for(int i = ; i<=; i++) printf("%-2d ",SG[i]); putchar('\n');
putchar('\n');
for(int i = ; i<=; i+=) printf("%-2d ",i); putchar('\n');
for(int i = ; i<=; i+=) printf("%-2d ",SG[i]); putchar('\n');
putchar('\n');
for(int i = ; i<=; i+=) printf("%-2d ",i); putchar('\n');
for(int i = ; i<=; i+=) printf("%-2d ",SG[i]); putchar('\n');
/*
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
0 0 1 0 2 1 3 0 4 2 5 1 6 3 7 0 8 4 9 2 10 5 11 1 12 6 13 3 14 7 15 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29
0 0 1 0 2 1 3 0 4 2 5 1 6 3 7
*/
} LL getSG(LL x){
return x%==?x/:getSG(x/);
} int main()
{
// table();
int T, n;
scanf("%d", &T);
while(T--)
{
LL a, v = ;
scanf("%d", &n);
for(int i = ; i<=n; i++)
{
scanf("%lld", &a);
v ^= getSG(a);
} if(v) printf("YES\n");
else printf("NO\n");
}
}
UVA1482 Playing With Stones —— SG博弈的更多相关文章
- UVA 1482 - Playing With Stones(SG打表规律)
UVA 1482 - Playing With Stones 题目链接 题意:给定n堆石头,每次选一堆取至少一个.不超过一半的石子,最后不能取的输,问是否先手必胜 思路:数值非常大.无法直接递推sg函 ...
- UVALive 5059 C - Playing With Stones 博弈论Sg函数
C - Playing With Stones Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu S ...
- UVA12293 Box Game —— SG博弈
题目链接:https://vjudge.net/problem/UVA-12293 题意: 两人玩游戏,有两个盒子,开始时第一个盒子装了n个球, 第二个盒子装了一个球.每次操作都将刷量少的盒子的球倒掉 ...
- HDU 1848(sg博弈) Fibonacci again and again
Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- uva1482:Playing With Stones (SG函数)
题意:有N堆石子,每次可以取一堆的不超过半数的石子,没有可取的为输. 思路:假设只有一堆,手推出来,数量x可以表示为2^p-1形式的必输. 但是没什么用,因为最后要的不是0和1,而是SG函数:所以必输 ...
- LA 5059 (找规律 SG函数) Playing With Stones
题意: 有n堆石子,两个人轮流取,每次只能取一堆的至少一个至多一半石子,直到不能取为止. 判断先手是否必胜. 分析: 本题的关键就是求SG函数,可是直接分析又不太好分析,于是乎找规律. 经过一番“巧妙 ...
- 【LA5059】Playing With Stones (SG函数)
题意:有n堆石子,分别有a[i]个.两个游戏者轮流操作,每次可以选一堆,拿走至少一个石子,但不能拿走超过一半的石子. 谁不能拿石子就算输,问先手胜负情况 n<=100,1<=a[i]< ...
- Playing With Stones UVALive - 5059 Nim SG函数 打表找规律
Code: #include<cstdio> #include<algorithm> using namespace std; typedef long long ll; ll ...
- hdu 1851(A Simple Game)(sg博弈)
A Simple Game Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)Tot ...
随机推荐
- POJ 3660 Cow Contest 传递闭包+Floyd
原题链接:http://poj.org/problem?id=3660 Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- 使用JAXP对xml文档进行DOM解析基础
XML解析方式分为两种:dom和sax dom:(Document Object Model, 即文档对象模型) 是 W3C 组织推荐的处理 XML 的一种方式. sax: ...
- Spring实战Day3
通过XML创建装配bean 1.装配不存在成员变量的bean <bean id="talent" class="cn.jqzhong.Spring.study.da ...
- BT中的磁力链接(转)
注意:磁力链接不是迅雷的,而是BT网络中的一种协议. 磁力链接与种子文件 磁力链接并不是一个新概念,早在2002年,相关的标准草稿就已经制定了.但直到2012年海盗湾为规避版权问题删除了站点上的所有T ...
- filter 中用spring StopWatch 监控请求执行时间
在filter中用spring stopWatch 来统计每个请求的执行时间: 虽然在firefox 中可以清楚的看到每个请求的执行时间,但是为了测试,记录日志, 方便以后查询维护. 还是必要的,下面 ...
- C#中通过反射获取类中非公有成员
public class NGlbGlobeXComm { public static T GetPrivateField<T>(object instance, string field ...
- Cocos2d-x学习笔记(18)(TestCpp源代码分析-2)
本章主要讲controller.h/cpp文件的分析,该文件主要用于演示样例场景管理类TestController,用于显示全部演示样例的菜单. //controller.cpp #include & ...
- nload 命令
网卡 流量监控命令 // 安装 yum intall nload nload 上下page 键 切换网卡查看
- C++编译错误 2001 1120
无法解析的外部符号"symbol" 代码引用了链接器无法在库和对象文件中找到的内容(如函数.变量或标签). 该错误信息之后为错误 LNK1120. 可能的原因 : 在将托管库或 W ...
- POJ 2480 Longge's problem 积性函数
题目来源:id=2480" style="color:rgb(106,57,6); text-decoration:none">POJ 2480 Longge's ...