UVA12293 Box Game

UVA12293 Box Game

题意

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

题解

显然原问题等价于有n个石子,每次至少拿一个,至多拿一半。

对于这个问题:

当n为奇数的时候 SG(n) = SG(n/2)

当n为偶数的时候SG(n) = n/2

 

#include<bits/stdc++.h>

using namespace std;

inline int read()
{
    int f = 1 , x = 0;
    char ch;
    do
    {
        ch = getchar();
        if(ch == '-') f = -1; 
    }while(ch < '0' || ch > '9');
    do
    {
        x = (x<<3) + (x<<1) + ch - '0';
        ch = getchar();
    }while(ch >= '0' && ch <= '9');
    return f*x;
} 
 
int n;
 
inline int SG(int x)
{
     if(x&1) return SG(x/2);
     else return x/2;
} 
 
int main()
{
     while(1)
     {
         n = read();
         if(!n) break;
         if(SG(n) == 0) cout << "Bob" << endl;
         else cout << "Alice" << endl;
    }
    return 0;
}

 

posted @ 2020-07-15 11:50  wlzs1432  阅读(124)  评论(0编辑  收藏  举报