HDU 5795 A Simple Nim(SG打表找规律)

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.

一开始思路错了。。后来发现打了个表分分钟ac。。。

规律:

sg[8k] = 8k-1;
sg[8k-1] = 8k;
sg[other] = other;

ac代码:

#include <bits/stdc++.h>

using namespace std;

#define rep(i,a,n) for(int i = (a); i < (n); i++)
#define per(i,a,n) for(int i = (n)-1; i >= (a); i--)
#define clr(arr,val) memset(arr, val, sizeof(arr))
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define pi acos(-1)
typedef pair<int, int> pii;
typedef long long LL;
const int maxn = 1006;
const double eps = 1e-8;
const int mod = 1000000007;
int a[1000006];
int main(){
    int t, n;
    cin >> t;
    while (t--) {
        cin >> n;
        int ans = 0;
        bool f = true;
        rep(i, 0, n){
            scanf("%d", &a[i]);
            if(a[i]%8==0) ans^=a[i]-1;
            else if(a[i]%8==7) ans^=a[i]+1;
            else ans ^=a[i];
        }
        puts(ans?"First player wins.":"Second player wins.");

    }
    return 0;
}

打表代码:

#include <bits/stdc++.h>

using namespace std;

#define rep(i,a,n) for(int i = (a); i < (n); i++)
#define per(i,a,n) for(int i = (n)-1; i >= (a); i--)
#define clr(arr,val) memset(arr, val, sizeof(arr))
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define pi acos(-1)
typedef pair<int, int> pii;
typedef long long LL;
const int maxn = 1006;
const double eps = 1e-8;
const int mod = 1000000007;
int sg[1000006];
bool vst[1000006];
void take_part(int n)
{
     for(int i = 1; i <= n / 3; i++)
     {
         for(int j = 1; j <= n / 3; j++){
             int yihuo = 0;
             yihuo ^= sg[i] ^ sg[n - j - i] ^ sg[j];
             vst[yihuo] = true;
        }
     }
}

 void get_sg()
 {
     for(int i = 3; i < 1000; i++)
     {
         memset(vst, false, sizeof(vst));
         int j = 0;
         while(j++ < i)
         {
             vst[sg[j]] = true;
         }
         take_part(i);
         for(int j = 0; j < 2000; j++)
         {
             if(!vst[j])
             {
                 sg[i] = j;
                 break;
             }
         }
     }
     return ;
 }
int main(){
    sg[0] = 0;
    sg[1] = 1;
    sg[2] = 2;
    get_sg();
    rep(i,1,1000){
        if(i!=sg[i])
        cout << i << ": " << sg[i] << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值