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 std;
typedef long long LL;
const LL N=1e6+10;
const LL mod=1000000007;
const LL INF=0x3f3f3f;
LL sg[300],vis[300];
void init()/*打表求前300的sg函数找规律*/
{
    int i,j,k;
    for(i=1; i<300; i++) {
        memset(vis,0,sizeof(vis));
        for(j=1; j<=i; j++) {
            for(k=1; j+k<=i; k++) {
                if((i-j-k)!=0) {
                    vis[sg[j]^sg[k]^sg[i-j-k]]=1;
                }
            }
        }
        for(j=0; j<i; j++)
            vis[sg[j]]=1;
        j=0;
        while(vis[j])
            j++;
        sg[i]=j;
    }
    for(i=0; i<30; i++)
        printf("sg[%d]=%lld\n",i,sg[i]);
}
int main()
{
    //init();
    int T;
    cin>>T;
    while(T--) {
        int n,i;
        LL a[N],ans=0,tmp;
        scanf("%d",&n);
        /*ans进行结果的异或*/
        for(i=1; i<=n; i++) {
            scanf("%lld",a+i);
            tmp=a[i];
            if(a[i]%8==7)
                tmp++;
            else if(a[i]%8==0)
                tmp--;
            ans^=tmp;
        }
        if(!ans)
            printf("Second player wins.\n");
        else
            printf("First player wins.\n");
    }
    return 0;
}
时间: 2024-10-12 19:24:49

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 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 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).T

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

HDU5795A Simple Nim SG定理

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

HDU 4994 Revenge of Nim (博弈)

题目链接:HDU 4994 Revenge of Nim 题意:两个取石头,分别在N堆里去.只有第一堆取完才能取第二堆,以此类推,最后一个取完的为赢家. 思路:从头开始扫,直到第一个不为1为止,判断现在的主动权在谁手里,谁就是赢家.(这里读者可以自己写几组数据试试.) AC代码: #include<stdio.h> #include<string.h> int main() { int yaoga; int t,i,n; int a[1010]; while(scanf("

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 4994 Revenge of Nim(博弈)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4994 Problem Description Nim is a mathematical game of strategy in which two players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remov

HDU 3032-Nim or not Nim?(sg函数打表)

Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1099    Accepted Submission(s): 547 Problem Description Nim is a two-player mathematic game of strategy in which players take tur