题目链接: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博弈的更多相关文章

  1. UVA 1482 - Playing With Stones(SG打表规律)

    UVA 1482 - Playing With Stones 题目链接 题意:给定n堆石头,每次选一堆取至少一个.不超过一半的石子,最后不能取的输,问是否先手必胜 思路:数值非常大.无法直接递推sg函 ...

  2. UVALive 5059 C - Playing With Stones 博弈论Sg函数

    C - Playing With Stones Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu S ...

  3. UVA12293 Box Game —— SG博弈

    题目链接:https://vjudge.net/problem/UVA-12293 题意: 两人玩游戏,有两个盒子,开始时第一个盒子装了n个球, 第二个盒子装了一个球.每次操作都将刷量少的盒子的球倒掉 ...

  4. HDU 1848(sg博弈) Fibonacci again and again

    Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  5. uva1482:Playing With Stones (SG函数)

    题意:有N堆石子,每次可以取一堆的不超过半数的石子,没有可取的为输. 思路:假设只有一堆,手推出来,数量x可以表示为2^p-1形式的必输. 但是没什么用,因为最后要的不是0和1,而是SG函数:所以必输 ...

  6. LA 5059 (找规律 SG函数) Playing With Stones

    题意: 有n堆石子,两个人轮流取,每次只能取一堆的至少一个至多一半石子,直到不能取为止. 判断先手是否必胜. 分析: 本题的关键就是求SG函数,可是直接分析又不太好分析,于是乎找规律. 经过一番“巧妙 ...

  7. 【LA5059】Playing With Stones (SG函数)

    题意:有n堆石子,分别有a[i]个.两个游戏者轮流操作,每次可以选一堆,拿走至少一个石子,但不能拿走超过一半的石子. 谁不能拿石子就算输,问先手胜负情况 n<=100,1<=a[i]< ...

  8. Playing With Stones UVALive - 5059 Nim SG函数 打表找规律

    Code: #include<cstdio> #include<algorithm> using namespace std; typedef long long ll; ll ...

  9. hdu 1851(A Simple Game)(sg博弈)

    A Simple Game Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)Tot ...

随机推荐

  1. [Python Cookbook] Numpy: How to Apply a Function to 1D Slices along the Given Axis

    Here is a function in Numpy module which could apply a function to 1D slices along the Given Axis. I ...

  2. Java爬虫系列二:使用HttpClient抓取页面HTML

    爬虫要想爬取需要的信息,首先第一步就要抓取到页面html内容,然后对html进行分析,获取想要的内容.上一篇随笔<Java爬虫系列一:写在开始前>中提到了HttpClient可以抓取页面内 ...

  3. luogu P1018 乘积最大

    题目描述 今年是国际数学联盟确定的"2000――世界数学年",又恰逢我国著名数学家华罗庚先生诞辰90周年.在华罗庚先生的家乡江苏金坛,组织了一场别开生面的数学智力竞赛的活动,你的一 ...

  4. UVA - 10972 RevolC FaeLoN

    一道特别好的题qwq. 题目大意就是给你一个无向图,让你把边定向之后再加一些边使得这个图强连通,求最少需要加多少边. 一开始毫无头绪23333,数据范围让人摸不着头脑..... 然后开始画图,,,发现 ...

  5. dtrace 网站

    Oracle SQL Tuning and CBO Internals: Based Optimizer with CBO Internals and SQL Tuning Optimization ...

  6. mysql读写分离的三种实现方式

    1 程序修改mysql操作类可以参考PHP实现的Mysql读写分离,阿权开始的本项目,以php程序解决此需求.优点:直接和数据库通信,简单快捷的读写分离和随机的方式实现的负载均衡,权限独立分配缺点:自 ...

  7. Node之父ry发布新项目deno:下一代Node

    https://mp.weixin.qq.com/s/1LcO3EqGV2iRlZ1aIrQeqw

  8. linux下脚本监控网络流量

    linux下脚本监控网络流量 学习了:https://blog.csdn.net/chenghuikai/article/details/48437479 学习了:http://www.jb51.ne ...

  9. react className 有多个值时的处理 / react 样式使用 百分比(%) 报错

    1.react className 有多个值时的处理 <fieldset className={`${styles.formFieldset} ${styles.formItem}`}> ...

  10. 微型企业如何使用odoo

    作者 jeffery Q913547235 保留所有权利     Odoo可以帮助微型企业提升运营效率,做到电子化,信息化. 管理仓库进销存,建立收货单.交货单,并基于收货.交货情况确认应收款和应付款 ...