ZOJ - 3591 Nim 尼姆博弈 + 前缀和
发布日期:2021-09-25 23:57:50 浏览次数:6 分类:技术文章

本文共 2753 字,大约阅读时间需要 9 分钟。

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 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

题目大意就是先由给定的程序生成 a 数组,让后选择 a 中连续的序列,进行尼姆游戏,判断先手获胜次数。

普通的尼姆游戏只要所有数的异或值不等于 0 ,那么先手必赢。这个题是要选出来一段序列判断,显然直接枚举是不现实的,那么需要考虑一下什么时候异或为0,显然当两个数相同的时候,即 sum [ i - 1 ] 和 sum [ j ] 相同的时候,i ~ j 的异或值为0,这个也比较好证,在最后附上吧。那么可以考虑维护一下前缀异或值,当某两个前缀的异或值相等的时候,那么选择他们之间的区间的时候,先手必败。而我们需要求出先手必赢的数量,那么可以考虑转换一下,由于知道所有区间的数量为 n * ( n + 1 ) / 2 ,只需要求出不符合要求的数量,减去即可。
实际写代码的时候,用map存一下当前值出现的数量即可,只是用到了前缀和思想,不需要开一个前缀和数组。

sum [ j ] = sum [ i - 1 ] ^ sum [ i ~ j ]

sum [ j ] ^ sum [ i - 1 ] = sum [ i - 1 ] ^ sum [ i - 1 ] ^ sun [ i ~ j ]
sum [ j ] ^ sum [ i - 1 ] = sum [ i ~ j ]

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define X first#define Y second#define L (u<<1)#define R (u<<1|1)#define Mid (tr[u].l+tr[u].r>>1)#define Len(u) (tr[u].r-tr[u].l+1)using namespace std;typedef long long LL;typedef pair
PII;const int N=100010,mod=1e9+7,INF=0x3f3f3f3f;const double eps=1e-6;int s,n,w;int a[N];void init(){ 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(){ // ios::sync_with_stdio(false);// cin.tie(0); int t; cin>>t; while(t--) { map
mp; scanf("%d%d%d",&n,&s,&w); init(); LL ans=(LL)n*(n+1)/2,t=0; mp[0]=1; for(int i=1;i<=n;i++) { t^=a[i]; if(mp.count(t)) ans-=mp[t]; mp[t]++; } cout<
<

转载地址:https://blog.csdn.net/DaNIelLAk/article/details/106245882 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:upc 兔 最小生成树
下一篇:upc PinkRabbit寻找妹子 dfs + 玄学优化

发表评论

最新留言

很好
[***.229.124.182]2024年04月02日 19时09分17秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章