【codeforces 768E】Game of Stones

【题目链接】:http://codeforces.com/contest/768/problem/E

【题意】

NIM游戏的变种;
要求每一堆石头一次拿了x个之后,下一次就不能一次拿x个了;
问你结果

【题解】

用二进制来表示哪些数字被用过;
然后写一个记忆化搜索;
从上到下获取1..60的sg函数
枚举用了哪个数字,然后总数减掉它,可以使用的数字的状态改变;
往下走就好;
一定要记住
sg函数的最小值是0
从1开始的话会wa。。

【Number Of WA

5

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)

typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 110;

int n, ju;
map <LL, int> dic[N];
LL sg[N], pre[N];

LL dfs(int x, LL zt) {
    //printf("%d\n", x);
    if (dic[x].count(zt))
        return dic[x][zt];
    LL ret = 0;
    for (LL i = 0; i <= x-1; i++)
        if (zt&pre[i]) {
            ret |= pre[dfs(x - i-1, zt ^ pre[i])];
        }
    for (LL i = 0; i <= x; i++)
        if ((ret&pre[i]) == 0)
            return dic[x][zt] = i;
}

int main() {
    //freopen("F:\\rush.txt", "r", stdin);
    pre[0] = 1;
    rep1(i, 1, 60)
        pre[i] = pre[i - 1] * 2;
    rep1(i, 1, 60) sg[i] = dfs(i, pre[i] - 1);
    rei(n);
    while (n--) {
        int x;
        rei(x);
        ju ^= sg[x];
    }
    if (ju != 0)
        puts("NO");
    else
        puts("YES");
    //printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
    return 0;
}
posted @ 2017-10-04 18:44  AWCXV  阅读(230)  评论(0编辑  收藏  举报