Cow Digit Game(博弈论:sg函数)
鏈接:https://ac.nowcoder.com/acm/contest/1071/G
來源:牛客網
時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 32768K,其他語言65536K
64bit IO Format: %lld
題目描述
Bessie is playing a number game against Farmer John, and she wants you to help her achieve victory.
Game i starts with an integer Ni (1 <= Ni <= 1,000,000). Bessie goes first, and then the two players alternate turns. On each turn, a player can subtract either the largest digit or the smallest non-zero digit from the current number to obtain a new number. For example, from 3014 we may subtract either 1 or 4 to obtain either 3013 or 3010, respectively. The game continues until the number becomes 0, at which point the last player to have taken a turn is the winner.
Bessie and FJ play G (1 <= G <= 100) games. Determine, for each game, whether Bessie or FJ will win, assuming that both play perfectly (that is, on each turn, if the current player has a move that will guarantee his or her win, he or she will take it).
Consider a sample game where Ni = 13. Bessie goes first and takes 3, leaving 10. FJ is forced to take 1, leaving 9. Bessie takes the remainder and wins the game.
輸入描述:
- Line 1: A single integer: G
- Lines 2…G+1: Line i+1 contains the single integer: Ni
輸出描述:
- Lines 1…G: Line i contains ‘YES’ if Bessie can win game i, and ‘NO’ otherwise.
示例1
輸入
復制
輸出
復制
說明
For the first game, Bessie simply takes the number 9 and wins. For the second game, Bessie must take 1 (since she cannot take 0), and then FJ can win by taking 9.
思路
博弈論
本題最后取完者勝,
所以終態n=0為先取者的必敗點,
可以用sg函數來求解。
sg函數說明:
首先定義mex(minimal excludant)運算,這是施加于一個集合的運算,表示最小的不屬于這個集合的非負整數。例如mex{0,1,2,4}=3、mex{2,3,5}=0、mex{}=0。
對于任意狀態 x , 定義 SG(x) = mex(S),其中 S 是 x 后繼狀態的SG函數值的集合。如 x 有三個后繼狀態分別為 SG(a),SG(b),SG( c),那么SG(x) = mex{SG(a),SG(b),SG( c)}。 這樣 集合S 的終態必然是空集,所以SG函數的終態為 SG(x) = 0,當且僅當 x 為必敗點P時。
---------------from sg函數詳解傳送門
總結
以上是生活随笔為你收集整理的Cow Digit Game(博弈论:sg函数)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 素数相关模板
- 下一篇: hdu1847(博弈论:sg函数)