zoukankan      html  css  js  c++  java
  • HDU 5795


    题意:

    两人轮流从 n 堆 糖果里取糖果,每次可以
    1) 选择一堆取任意个(不为 0)
    2) 选择一堆糖果分成 3 堆(每堆数量 >= 1)
    拿到最后一颗糖果的人赢。

    解题 :

    打表算出一些 sg 值,就可以发现规律,

    sg(0) = 0; sg(1) = 1; sg(2)= 2;
    当 x >= 3 时,后继中就有分堆的情况辣
    sg(3) = mex(sg(0),sg(1),sg(2),sg(1,1,1)) = 3;
    sg(4) = mex(sg(0),sg(1),sg(2),sg(3),sg(1,1,2)) = 4;
    ...
    sg(7) = 8;
    sg(8) = 7;
    ...
    sg(15) = 14;
    sg(16) = 15;
    ...
    所以 规律为
    若 x = 8k + 7,sg(x) = x + 1; (k>=0)
    若 x = 8k + 8, sg(x) = x - 1; (k>=0)
    其余情况 ,sg(x) = x ;

    开始自己找的是 8k-1 和 8k+1,也过了

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <iostream>
    #include <string>
    #include <map>
    #include <cmath>
    using namespace std;
    int a[1000010];
    int main()
    {
        int t,n;
        scanf("%d",&t);
        while(t--) {
            int ans = 0;
            scanf("%d",&n);
            for(int i=0;i<n;i++) {
                scanf("%d",&a[i]);
                if((a[i]-7)%8 == 0)  a[i] += 1;            
                else if( ((a[i] -8) % 8 ) == 0) a[i] -= 1;
                ans ^= a[i];
            }
            printf((ans == 0)?"Second player wins.
    ":"First player wins.
    ");
        }
        return 0;
    }
  • 相关阅读:
    C#中async/await中的异常处理
    Xunit
    Markdown安装与简单使用
    .Net Ajax跨域请求总结
    centos 安装 Vmare tool
    linux安装Java
    linux常用命令整理
    autofac解析Mvc和Webapi的坑
    swarm on ubuntu
    deploy service on swarm
  • 原文地址:https://www.cnblogs.com/ember/p/5741719.html
Copyright © 2011-2022 走看看