【题解】HDU-3404 Switch lights
Switch lights(HDU-3404)
题面
lxhgww is playing a game with his computer Deep Blue. The game is played on a matrix containing lights. At first, some lights are on, while others are off. lxhgww and Deep Blue take turns to switch the lights. For each step, the player should choose a rectangle in the matrix: (x1 , y1) , (x1 , y2) , (x2 , y1) , (x2 , y2) , (x1<=x2,y1<=y2, the light at (x2, y2) should be on) and change the lights’ status on the four vertex of the rectangle, namely on to off, and off to on. The player turns all the lights off wins the game. Notice the rectangle is possibly degenerated to line or even a single cell so that the player may also switch two or one besides four lights in a move. Deep Blue's strategy is perfect, if it has a chance to win, never will it lose. Does lxhgww have a chance to win if he takes the first step?
输入
The first line is an integer T(T<=100) indicating the case number. Each case has one integers n (n<= 1000 ), the number of on-lights at the beginning of the game. Then come n lines, each line has two integers, xi , yi, (1<=xi<=10000, 1<=yi<=10000) , so light at (xi, yi) is on at first. (No two lights at the same position)
输出
If lxhgww still has a chance to win, output "Have a try, lxhgww.", otherwise tell lxhgww "Don't waste your time."
样例输入
1 | 2 |
样例输出
1 | Don't waste your time. |
提示
无
思路
一个二维矩阵上,有若干个亮着的灯泡 。 每次选择一个矩阵(右上角的灯泡必须是亮的),改变四个角灯泡的状态,不能操作的选手判负。
Nim积定义
$ x y = sg(x, y) = mex{(a y) (x b) (a b), 0 a x, 0 b y} $
以下是对于 x, y≤4 的一个小表。
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 0 |
1 | 0 | 1 | 2 | 3 | 4 |
2 | 0 | 2 | 3 | 1 | 8 |
3 | 0 | 3 | 1 | 2 | 12 |
4 | 0 | 4 | 8 | 12 | 6 |
定义费马数Fermat 2-power为$ 2{2{k}} \(,对于\) x,y < 2{2{k}} $有以下性质
一个 Fermat 2-power 与任意小于它的数的 Nim 积为一般意义下乘法的积,即$ x {2{k}} = x * 2{2{k}} $
一个 Fermat 2-power 与自己的 Nim 积为自己的 $ $ 倍,即 $ 2{2{k}} {2{k}} = * 2{2{k}}$
$ x y < 2{2{k}} $
根据性质可以得出递归求Nim积的板子。
详细参见论文《从“k倍动态减法游戏”出发探究一类组合游戏问题》。
代码
1 | using namespace std; |