HDU 5795 A Simple Nim 打表求SG函数的规律

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 1≤T≤100, 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[n−1], representing heaps with s[0],s[1],...,s[n−1] objects respectively.(1≤n≤106,1≤s[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.

题意:

  给你n堆糖果

  A,B两个人可以像nim游戏那样,在一堆中取任意个糖果

  但是此题新增了, 玩家可以将一堆糖果分成三个非空堆,并且不取

  最后取完的胜利;

题解:

  像这种暴力求SG函数会超时的

  咋们就打表找规律

  打表程序已给出,规律你就自己找吧

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double Pi = acos(-1.0);
const int N = 5e5+10, M = 2e5+20, mod = 1e9+7, inf = 2e9;

int sg[N],vis[N];
int main() {
        /*sg[0] = 0;
        for(int i = 1; i <= 100; ++i) {
            memset(vis,0,sizeof(vis));
            for(int j = 0; j < i; ++j) vis[sg[j]] = 1;
            for(int j = 1; j <= i; ++j){
                for(int jj = 1; jj <= i; ++jj) {
                    for(int k = 1; k <= i; ++k) {
                        if(j + jj + k == i) {
                            vis[sg[j] ^ sg[jj] ^ sg[k]] = 1;
                        }
                    }
                }
            }
            for(int j = 0; j <= 1000; ++j) {
                if(!vis[j]) {
                    sg[i] = j;
                    break;
                }
            }
            cout<<i<<": " << sg[i]<<endl;
        }
        */
        int T,n,x;
        scanf("%d",&T);
        while(T--) {
            scanf("%d",&n);
            int ans = 0;
            for(int i = 1; i <= n; ++i) {
                scanf("%d",&x);
                if((x+1)%8 == 0) {
                    ans ^= (x+1);
                } else if(x % 8 == 0) ans ^= (x-1);
                else ans ^= x;
            }
            if(!ans) puts("Second player wins.");else puts("First player wins.");

        }
        return 0;
}
时间: 2024-08-05 02:13:23

HDU 5795 A Simple Nim 打表求SG函数的规律的相关文章

?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 e

hdu 5795 A Simple Nim (sg 博弈)

官方题解: A Simple Nim sg[0]=0 当x=8k+7时sg[x]=8k+8, 当x=8k+8时sg[x]=8k+7, 其余时候sg[x]=x:(k>=0) 打表找规律可得,数学归纳法可证. 具体的打表放在了代码里面 ,详见init函数 /*by*/ #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace s

HDU 5795 A Simple Nim (博弈) ---2016杭电多校联合第六场

A Simple Nim Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 79    Accepted Submission(s): 48 Problem Description Two players take turns picking candies from n heaps,the player who picks the las

hdu 5795 A Simple Nim 博弈sg函数

A Simple 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 an

HDU 5794 A Simple Nim 2016多校第六场1003

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5795 题意:给你n堆石子,每一堆有ai石头,每次有两种操作,第一种在任意一堆取出任意数量的石头但不能为0,第二种把一堆石头分成三堆任意数量的石头(不能为0),问是先手赢还是后手赢. 题解:这就是一个Nim游戏!那么Nim游戏一般跟SG函数有关,所以这道题打表找规律即可. 关于SG函数,在这里也说一下吧!毕竟第一次接触. Nim游戏与SG函数:http://baike.baidu.com/link?u

HDU 3032 Nim or not Nim? (博弈之求SG函数)

题意:经典Nim博弈游戏变换,给你n堆石子pi,每堆有pi个石子, Alice和Bob轮流取石子,每次可以从任意一堆中拿走任意个石子,也可以将某一堆石子分成两个小堆 (每堆石子个数必须不能为0),先拿完者获胜 思路:求SG函数后找规律: SG函数定义及求法:点击打开链接 #include<cstdio> #include<stdlib.h> #include<string.h> #include<string> #include<map> #in

HDU 1536 S-Nim 求SG函数

题意:给你n个数Nnum[ i ],表示每次只能取Nnum[ i ]个数. m个问题:每次给你 l 堆石子,每堆有num个石子,问先手是否会赢. Sample Input 2 2 5 3 2 5 12 3 2 4 7 4 2 3 7 12 5 1 2 3 4 5 3 2 5 12 3 2 4 7 4 2 3 7 12 0 Sample Output LWW WWL 经典Nim游戏,找出SG就可以了. 至于如何找SG,这里有详细的 点我 #include<cstdio> #include<

HDU 1517 A Multiplication Game (博弈-求sg)

A Multiplication Game Problem Description Stan and Ollie play the game of multiplication by multiplying an integer p by one of the numbers 2 to 9. Stan always starts with p = 1, does his multiplication, then Ollie multiplies the number, then Stan and

UVALive 5059 Playing With Stones(求sg函数)

题意和nim游戏差不多,就是取石子的时候最多只能拿原来的一半,比如一堆5个石子最多拿两个. 先用打表的方式看出前面一部分的sg值,然后找规律来做. 打表求sg值的程序才是最重要的. #include<cstdio> #include<cstring> #define ll long long int main() { int vis[50]; int sg[50]; sg[1] = 0; for(int i = 2; i <= 30; i++) { memset(vis, 0