Light OJ 1296 - Again Stone Game (博弈sg函数递推)
2016-05-24 10:14
483 查看
F - Again Stone GameTime Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %lluSubmit StatusDescriptionAlice 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 oneand 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.InputInput 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 thisline denotes the number of stones in the ith pile.OutputFor each case, print the case number and the name of the player who will win the game.Sample Input511310 11 1251 2 3 4 524 931 3 9Sample OutputCase 1: BobCase 2: AliceCase 3: AliceCase 4: BobCase 5: Alice题意:有n堆石子,分别有a1,a2,......,an个。两个游戏者轮流操作,每次可以选一堆, 拿走至少一个石子,但不能拿走超过一半的石子。比如,若有3堆石子,每堆分别有 5,1,2个,则在下一轮中,游戏者可以从第一堆中拿1个或2个,第二堆中不能拿,第三堆 只能拿一个。谁不能拿石子,就算输。题解:刘汝佳白皮书《算法竞赛入门经典训练指南》 p137原题。首先递推出sg函数找规律,发现 n为偶数时,sg(n)=n/2。把n为偶数的值全部删除,发现得到的数列和原数列一样。则当n 为奇数时,sg(n)=sg(n/2),这里的n是向下取整的。
#include <iostream>#include <stdio.h>using namespace std;int sg(int n){while(n&1) n>>=1; //当n为奇数时,递归到为偶数为止。return n>>1;}int main(){int i,n,t,cas=1;cin>>t;while(t--){int ans=0,data;cin>>n;for(i=0;i<n;i++){cin>>data;ans^=sg(data);}if(ans)printf("Case %d: Alice\n",cas++);elseprintf("Case %d: Bob\n",cas++);}return 0;}
相关文章推荐
- AIX5.3扩容文件系统时报0516-404
- The Chain Of Responsibility (1)
- The Chain Of Responsibility (1)
- http://zhidao.baidu.com/link?url=3tJ_i5gyYLrd7rFPk0eRYre_oxjCZvTOMOutp89LGhUgi6Ic6Ncama_GMAHnwfF73SVYGqy364vDfv6AY4ERPa
- 依靠国家资源,发展人工智能
- 国家发力,大力推进我国人工智能产业的大发展
- 百度地图 baiduMap(三) 移动轨迹
- LOJ1282 Leading and Trailing(数论)
- 关闭 CLOSE_WAIT 状态的连接
- 经典:传统相机标定方法解析:直接线性法和Tsai两步标定法
- Service和AIDL
- 《机器学习实战》--朴素贝叶斯
- http://blog.csdn.net/elifefly/article/details/3964766
- Leetcode 217. Contains Duplicate
- LK源码解析 4 main.c
- LK源码解析 2 main.c
- DataInputString
- {题解}[jzoj3396] 【NOIP2013模拟】Rainbow的信号
- 【NOIP2013模拟】Rainbow的信号
- uva 442 Matrix Chain Multiplication