zoukankan      html  css  js  c++  java
  • HDU 5795 A Simple Nim(简单Nim)

    HDU 5795 A Simple Nim简单Nim

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

     

    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.

    两个玩家轮流从n堆糖中挑若干糖果,选到最后一颗糖的人赢得游戏。他们每回合他们可以在同一堆糖上取任意数量的糖果(不能不拿)。为了让游戏更有意思,玩家可以选择把某堆糖一分为三(没有空堆)而不去取糖果。在两人皆不犯错的情况下,找出游戏的赢家。
    CN

     

    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)

    多组测试用例。第一行是一个整数1≤T≤100,表示测试用例的数量。每组测试用例以一个整数n打头,表示糖果的堆数,随有N个整数s[0],s[1],....,s[n−1], 分别表示各堆糖果的数量。(1≤n≤10^6,1≤s[i]≤10^9)
    CN

    Output - 输出

    For each test case,output a line whick contains either"First player wins."or"Second player wins".

    对于每个测试用例,输出一行"First player wins.""Second player wins"
    CN

     

    Sample Input - 输入样例

    2
    2
    4 4
    3
    1 2 4
    

     

    Sample Output - 输出样例

    Second player wins.
    First player wins.
    

    题解

      暴力出奇迹,搜索是真知,打表找规律,反正SG……

     

    代码 C++

     1 #include <cstdio>
     2 #include <cstring>
     3 #define mx 100
     4 int sg[mx], w[mx << 2];
     5 void rdy(){
     6     int  i1, i2, i3, i, j;
     7     memset(sg, 0, sizeof(sg));
     8     for (i = 1; i < mx; ++i){
     9         memset(w, 0, sizeof(w));
    10         for (j = 0; j < i; ++j) w[sg[j]] = 1;
    11         if (i >= 3){
    12             for (i1 = 1; i1 * 3 <= i; ++i1){
    13                 for (i2 = i1; i1 + i2 * 2 <= i; ++i2){
    14                     i3 = i - i1 - i2;
    15                     if (i3 >= i2) w[sg[i1] ^ sg[i2] ^ sg[i3]] = 1;
    16                 }
    17             }
    18         }
    19         for (j = 0; w[j]; ++j);
    20         sg[i] = j;
    21     }
    22 }
    23 int main(){
    24     rdy();
    25     int n, i;
    26     for (i = 0; i < mx; ++i){
    27         if (sg[i] != i) printf("i=%d sg=%d
    ", i, sg[i]);
    28     }
    29     return 0;
    30 }
    打表围观

     

     1 #include <cstdio>
     2 int sg(int i){
     3     if (i % 8 == 0 || (i + 1) % 8 == 0){
     4         if (i & 1) ++i;
     5         else --i;
     6     }
     7     return i;
     8 }
     9 int main(){
    10     int t, n, s, opt;
    11     scanf("%d", &t);
    12     while (t--){
    13         scanf("%d", &n); opt = 0;
    14         for (opt = 0; n--; opt ^= sg(s)) scanf("%d", &s);
    15         if (opt) puts("First player wins.");
    16         else puts("Second player wins.");
    17     }
    18     return 0;
    19 }


     

     

  • 相关阅读:
    2
    spring jdbcTemplate 基本使用
    Day38-数据库入门学习
    DAY37-Python入门学习-进程池与线程池、协程、gevent模块
    Python入门学习-DAY36-GIL全局解释器锁、死锁现象与递归锁、信号量、Event事件、线程queue
    Python入门学习-DAY35-线程
    Python入门学习-DAY34-开启进程的两种方法、join方法、进程之间内存空间互相隔离、进程对象其他相关的属性或方法、僵尸进程与孤儿进程、守护进程、互斥锁
    Python入门学习-DAY33-基于UDP的套接字、socketserver模块、进程简单介绍
    Python入门学习-DAY32-链接循环与通信循环,粘包问题,远程控制,文件上传与下载
    Python入门学习-DAY31-三次握手原理与四次挥手,socket
  • 原文地址:https://www.cnblogs.com/Simon-X/p/5950697.html
Copyright © 2011-2022 走看看