HDU 5795 A Simple Nim(博弈+找规律)

A Simple Nim

题目连接

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

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.

Hint

题意

n堆石子,两个人轮流操作,每次可以从一堆中取若干个石子,或把某一堆石子分成3份,每份不小于0,最先取完的人获胜。

题解

sg[0]=0

当x=8k+7时sg[x]=8k+8,

当x=8k+8时sg[x]=8k+7,

其余时候sg[x]=x;(k>=0)

打表调用wtf函数

代码

#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef __int64 LL;
typedef pair<int,int> PII;
#define FIN freopen("in.txt", "r", stdin);
#define FOUT freopen("out.txt", "w", stdout);
#define lson l, mid, cur << 1
#define rson mid + 1, r, cur << 1 | 1
//#pragma comment(linker, "/STACK:1024000000,1024000000")
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const double EXP = 1e-8;
const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 50;
const int MAXM = 1e6 + 50;

void wtf()
{
    LL sg[205];
    int vis[205];
    memset(sg, 0, sizeof(sg));
    for (int i = 1; i <= 200; i++)
    {
        memset(vis, 0, sizeof(vis));
        for (int a = 1; a <= i; a++)
            for (int b = 1; b <= i; b++)
            {
                int c = i - a - b;
                if (c > 0)
                    vis[sg[a] ^ sg[b] ^ sg[c]] = 1;
            }
        for (int j = 0; j < i; j++)
            vis[sg[j]] = 1;
        for (int j = 0; j <= 200; j++)
            if (vis[j] == 0)
            {
                sg[i] = j;
                break;
            }
    }
    for (int i = 1; i <= 50; i++)
        printf("%d %lld\n", i, sg[i]);
}

int main()
{
#ifdef LOCAL_NORTH
    FIN;
#endif // LOCAL_NORTH
//    wtf();
    int tcase, n, tmp;
    scanf("%d", &tcase);
    while (tcase--)
    {
        scanf("%d", &n);
        int ans = 0;
        for (int i = 1; i <= n; i++)
        {
            scanf("%d", &tmp);
            if (tmp % 8 == 7)
                tmp++;
            else if (tmp % 8 == 0)
                tmp--;
            ans ^= tmp;
        }
        printf("%s\n", ans ? "First player wins." : "Second player wins.");
    }
#ifdef LOCAL_NORTH
    cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC * 1000 << " ms." << endl;
#endif // LOCAL_NORTH
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值