题干:

Nim is a mathematical game of strategy in which two players take turns removing objects from distinct heaps. The game ends when one of the players is unable to remove object in his/her turn. This player will then lose. On each turn, a player must remove at least one object, and may remove any number of objects provided they all come from the same heap. Here is another version of Nim game. There are N piles of stones on the table. Alice first chooses some CONSECUTIVE piles of stones to play the Nim game with Tom. Also, Alice will make the first move. Alice wants to know how many ways of choosing can make her win the game if both players play optimally.

You are given a sequence a[0],a[1], ... a[N-1] of positive integers to indicate the number of stones in each pile. The sequence a[0]...a[N-1] of length N is generated by the following code:

int g = S; 

for (int i=0; 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; }

}

Input

There are multiple test cases. The first line of input is an integer T(T ≤ 100) indicates the number of test cases. Then T test cases follow. Each test case is represented by a line containing 3 integers NS and W, separated by spaces. (0 < N≤ 105, 0 < S, W ≤ 109)

Output

For each test case, output the number of ways to win the game.

Sample Input

2
3 1 1
3 2 1

Sample Output

4
5

题目大意:

   因为我们知道Nim博弈,这x堆石子的异或和为0则必败,不为零则必胜,所以就是问你在这些石子中选择哪些连续的堆,可以必胜,问你选择方案数。

  也就是,给n个数,让你求,有多少段连续区间,使得这段区间的异或和不为0。

解题报告:

正难则反,可以先求出有多少个区间,异或和为零,然后用总区间数去作差就行了。 

做法就是个常见的在线处理。。。这题也可以先求出这个和来,然后用C(n,2)去减这个和,得到的就是答案。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
int n;
ll a[100010];
ll sum[100010];
map<ll,int> mp;
int main() {
	int t,q,cnt;
	ll i,j,k,g,S,W,res,tmp,ss;
	cin>>t;
	while(t--) {
		cin>>n>>S>>W;
		g = S;
		res=0;
		for (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;
		}
		mp.clear();
		mp[0]=1;
		for(i=1; i<=n; i++) {
			sum[i]=sum[i-1]^a[i];
			res+=i-mp[sum[i]];
			mp[sum[i]]++;
		}
		cout<<res<<endl;
	}

	return 0;
}