【题解】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
3
4
5
6
7
2
2
1 2
2 1
2
1 1
2 2

样例输出

1
2
Don't waste your time.
Have a try, lxhgww.

提示

思路

一个二维矩阵上,有若干个亮着的灯泡 。 每次选择一个矩阵(右上角的灯泡必须是亮的),改变四个角灯泡的状态,不能操作的选手判负。

Nim积定义

$ x y = sg(x, y) = mex{(a y) (x b) (a b), 0 a x, 0 b y} $

以下是对于 x, y≤4 的一个小表。

01234
000000
101234
202318
3031212
4048126

定义费马数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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using namespace std;

/*
Nim积 x @ y = mex{(a @ y) ^ (x @ b) ^ (a @ b)}, 0 <= a < x, 0 <= b < y

1. X x 2^(2^a) = X * 2^(2^a)
2. X x Y < 2^(2^a)
3. 2^(2^a) x 2^(2^a) = (3/2) * 2^(2^a)

模板一:调用 ans ^= f(x, y)
*/


int SG[20][20];

int f(int, int);
int g(int x, int y) // 计算2^x与2^y的nim积
{
if(SG[x][y] != -1) return SG[x][y];
if(!x) return SG[x][y] = 1<<y; // x==0也就是1与2^y的nim积,等于2^y
if(!y) return SG[x][y] = 1<<x;

int ans=1, t;
int xx=x, yy=y, k=1;
while(x || y) // 再将x和y分为二进制,这里计算那些普通乘积的(即对应二进制位不同的)
{
t = 1<<k; // 从此位得到的最终的数2^k
if((x^y)&1) ans *= t; // 该位不同
x>>=1; y>>=1; k<<=1; // 从此位得到的指数(本身也是2的幂)
}

x=xx; y=yy; k=1;
while(x || y) // 计算那些相同的fermat 2-power 数,与已得出的数的nim积
{
t = 1<<k;
if ((x&y)&1) ans = f(ans, t/2*3); // 该位相同
x>>=1; y>>=1; k<<=1; // 从此位得到的指数(本身也是2的幂)
}
return SG[xx][yy] = ans;
}

int f(int x, int y) //计算二位Nim积
{
if(!x || !y) return 0;
if(x == 1) return y;
if(y == 1) return x;

int ans=0;
for (int i=x, a=0; i; i>>=1, a++) //将x和二进制分解
{
if ((i&1)==0) continue; //该位是1才计算
for (int j=y, b=0; j; j>>=1, b++)
{
if ((j&1)==0) continue;
ans ^= g(a, b);
}
}
return ans;
}

/*
k为最大的整数满足 M = 2^(2^k) <= x
x = p * M + q, y = s * M + t
x @ y = spMM + sqM + tpM + tq
= M(sp+sq+tp) + tq + (M/2 @ sp)

模板二:调用 ans ^ Nim_Multi(x, y)
*/

int getSg(int x, int y){
if(!x || !y) return 0;
if(SG[x][y]!=-1) return SG[x][y];
bool S[1<<8] = {0};

for(int i=0; i<x; i++)
S[getSg(i, y)] = 1;

for(int i=0; i<y; i++)
S[getSg(x, i)] = 1;

for(int i=1; i<x; i++)
for(int j=1; j<y; j++)
S[getSg(i,y) ^ getSg(x,j) ^ getSg(i,j)] = 1;

int mex = 0;
while(S[mex]) mex++;
return SG[x][y]=mex;
}

int Nim_Multi_Power(int x, int y)
{
if (x < 16) return getSg(x, y);
int a=1, m;
for(; (1<<a) <= x; a<<=1);
a >>= 1; m = 1<<a;
int p = x/m, s = y/m, t = y&(m-1);
int d1 = Nim_Multi_Power(p, s);
int d2 = Nim_Multi_Power(p, t);
return ((d1^d2) << a) ^ Nim_Multi_Power(m/2, d1);
}

int Nim_Multi(int x, int y)
{
if (x < y) swap(x, y);
if (x < 16) return getSg(x, y);
int a=1, m;
for(; (1<<a) <= x; a<<=1);
a >>= 1; m = 1<<a;
int p = x/m, q = x&(m-1), s = y/ m, t = y&(m-1);
int c1 = Nim_Multi(p, s);
int c2 = Nim_Multi(p, t) ^ Nim_Multi(q, s);
int c3 = Nim_Multi(q, t);
return ((c1^c2) << a) ^ c3 ^ Nim_Multi_Power(m/2, c1);
}


int main()
{
memset(SG, -1, sizeof(SG));
int T; scanf("%d", &T);
while(T--)
{
int n; scanf("%d", &n);

int nim=0;
for(int i=0; i<n; i++){
int x, y;
scanf("%d %d", &x, &y);
//nim ^= f(x, y);
nim ^= Nim_Multi(x, y);
}
if(nim){
printf("Have a try, lxhgww.\n");
}else{
printf("Don't waste your time.\n");
}
}
return 0;
}