标签:
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. 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先打表找规律,简单的尼姆博奕
打表代码 :
1 #include <cstdio> 2 #include <cstring> 3 4 #define maxn 1000005 5 6 int sg[maxn]; 7 bool vis[maxn]; 8 9 10 void GetSG() 11 { 12 for(int i = 1 ; i <= 100 ; i++) 13 { 14 memset(vis , 0 , sizeof(vis)); 15 for(int j = 0 ; j < i ; j++) 16 vis[sg[j]] = 1; 17 if(i >= 2) 18 { 19 for(int j = 1 ; j < i ; j++) 20 vis[ sg[j] ^ sg[i-j] ] = 1; 21 } 22 int mid = 0; 23 while(vis[mid]) 24 mid++; 25 sg[i] = mid; 26 printf("%d : %d\n",i,sg[i]); 27 } 28 } 29 30 31 int main() 32 { 33 GetSG(); 34 return 0; 35 36 }
打表结果 :
1 : 1 2 : 2 3 : 4 4 : 3 5 : 5 6 : 6 7 : 8 8 : 7 9 : 9 10 : 10 11 : 12 12 : 11 13 : 13 14 : 14 15 : 16 16 : 15 17 : 17 18 : 18 19 : 20 20 : 19 21 : 21 22 : 22 23 : 24 24 : 23 25 : 25 26 : 26 27 : 28 28 : 27 29 : 29 30 : 30 31 : 32 32 : 31 33 : 33 34 : 34 35 : 36 36 : 35 37 : 37 38 : 38 39 : 40 40 : 39 41 : 41 42 : 42 43 : 44 44 : 43 45 : 45 46 : 46 47 : 48 48 : 47 49 : 49 50 : 50 51 : 52 52 : 51 53 : 53 54 : 54 55 : 56 56 : 55 57 : 57 58 : 58 59 : 60 60 : 59 61 : 61 62 : 62 63 : 64 64 : 63 65 : 65 66 : 66 67 : 68 68 : 67 69 : 69 70 : 70 71 : 72 72 : 71 73 : 73 74 : 74 75 : 76 76 : 75 77 : 77 78 : 78 79 : 80 80 : 79 81 : 81 82 : 82 83 : 84 84 : 83 85 : 85 86 : 86 87 : 88 88 : 87 89 : 89 90 : 90 91 : 92 92 : 91 93 : 93 94 : 94 95 : 96 96 : 95 97 : 97 98 : 98 99 : 100 100 : 99
由于是刚开始接触这种类型的尼姆博弈,说实话对SG还不是很理解,还不能灵活的应用,所以这篇随笔目前也没啥注释,
待我对这类问题有了更清楚的理解,再回来看看这吧。
AC Code :
1 #include <bits/stdc++.h> 2 3 int GetSG(int num) 4 { 5 if(0 == num % 4) 6 return num - 1; 7 if(3 == num % 4) 8 return num + 1; 9 return num; 10 } 11 12 int main() 13 { 14 int t; 15 scanf("%d",&t); 16 while(t--) 17 { 18 int n , res = 0 , num; 19 scanf("%d",&n); 20 for(int i = 1 ; i <= n ; i++) 21 { 22 scanf("%d" , &num); 23 res ^= GetSG(num); 24 } 25 if(res) 26 printf("Alice\n"); 27 else 28 printf("Bob\n"); 29 } 30 return 0; 31 }
其实这个题和 HDU-5795 http://acm.split.hdu.edu.cn/showproblem.php?pid=5795 神似,(同样的多校赛的题,年份不一样//滑稽)不过就是那个题可以分为3个更小的堆(其实我是先做那个题才做这个的),同样打表找规律,代码稍微修改下就能过,这里就不多说了。
标签:
原文地址:http://www.cnblogs.com/x-1204729564/p/5798793.html