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;
} 
时间: 2024-10-23 20:50:07

ZOJ 3591 Nim 前缀和+位运算的相关文章

ZOJ 3591 Nim (连续子序列异或和)

题目链接:ZOJ 3591 Nim 题意:根据题目给出的代码得到n堆石头的各自的数量,求先手选出连续的若干堆并且必胜的方法数.(比如:3,1,1 每堆石头数是1,1,1.先手选出(1),(1),(1),(1,1,1) 这四种方案是必胜的,所以答案是4) 思路:在n堆取石头首先想到的是Nim博弈,连续的若干堆,即求连续子序列异或和为0的数量m,n*(n+1)/2-m就是答案 (Nim博弈结论,a1,a2,a3--an,an-1,若a1^a2^a3^--^an^an-1=0先手必败 ) 连续子序列异

51Nod 1069 Nim游戏 (位运算)

题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1069 有N堆石子.A B两个人轮流拿,A先拿.每次只能从一堆中取若干个,可将一堆全取走,但不可不取,拿到最后1颗石子的人获胜.假设A B都非常聪明,拿石子的过程中不会出现失误.给出N及每堆石子的数量,问最后谁能赢得比赛. 例如:3堆石子,每堆1颗.A拿1颗,B拿1颗,此时还剩1堆,所以A可以拿到最后1颗石子. Input 第1行:一个数N,表示有N堆石子.(

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

Zoj 3591 Nim 【博弈】【搜索】

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3591 题目大意:给你T组case,每组case有N,S,W三个数字,根据题目给出的代码可以算出每组石子个数a[i]. 已知了N组石子的个数,现在让你来选出连续的石子堆,使得先手会赢,问有多少种选法. 比如给出的样例:NSW分别是 3 1 1 则算出来的每堆石子的个数a[i]是 1 1 1,则 1 1 1三堆石子的选法有: 选第一堆,选第二堆,选第三堆,选第一堆到

HDU - 6186 前缀和位运算

异或操作蒙蔽了我的双眼 以至于没有第一时间想到前缀和与后缀和 水题做的不够多 #include<bits/stdc++.h> #define rep(i,j,k) for(register int i=j;i<=k;i++) #define rrep(i,j,k) for(register int i=j;i>=k;i--) using namespace std; typedef long long ll; const int maxn = 1e5+11; ll xxor[max

ZOJ 3870 Team Formation(位运算)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5518 For an upcoming programming contest, Edward, the headmaster of Marjar University, is forming a two-man team from N students of his university. Edward knows the skill level of each s

位运算 ZOJ 3870 Team Formation

题目传送门 1 /* 2 题意:找出符合 A^B > max (A, B) 的组数: 3 位运算:异或的性质,1^1=0, 1^0=1, 0^1=1, 0^0=0:与的性质:1^1=1, 1^0=0, 0^1=0, 0^0=0: 4 假设A < B,一定要满足B的最高位对应A的值是0,这样才可能>B(即0^1=1): 5 然后比赛时假设A的极限是类似0111111的情况,最后假设有误: 6 题解是先把每个数最高位(1)的位置统计个数,1<<4 的意思是 000010000:

HDOJ 5088 Revenge of Nim II 位运算

位运算.... Revenge of Nim II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 229    Accepted Submission(s): 79 Problem Description Nim is a mathematical game of strategy in which two players take

HDU 4317 位运算

[题意]: 在一个常规的NIM游戏里,你可以在每堆石子拿走任意数量的石子,问求使先手必败的情况下拿走石子数量的最小值. [知识点]: 位运算,DP [题解]: 一道精致的位运算的好题目,细节有不少. 具体解释在代码内. [代码]: 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <cmath> 5 #include <cstdlib> 6 #inc