Nim or not Nim?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1326 Accepted Submission(s): 656
Problem Description
Nim is a two-player mathematic game of strategy in which players take turns removing objects from distinct heaps. 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.
Nim is usually played as a misere game, in which the player to take the last object loses. Nim can also be played as a normal play game, which means that the person who makes the last move (i.e., who takes the last object) wins. This is called normal play because most games follow this convention, even though Nim usually does not.
Alice and Bob is tired of playing Nim under the standard rule, so they make a difference by also allowing the player to separate one of the heaps into two smaller ones. That is, each turn the player may either remove any number of objects from a heap or separate a heap into two smaller ones, and the one who takes the last object wins.
Nim is usually played as a misere game, in which the player to take the last object loses. Nim can also be played as a normal play game, which means that the person who makes the last move (i.e., who takes the last object) wins. This is called normal play because most games follow this convention, even though Nim usually does not.
Alice and Bob is tired of playing Nim under the standard rule, so they make a difference by also allowing the player to separate one of the heaps into two smaller ones. That is, each turn the player may either remove any number of objects from a heap or separate a heap into two smaller ones, and the one who takes the last object wins.
Input
Input contains multiple test cases. The first line is an integer 1 ≤ T ≤ 100, the number of test cases. Each case begins with an integer N, indicating the number of the heaps, the next line contains N integers s[0], s[1], ...., s[N-1], representing heaps with s[0], s[1], ..., s[N-1] objects respectively.(1 ≤ N ≤ 10^6, 1 ≤ S[i] ≤ 2^31 - 1)
Output
For each test case, output a line which contains either "Alice" or "Bob", which is the winner of this game. Alice will play first. You may asume they never make mistakes.
Sample Input
2 3 2 2 3 2 3 3
Sample Output
Alice Bob
Source
Recommend
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)
#define bug printf("hihi\n")
#define eps 1e-8
typedef __int64 ll;
using namespace std;
#define INF 0x3f3f3f3f
#define N 10005
int main()
{
int i,j,t;
int n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int ans=0;
int x;
while(n--)
{
scanf("%d",&x);
if(x%4==1)
ans^=x;
if(x%4==2)
ans^=x;
if(x%4==0)
ans^=(x-1);
if(x%4==3)
ans^=(x+1);
}
if(ans)
printf("Alice\n");
else
printf("Bob\n");
}
return 0;
}
/*
打sg 表看规律
sg(4k)=4k-1;sg(4k+1)=4k+1;sg(4k+2)=4k+2;sg(4k+3)=4k+4;
#include<iostream>
#include<string.h>
#include<cstdio>
#define N 1000001
using namespace std;
int sg[N];
int g(int x)
{
int mex[1000];
memset(mex,0,sizeof(mex));
if(sg[x]!=-1) return sg[x];
for(int i=x-1;i>=0;i--)
{
mex[g(i)]=1;
}
for(int i=1;i<=x/2;i++)
{
int ans=0;
ans^=g(i);
ans^=g(x-i);
mex[ans]=1;
}
for(int i=0;;i++)
if(!mex[i]) return sg[x]=i;
}
int main()
{
int t , n ,x ;
memset(sg,-1,sizeof(sg));
sg[0]=0;
n=100;
for(int i=1;i<n;i++)
g(i);
for(int i=0;i<=20;i++)
{
printf("i=%d %d\n",i,sg[i]);
}
return 0;
}
/*
i=0 0
i=1 1
i=2 2
i=3 4
i=4 3
i=5 5
i=6 6
i=7 8
i=8 7
i=9 9
i=10 10
i=11 12
i=12 11
i=13 13
i=14 14
i=15 16
i=16 15
i=17 17
i=18 18
i=19 20
i=20 19
Process returned 0 (0x0) execution time : 0.194 s
Press any key to continue.
*/
本文介绍了一种Nim游戏的变种,玩家可以选择移除堆中的物品或将一个堆分成两个较小的堆。通过分析不同堆的状态,可以确定先手玩家是否有必胜策略。
2868

被折叠的 条评论
为什么被折叠?



