link:http://acm.zju.edu.cn/onlinejudge/showproblem.do?problemid=4675
Nim time Limit:3 Seconds Memory limit:65536 KB
Nim is a mathematical game of the strategy in which two players the take turns removing objects from distinct heaps. The game ends when one of the players are unable to remove object in his/her turn. This player would 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 T He same heap. This is another version of Nim game. There are N piles of stones on the table. Alice I chooses some consecutive piles of the Nim stones with Tom. Also, Alice would make the "the". 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 the "a" 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 N, S 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
Author:cao, Peng
Contest:the 12th Zhejiang University Programming Contest
Programming thought: Game + reverse Negative thought
AC Code:
#include <iostream> #include <algorithm> #include <stdio.h> #include <math.h> #include < cstring> #include <queue> #include <map> #include <set> #define LL Long long #define M 1100010 using Nam
Espace std;
int a[100100],b[100100];
LL Ans,dd;
int main () {int n,s,w,t;
scanf ("%d", &t);
while (t--) {scanf ("%d%d%d", &n,&s,&w);
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;}
Ans= (LL) (1+n) *n/2;//must impose LL conversion to long long, otherwise (1+n) *n/2 the super int type, and N is int, so it will overflow int, cause WA ...
int tmp=0;
for (int i=0;i<n;i++) {tmp^=a[i];
b[i]=tmp;
Sort (b,b+n);
int bb=b[0];
Dd=0;
LL cnt=1;
for (int i=1;i<n;i++) {if (Bb==b[i]) {cnt++;
} else {if (bb==0) dd+=cnt;//is 0, from the No. 0 heap to the I heap this group is a must-lose situation, to reduce the dd+= (LL) cnt* (cnt-1)/2;
Cnt=1;
Bb=b[i];
} dd+= (LL) cnt* (cnt-1)/2;
ANS-=DD; printf ("%lld\n", ans);
return 0; }