# Gena vs Petya

## 题目描述

Gena and Petya love playing the following game with each other. There are $n$ piles of stones, the $i$-th pile contains $a_i$ stones. The players move in turns, Gena moves first. A player moves by choosing any non-empty pile and taking an arbitrary positive number of stones from it. If the move is impossible (that is, all piles are empty), then the game finishes and the current player is considered a loser.
Gena and Petya are the world famous experts in unusual games. We will assume that they play optimally.
Recently Petya started to notice that Gena wins too often. Petya decided that the problem is the unjust rules as Gena always gets to move first! To even their chances, Petya decided to cheat and take and hide some stones before the game begins. Since Petya does not want Gena to suspect anything, he will take the same number of stones $x$ from each pile. This number $x$ can be an arbitrary non-negative integer, strictly less that the minimum of $a_i$ values.
Your task is to find the number of distinct numbers $x$ such that Petya will win the game.

## 算法分析

Nim游戏后手胜利的条件是所有数的异或和为$0$。

## 代码

#include <algorithm>
#include <cstdio>
#include <cstring>

#define int long long

static int const N = 200005;
static int const M = 65;
int a[N], cnt[M], f[M][N], s[2][N];

signed main() {
int n;
scanf("%lld", &n);
for (int i = 0; i < n; ++i) {
scanf("%lld", a + i);
for (int j = 0; j < M; ++j)
cnt[j] += a[i] >> j & 1;
}
f[0][0] = 1;
for (int i = 0; i < M - 1; ++i) {
int c0 = n - cnt[i], c1 = cnt[i], c = 0;
for (int j = 0; j <= n; ++j) {
if (j)
if (a[j - 1] >> i & 1)
++c0, --c1;
else
--c0, ++c1, ++c;
if (!(c0 & 1))
f[i + 1][c + c0] += f[i][j];
if (!(c1 & 1))
f[i + 1][c] += f[i][j];
}
*s[0] = *s[1] = 0;
for (int j = 0; j < n; ++j)
s[a[j] >> i & 1][++*s[a[j] >> i & 1]] = a[j];
for (int j = 1; j <= *s[0]; ++j)
a[j - 1] = s[0][j];
for (int j = 1; j <= *s[1]; ++j)
a[*s[0] + j - 1] = s[1][j];
}
int sum = 0;
for (int i = 1; i < n; ++i)
sum ^= a[i] - a[0];
printf("%lld\n", f[M - 1][0] - (sum == 0));
return 0;
}


418 I'm a teapot