hdu 5795 A Simple Nim (sg函数)

本文探讨了ASimpleNim游戏的策略问题,这是一种两人轮流从多堆糖果中取糖果的游戏,可以拆分堆但不能不取。文章通过分析找到了获胜的关键规律,并给出了实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A Simple Nim

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 33    Accepted Submission(s): 18


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.
 
找到规律以后很好做,关键是打表的程序。
 
打表程序

 

#include <bits/stdc++.h>
using namespace std;
int g[1010];
void init() {
    memset(g, -1, sizeof(g));
}
int getSG(int x) {
    if(g[x] != -1) return g[x];
    if(x == 0) return 0;
    if(x == 1) return 1;
    if(x == 2) return 2;
    int vis[110];
    memset(vis, 0, sizeof(vis));
    for(int i = 1; i < x; i++) {
        int t = 0;
        int a = getSG(i);
        t ^= a;
        for(int j = 1; j < x - i; j++) {
            int tt = t;
            int b = getSG(j);
            int c = getSG(x - i - j);
            tt ^= b;
            tt ^= c;
            vis[tt] = 1;
            vis[c] = vis[b] = 1;
        }
        vis[a] = 1;
        vis[t] = 1;
    }
    vis[0] = 1;
    for(int i = 0; ; i++) if(!vis[i])
        return i;
}
int main() {
    int n;
    init();
    for(int i = 1; i <= 100; i++) {
        g[i] = getSG(i);
        printf("%d %d %d\n", i, i % 8, g[i]);
    }
}

 

ac代码

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
int getSG(int x) {
    if(x == 0)
        return 0;
    if(x % 8 == 0)
        return x - 1;
    if(x % 8 == 7)
        return x + 1;
    return x;
}

int a[maxn];

int main() {
    int t;
    scanf("%d", &t);
    while(t--) {
        int n;
        scanf("%d",&n);
        int ans = 0;
        for(int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
            ans ^= getSG(a[i]);
        }
        if(!ans)
            puts("Second player wins.");
        else
            puts("First player wins.");
    }
}

 

转载于:https://www.cnblogs.com/lonewanderer/p/5737674.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值