题目链接:https://vjudge.net/problem/UVA-12293

题意:

两人玩游戏,有两个盒子,开始时第一个盒子装了n个球, 第二个盒子装了一个球。每次操作都将刷量少的盒子的球倒掉,然后再从数量多的盒子中拿出若干个球放到空盒子里,最终状态为(1,1),达到这个状态的玩家获胜。

题解:

1.由于每次都是倒掉数量少的那个盒子,再对数量多的盒子进行分割,所以可以把规则简化为:初始时有n个球,每次只能拿走不多于n/2的球,最终状态为1个球,达到这个状态的玩家获胜。

2.简化游戏规则之后,可知这是一个典型的SG博弈,但是由于n的范围很大,不能直接求SG值,那就打表找规律,如下:

可知,当n为 2^i - 1时,先手输;否则先手赢。

代码一:

 #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[] = ;
for(int i = ; i<=; i++)
{
memset(vis, , sizeof(vis));
for(int j = (i+)/; j<i; j++) vis[SG[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');
/*
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 31 32
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 16
*/
} bool judge(int x)
{
x++;
int bit = ;
while(x)
{
bit += x&;
x >>= ;
}
return bit==;
} int main()
{
// table();
int n;
while(scanf("%d", &n) &&n)
{
if(judge(n)) printf("Bob\n");
else printf("Alice\n");
}
}

代码二:

 #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[] = ;
for(int i = ; i<=; i++)
{
memset(vis, , sizeof(vis));
for(int j = (i+)/; j<i; j++) vis[SG[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');
/*
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 31 32
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 16
*/
} int main()
{
// table();
int n;
while(scanf("%d", &n) &&n)
{
if(n&(n+)) printf("Alice\n");
else printf("Bob\n");
}
}

UVA12293 Box Game —— SG博弈的更多相关文章

  1. UVA1482 Playing With Stones —— SG博弈

    题目链接:https://vjudge.net/problem/UVA-1482 题意: 有n堆石子, 每堆石子有ai(ai<=1e18).两个人轮流取石子,要求每次只能从一堆石子中抽取不多于一 ...

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

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

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

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

  4. HDU 1536 S-Nim SG博弈

    S-Nim Problem Description   Arthur and his sister Caroll have been playing a game called Nim for som ...

  5. POJ 3710 Christmas Game#经典图SG博弈

    http://poj.org/problem?id=3710 (说实话对于Tarjan算法在搞图论的时候就没搞太懂,以后得找时间深入了解) (以下有关无向图删边游戏的资料来自论文贾志豪<组合游戏 ...

  6. TZOJ 2703 Cow Digit Game(sg博弈)

    描述 Bessie is playing a number game against Farmer John, and she wants you to help her achieve victor ...

  7. hdu 1517 A Multiplication Game 段sg 博弈 难度:0

    A Multiplication Game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

  8. hdu 1848(Fibonacci again and again)(SG博弈)

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

  9. POJ 3537 Crosses and Crosses(sg博弈)

    题目:在1*n 的棋盘里面,A和B都在里面画叉 , 如果谁可以画了一个叉后,可以连成3个叉,那谁胜利 : 分析: 首先考虑如果我在玩游戏,我最希望对手可以画出-x-x or  -xx-   ,  这种 ...

随机推荐

  1. android添加桌面悬浮窗

    1. 添加权限 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> 2. ...

  2. 我的第一个Java程序HelloWorld

    public class HelloWorld{ public static void main(String [] args){ System.out.println("HelloWorl ...

  3. java多线程03-----------------volatile内存语义

    java多线程02-----------------volatile内存语义 volatile关键字是java虚拟机提供的最轻量级额的同步机制.由于volatile关键字与java内存模型相关,因此, ...

  4. Android 打开其他程序

    Intent intent = new Intent(); intent.setComponent(new ComponentName("所要打开的程序包名", "所要打 ...

  5. IOS 教你玩转UITableViewController和TableView

    iphone和Ipad开发中UITableViewController和TableView应该是用得比較多得控件. 可是你是会由于写这些控件写得多了而厌烦. 全部怎么让这个控件一直能用.怎么让这个控件 ...

  6. PS 如何用制作键盘图标

    1 键盘可以大致分为笔记本键盘和台式机键盘,颜色一般是黑色或白色.不同的键盘,拍摄角度不同(俯视或者平视)得到的效果也不一样.一般我们根据自己需要得到需要的键盘形式.比如下面别人制作的一套立体键盘,立 ...

  7. C++11 并发指南四(<future> 详解一 std::promise 介绍)(转)

    前面两讲<C++11 并发指南二(std::thread 详解)>,<C++11 并发指南三(std::mutex 详解)>分别介绍了 std::thread 和 std::m ...

  8. css hack(部分)

    一.去掉图片间隙:hack1.img{ display:block: }hack2.将<div></div>与<img>写在同一行 二.ie6双倍浮向(双倍边距)出 ...

  9. hibernate5(10)注解映射[2]一对多单向关联

    在上一篇文章里.我们从端方向一端建立关联关系,完毕了从文章到作者的关联关系建立.但在实际的博客站点中,用户肯定还须要获取自己所写的文章,这时能够建立用户(一)对文章(多)的单向关联映射. 先来看我们的 ...

  10. POJ 3335 Rotating Scoreboard(半平面交 多边形是否有核 模板)

    题目链接:http://poj.org/problem? id=3335 Description This year, ACM/ICPC World finals will be held in a ...