分为3堆和三堆的规律相似
2^n堆
sg[8*k+7] = 8k+8;
sg[8*k+8] = 8k+7;
sg[4k+3]=4k+4;
sg[4k+4]=4k+3;
C - A Simple Nim
Two players take turns picking candies from n heaps,the player who picks the last one will win the game.On each turn they can pick any number of candies which come from the same heap(picking no candy is not allowed).To make the game more interesting,players can separate one heap into three smaller heaps(no empty heaps)instead of the picking operation.Please find out which player will win the game if each of them never make mistakes.
Input
Intput contains multiple test cases. The first line is an integer 1≤T≤1001≤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]s[0],s[1],....,s[n−1], representing heaps with s[0],s[1],...,s[n−1]s[0],s[1],...,s[n−1] objects respectively.(1≤n≤106,1≤s[i]≤109)(1≤n≤106,1≤s[i]≤109)
Output
For each test case,output a line whick contains either"First player wins."or"Second player wins".
Sample Input
2
2
4 4
3
1 2 4
Sample Output
Second player wins.
First player wins.
变形的Nim博弈. 这里推导部分sg值找规律:
sg[0] = 0;
sg[1] = 1;
sg[2] = mex{sg[0], sg[1]} = mex{0,1} = 2;
sg[3] = mex{sg[0], sg[1], sg[2], sg[1,1,1]} = mex{0,1,2, sg[1]^sg[1]^sg[1] = 1} = 3;
sg[4] = 4;
sg[5] = 5;
sg[6] = 6;
sg[7] = 8;
sg[8] = 7;
sg[9] = 9;
综上,可以看出规律:
sg[0] = 0;
sg[8*k+7] = 8k+8;
sg[8*k+8] = 8k+7;
最后异或起来就可以了.
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int MAXN =1e5+5;
const int MAXM = 1015;
int n,m;
int s;
int main()
{
// ios_base::sync_with_stdio(false);
// freopen("data.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
int ans = 0;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&s);
if(s%8==0)
{
ans^=(s-1);
}
else if(s%8==7)
{
ans ^= s+1;
}
else
{
ans^=s;
}
}
if(ans)
{
puts("First player wins.");
}
else
{
puts("Second player wins.");
}
}
}
E - Nim or not Nim?
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.
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
结果是sg[4k+3]=4k+4; sg[4k+4]=4k+3;
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int MAXN =1e5+5;
const int MAXM = 1015;
int n,m;
int s;
int main()
{
// ios_base::sync_with_stdio(false);
// freopen("data.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
int ans = 0;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&s);
if(s%4==0)
{
ans^=(s-1);
}
else if(s%4==3)
{
ans ^= s+1;
}
else
{
ans^=s;
}
}
if(ans)
{
puts("Alice");
}
else
{
puts("Bob");
}
}
}