1296 - Again Stone Game
PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 32 MB |
Alice and Bob are playing a stone game. Initially there are n piles of stones and each pile contains some stone. Alice stars the game and they alternate moves. In each move, a player has to select any pile and
should remove at least one and no more than half stones from that pile. So, for example if a pile contains 10 stones, then a player can take at least 1 and at most 5 stones from that pile. If a pile contains 7 stones; at most 3 stones from that pile can be
removed.
Both Alice and Bob play perfectly. The player who cannot make a valid move loses. Now you are given the information of the piles and the number of stones in all the piles, you have to find the player who will win if both play
optimally.
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case starts with a line containing an integer n (1 ≤ n ≤ 1000). The next line contains n space separated integers ranging in [1, 109]. The ith integer
in this line denotes the number of stones in the ith pile.
Output
For each case, print the case number and the name of the player who will win the game.
Sample Input |
Output for Sample Input |
5 1 1 3 10 11 12 5 1 2 3 4 5 2 4 9 3 1 3 9 |
Case 1: Bob Case 2: Alice Case 3: Alice Case 4: Bob Case 5: Alice |
题目大意:
有 m 堆石子,每堆分别有x个,两个人(Alice 和 Bob)轮流进行操作,每次可以选任意一堆,拿走至少一个石子,但是不要拿走超过 一半的石子,谁不能拿了谁就输了Alice先手
解题思路:
首先这个题如果要是求SG函数的话 10^9次方 肯定要超时,所以先要求一下 SG值,因为每次只能拿走不超过一半的石子数,所以我们就可以先打一个sg表,下面是30个数的sg值:
0 1 0 2 1 3 0 4 2 5 1 6 3 7 0 8 4 9 2 10 5 11 1 12 6 13 3 14 7 15,
通过观察我们可以发现 偶数的SG值都是偶数的一半,当我们拿掉所有偶数的时候:
0 1 0 2 1 3 0 4 2 5 1 6 3 7...跟上面的是一样的所以 奇数的SG值就是 SG[i/2],我们就可以做了。。
My Code:
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int MAXN = 1e3+5; int sg[MAXN]; int hash[MAXN]; void get_sg() { memset(sg, 0, sizeof(sg)); for(int i=1; i<MAXN; i++) { memset(hash, 0, sizeof(hash)); for(int j=1; j<=i/2; j++) { hash[sg[i-j]] = 1; } int j; for(j=0; ;j++) { if(!hash[j]) break; } sg[i] = j; } for(int i=1; i<=30; i++) cout<<sg[i]<<" "; } int main() { ///get_sg(); int T; scanf("%d",&T); for(int cas=1; cas<=T; cas++) { int m, x, ans=0; scanf("%d",&m); for(int i=0; i<m; i++) { scanf("%d",&x); while(x&1) x>>=1; ans ^= (x>>1); } if(!ans) printf("Case %d: Bob\n",cas); else printf("Case %d: Alice\n",cas); } return 0; }