1. 程式人生 > 實用技巧 >UVA12293 Box Game

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; }