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

 

转载于:https://www.cnblogs.com/cychester/p/9638386.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值