Nim or not Nim?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2513 Accepted Submission(s): 1300
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
分析
打表找规律。
$$
sg(x)=
\begin{cases}
x-1,&x\ mod\ 4=0\\
x+1,&x\ mod\ 4=3\\
x,&else \\
\end{cases}
$$
code
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include<cstdio> 2 #include<algorithm> 3 4 using namespace std; 5 6 int get_SG(int x) { 7 if (x%4==0) return x-1; 8 else if (x%4==3) return x+1; 9 return x; 10 } 11 int main () { 12 int T,n,ans; 13 scanf("%d",&T); 14 for (int Case=1; Case<=T; ++Case) { 15 scanf("%d",&n); 16 ans = 0; 17 for (int t,i=1; i<=n; ++i) { 18 scanf("%d",&t); 19 ans ^= get_SG(t); 20 } 21 if (ans==0) puts("Bob"); 22 else puts("Alice"); 23 24 } 25 return 0; 26 }
打表代码
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 5 using namespace std; 6 7 int sg[1010]; 8 bool Hash[1010]; 9 const int N = 1000; 10 11 void get_SG() { 12 for (int i=1; i<=N; ++i) { 13 memset(Hash,false,sizeof(Hash)); 14 for (int j=0; j<=i; ++j) { 15 Hash[sg[j]] = true; // 取石子 16 if (j!=0 && j!=N) Hash[sg[j]^sg[i-j]] = true; //拆分 17 } 18 for (int j=0; ; ++j) 19 if (!Hash[j]) {sg[i] = j;break;}; 20 } 21 } 22 23 int main () { 24 get_SG(); 25 /*for (int i=1; i<=10; ++i) { 26 for (int j=1; j<=10; ++j) { 27 printf("%d ",sg[(i-1)*10+j]); 28 } 29 puts(""); 30 }*/ 31 for (int i=1; i<=100; ++i) { 32 printf("%d ",sg[i]); 33 if (i % 4 == 0) puts(""); 34 } 35 return 0; 36 }