Topcoder SRM 701 Div2-900 ThueMorseGame(博弈+預處理)
阿新 • • 發佈:2017-10-11
n) const ons ret 一個人 || spa 進制 fin
題意 Alice和Bob在玩一個遊戲,Alice先手。
每次一個人可以從一堆式子中拿走任意數量(不超過m)的式子。
取走最後一顆式子的人勝利。
當一個取完某一步的時候剩下的石子數量的二進制表示中1的個數為奇數時,這個人直接輸。
$n <= 5* 10^{8}, m <= 50$
考慮博弈
我們可以用一個01數組來表示之前50步的勝敗狀態。
因為m <= 50,可以直接用一個long long 來替換掉這個數組。
求二進制表示中1的個數的時候,采用預處理的方式,拆成前16位和後16位就可以了。
其他都是博弈的老套路。
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i) #define dec(i, a, b) for (int i(a); i >= (b); --i) #define MP make_pair #define fi first #define se second typedef long long LL; const int N = 1e6 + 10; int pre[N]; class ThueMorseGame { public: string get(int n, int m){ LL all = (1LL << m) - 1; LL cnt = all; rep(i, 0, (1 << 16) - 1) pre[i]= __builtin_popcount(i); rep(i, 0, n) cnt = ((cnt << 1) & all) | (((pre[i >> 16] + pre[i & ((1 << 16) - 1)]) & 1) || (cnt ^ all)); return cnt & 1 ? "Alice" : "Bob"; } };
Topcoder SRM 701 Div2-900 ThueMorseGame(博弈+預處理)