ZOJ 3591 Nim 前缀和+位运算

Nim

Time Limit: 3 Seconds       Memory Limit: 65536 KB

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



题意:通过他给的代码,跑出 n个数a【i】。 然后取任意多个连续的数,让他们 异或操作。 计算有多少种取法,使操作后结果为0.


做法:连续就想到了前缀和。  把前i个a的异或操作结果放在 num[i]中, 那么a【i】到a【j】个数的异或结果就是 num[j]^num[i-1]。  还有num【i】自身代表了开头到a【i】这些数的异或结果。     

然后只要计算有多少num【i】为0,以及多少  num[j]^num[i-1]  为0就行了。再把总方案数 c(n,2)减去为0的个数,就是答案了。但是普通方法复杂度n^2还是会超时。 

所以我们可以给num数组排个序。然后计算连续的数的个数。连续x个1,那为0的个数 + c(x,2)。最后的排列组合的总和,加上num【i】为0的个数就是区间异或为0的总数了。把总方案数减下就是答案了。





注意要用64位


long long a[920000]; 
long long str[999999];
long long num[999999];

int main()
{
	long long t;
	cin>>t;
	long long n;
	while(t--)
	{
		//N, S and W
		long long N,S,W;
		cin>>N>>S>>W;
		long long g = S;  
		for (long long 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; }
		}


		num[0]=a[0];
		for(long long i=1;i<N;i++)
		{
			num[i]=(num[i-1]^a[i]);
		}  
		sort(num,num+N);
		long long ans=0;
		long long nn=1;
		if(num[0]==0)
			ans++;
		for(long long i=1;i<N;i++)
		{
			if(num[i]==0)
				ans++;
			if(num[i]==num[i-1])
			{
				nn++;
			}
			else
			{
				ans+=nn*(nn-1)/2;
				nn=1;
			}
		}
		ans+=nn*(nn-1)/2;

		
		ans=N*(N-1)/2+N-ans;
		printf("%lld\n",ans); 
	} 
	return 0;
} 









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值