# 博弈论学习笔记

## 博弈论学习总结

### Nim游戏

Nim游戏就是经典的取石子游戏，规则如下：

### Sg函数

• 每个点对应一种状态
• 每条边对应一种移动

$sg(v) = mex{sg(u)|$图中有一条v到u的边$}$

#### 定义P-position和N-position

• 无法进行任何移动的局面是P-position
• 可以移动到P-position的局面是N-position
• 所有移动都导致N-position的局面是P-position
• P即先手必败
• N即先手必胜
• 一个状态为P状态当且仅当它的sg值为0

### Anti-Sg

Anti-SG 游戏规定，决策集合为的游戏者赢。
Anti-SG 其他规则与 SG 游戏相同

#### SJ 定理

（1）游戏的 SG 函数不为 0 且游戏中某个单一游戏的 SG 函数大于 1；

（2）游戏的 SG 函数 为 0 且游戏中没有单一游戏的 SG 函数大于 1

### Sg函数模板

#### Sg打表

size求解范围 f组是可以每次取的值,nf的长度。

#### dfs

n是集合f的大小，f[i]是定义的特殊取法规则的数组

### 例题

#### S-Nim【hdu1536】

hdu1536
Problem Description
Arthur and his sister Caroll have been playing a game called Nim for some time now. Nim is played as follows:
The starting position has a number of heaps, all containing some, not necessarily equal, number of beads.
The players take turns chosing a heap and removing a positive number of beads from it.
The first player not able to make a move, loses.
Arthur and Caroll really enjoyed playing this simple game until they recently learned an easy way to always be able to find the best move:
Xor the number of beads in the heaps in the current position (i.e. if we have 2, 4 and 7 the xor-sum will be 1 as 2 xor 4 xor 7 = 1).
If the xor-sum is 0, too bad, you will lose.
Otherwise, move such that the xor-sum becomes 0. This is always possible.
It is quite easy to convince oneself that this works. Consider these facts:
The player that takes the last bead wins.
After the winning player’s last move the xor-sum will be 0.
The xor-sum will change after every move.
Which means that if you make sure that the xor-sum always is 0 when you have made your move, your opponent will never be able to win, and, thus, you will win.
Understandibly it is no fun to play a game when both players know how to play perfectly (ignorance is bliss). Fourtunately, Arthur and Caroll soon came up with a similar game, S-Nim, that seemed to solve this problem. Each player is now only allowed to remove a number of beads in some predefined set S, e.g. if we have S =(2, 5) each player is only allowed to remove 2 or 5 beads. Now it is not always possible to make the xor-sum 0 and, thus, the strategy above is useless. Or is it?
your job is to write a program that determines if a position of S-Nim is a losing or a winning position. A position is a winning position if there is at least one move to a losing position. A position is a losing position if there are no moves to a losing position. This means, as expected, that a position with no legal moves is a losing position.
Input
Input consists of a number of test cases. For each test case: The first line contains a number k (0 < k \leq 100 describing the size of S, followed by k numbers si (0 < si \leq 10000) describing S. The second line contains a number m (0 < m \leq 100) describing the number of positions to evaluate. The next m lines each contain a number l (0 < l \leq 100) describing the number of heaps and l numbers hi (0 \leq hi \leq 10000) describing the number of beads in the heaps. The last test case is followed by a 0 on a line of its own.

#### Climbing the Hill【hdu4315】

hdu4315

• 考虑与Nim游戏的转化，如果$k = 1$，那么显然先手必胜。
• 先考虑一个简化问题：不考虑king，当不能移动时为败。
• 首先注意到一个必败：如果n为偶数，且所有奇数位的人与其后的人均紧靠，那么此时为必败态，因为不论先手如何移动（只可能移动奇数位），后手必然可以移动偶数位使其依然保持紧靠。如果加入考虑king，那么k为偶数时，同上讨论，先手必败，如果k为奇数，那么可以把游戏看为移动k - 1位置的人到1（由前讨论，后手可以达到这一目标），故k为奇数时仍然为先手必败态。
• 此时我们就可以考虑如何将游戏进行至必败态，如果先手移动奇数位，那么后手总可以移动偶数位使得相隔距离不变，所以问题可以只考虑奇数位和其后相邻的偶数位的间距，把他们看成Nim堆，移动对应减少石子，当石子数为0时就是紧靠的必败态。
• n为奇数时，如果k不等于2，那么可以把第一个人到山顶看成一堆石子，当Nim游戏结束时第一个人到达山顶，情况就变得和偶数时一样。
• 如果k等于2，那么第一个人到山顶就是必败态，故第一个人最多能到1的位置，第一堆石子数减一。
#