BZOJ 1874 取石子游戏 - SG函数

Description

$N$堆石子, $M$种取石子的方式, 最后取石子的人赢, 问先手是否必胜

$A_i <= 1000$,$ B_i <= 10$

Solution

由于数据很小, 直接暴力求SG函数即可判断。

Code

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define rd read()
 5 using namespace std;
 6 
 7 const int N = 1e3 + 5;
 8 
 9 int n, m, a[105], b[N];
10 int SG[N], S[105];
11 
12 int read() {
13     int X = 0, p = 1; char c = getchar();
14     for(;c > '9' || c < '0'; c = getchar()) if(c == '-') p = -1;
15     for(; c >= '0' && c <= '9'; c = getchar()) X = X * 10 + c - '0';
16     return X * p;
17 }
18 
19 void init() {
20     for(int i = 0; i <= 1000; ++i) {
21         int tmp = 0;
22         memset(S, 0, sizeof(S));
23         for(int j = 1; j <= m && b[j] <= i; ++j) 
24             S[SG[i - b[j]]] = 1;
25         for(; S[tmp]; ++tmp);
26         SG[i] = tmp;
27     }
28 }
29 
30 int main()
31 {
32     n = rd;
33     for(int i = 1; i <= n; ++i)
34         a[i] = rd;
35     m = rd;
36     for(int i = 1; i <= m; ++i)
37         b[i] = rd;
38     init();
39     int ans = 0;
40     for(int i = 1; i <= n; ++i)
41         ans ^= SG[a[i]];
42     if(ans) printf("YES\n");
43     else return printf("NO\n"), 0;
44     for(int i = 1; i <= n; ++i) {
45         int tmp = ans ^ SG[a[i]];
46         for(int j = 1; j <= m && b[j] <= a[i]; ++j)
47             if((tmp ^ SG[a[i] - b[j]]) == 0)
48                 return printf("%d %d\n", i, b[j]);
49     }
50 }
View Code

猜你喜欢

转载自www.cnblogs.com/cychester/p/9638386.html
今日推荐