构造一个SG函数前缀和 设前缀和值相同的两元素i ,j 那么(i+1, j)就是一个必败态 减去即可
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<map>
#define SF scanf
#define PF printf
#define max(a, b) ((a) < (b) ? (b) : (a))
using namespace std;
typedef long long LL;
const int MAXN = 100000;
int s[MAXN+10], a[MAXN+10], n, S, W;
map <int, int> M;
void Make_a()
{
int g = S;
for (int i=1; i<=n; i++) {
a[i] = g;
if( a[i] == 0 ) { a[i] = g = W; }
if( g%2 == 0 ) { g = (g/2); }
else { g = (g/2) ^ W; }
}
}
int main()
{
int T; SF("%d", &T); while(T--) {
SF("%d%d%d", &n, &S, &W);
Make_a();
for(int i = 1; i <= n; i++) s[i] = s[i-1] ^ a[i];
M.clear();
M[0] = 1;
LL Fail = 0;
for(int i = 1; i <= n; i++)
{
if(M.count(s[i]))
{
Fail += M[s[i]];
M[s[i]]++;
}
else M[s[i]] = 1;
}
cout << (LL)n * (n + 1) / 2 - Fail << '\n';
}
}