zoukankan      html  css  js  c++  java
  • BZOJ 1874 取石子游戏

    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
    ");
    43     else return printf("NO
    "), 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
    ", i, b[j]);
    49     }
    50 }
    View Code
  • 相关阅读:
    Python统计字符串中出现次数最多的人名
    初探CORBA组件化编程
    shell脚本—基础知识,变量
    Java多线程--线程交替
    Qt中采用多线程实现Socket编程
    Python字符串格式化--formate()的应用
    JAVA中浅复制与深复制
    Python这些问题你会吗?
    PHP控制反转(IOC)和依赖注入(DI
    Go 语言指针
  • 原文地址:https://www.cnblogs.com/cychester/p/9638386.html
Copyright © 2011-2022 走看看