zoukankan      html  css  js  c++  java
  • HDU 5795:A Simple Nim(博弈)

    http://acm.hdu.edu.cn/showproblem.php?pid=5795

    A Simple Nim

    Problem Description
     
    Two players take turns picking candies from n heaps,the player who picks the last one will win the game.On each turn they can pick any number of candies which come from the same heap(picking no candy is not allowed).To make the game more interesting,players can separate one heap into three smaller heaps(no empty heaps)instead of the picking operation.Please find out which player will win the game if each of them never make mistakes.
     
    Input
     
    Intput contains multiple test cases. The first line is an integer 1T100, the number of test cases. Each case begins with an integer n, indicating the number of the heaps, the next line contains N integers s[0],s[1],....,s[n1], representing heaps with s[0],s[1],...,s[n1] objects respectively.(1n106,1s[i]109)
     
    Output
     
    For each test case,output a line whick contains either"First player wins."or"Second player wins".
     
    Sample Input
     
    2
    2
    4 4
    3
    1 2 4
     
    Sample Output
     
    Second player wins.
    First player wins.
     
    题意:和普通的Nim相似以外,还有一个操作,就是可以将一堆数量大于三的石头分成三份,每份可以不同(进行该操作的话该回合是不能取的)。
     1 #include <cstdio>
     2 #include <cstring>
     3 #define N 1000005
     4 int sg[N];
     5 /*
     6 官方题解:
     7 sg[0]=0
     8 
     9 当x=8k+7时sg[x]=8k+8,
    10 
    11 当x=8k+8时sg[x]=8k+7,
    12 
    13 其余时候sg[x]=x;(k>=0)
    14 
    15 打表找规律可得,数学归纳法可证。
    16 */
    17 void sg_do()
    18 {
    19     //学习打表找规律
    20     sg[0] = 0;
    21     bool vis[N];
    22     for(int i = 1; i <= 100; i++) {
    23         memset(vis, 0, sizeof(vis));
    24         for(int j = 1; j <= i; j++) {
    25             vis[sg[i-j]] = 1;
    26             //拿走的情况
    27         }
    28         if(i >= 3) {
    29             //只要对分成的三部分取异或,就代表x所能转移到的下一状态。
    30             for(int j = 1; j <= i - 2; j++) {
    31                 for(int k = 1; k <= i - 2; k++) {
    32                     if(j + k < i) {
    33                         vis[sg[i-j-k]^sg[j]^sg[k]] = 1;
    34                         //将一堆拆成三份的情况
    35                     }
    36                 }
    37             }
    38         }
    39         int j = 0;
    40         while(vis[j]) j++;
    41         sg[i] = j;
    42         printf("%d : %d
    ", i, sg[i]);
    43     }
    44 }
    45 
    46 int main()
    47 {
    48 //    sg_do();
    49     int t;
    50     scanf("%d", &t);
    51     while(t--) {
    52         int n, sg, x, ans = 0;
    53         scanf("%d", &n);
    54         for(int i = 1; i <= n; i++) {
    55             scanf("%d", &x);
    56             if((x+1) % 8 == 0) sg = x + 1;
    57             else if(x % 8 == 0) sg = x - 1;
    58             else sg = x;
    59             ans ^= sg;
    60         }
    61         if(ans == 0) puts("Second player wins.");
    62         else puts("First player wins.");
    63     }
    64     return 0;
    65 }
  • 相关阅读:
    javascript运算符
    javascript字符串转数字
    javascript的变量声明和数据类型
    javascript的历史和入门
    CSS中定位
    CSS中盒子模型
    CSS操作表格的边框和表格的属性示例代码
    常用的CSS样式示例代码
    CSS伪类选择器
    CSS选择器
  • 原文地址:https://www.cnblogs.com/fightfordream/p/5757171.html
Copyright © 2011-2022 走看看