Light OJ 1296:Again Stone Game(SG函数打表找规律)
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
5
1
1
3
10 11 12
5
1 2 3 4 5
2
4 9
3
1 3 9
Sample Output
Case 1: Bob
Case 2: Alice
Case 3: Alice
Case 4: Bob
Case 5: Alice
题意
有n堆石子,每堆ai个,Alice和Bob两人取石子,可以任选一堆取,每次至少一个至多ai/2个(奇数情况向下取整)。最后不能取石子的人失败
思路
数据范围1e9,肯定不能打表计算,但是可以打表来找一下规律。最后会发现,如果ai是偶数的话,可以把这堆石子看成ai/2个石子进行取,如果是奇数,就让ai一直除以2,至到找到偶数,然后再按照石子数是偶数的情况处理一下就可以了
AC代码
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ull unsigned long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
#define lson o<<1
#define rson o<<1|1
const double E=exp(1);
const int maxn=1e6+10;
const int mod=1e9+7;
using namespace std;
int SG[1005], mex[1005];
int sg[maxn];
int vis[maxn];
int a[maxn];
inline void getSG()
{
ms(sg);
for(register int i=1;i<=maxn;i++)
{
ms(vis);
for(int j=1;2*j<=i;j++)
vis[sg[i-j]]=1;
for(register int j=0;j<=maxn;j++)
{
if(!vis[j])
{
sg[i]=j;
break;
}
}
cout<<i<<"======="<<sg[i]<<endl;
}
}
int main(int argc, char const *argv[])
{
// getSG();
ios::sync_with_stdio(false);
register int t;
register int _=0;
register int n;
cin>>t;
while(t--)
{
register int sum=0,x;
cin>>n;
while(n--)
{
cin>>x;
while(x%2!=0)
x/=2;
x/=2;
sum^=x;
}
cout<<"Case "<<++_<<": ";
if(sum)
cout<<"Alice"<<endl;
else
cout<<"Bob"<<endl;
}
return 0;
}
Light OJ 1296:Again Stone Game(SG函数打表找规律)的更多相关文章
- bzoj 1228 [SDOI2009]E&D SG函数打表 找规律
题目链接 Description 桌子上有2n 堆石子,编号为1..2n.将第2k-1 堆与第2k 堆(1 ≤ k ≤ n)为同一组.第i堆的石子个数用一个正整数Si表示.一次分割操作指的是,从桌子上 ...
- hdu 3032 Nim or not Nim? (sg函数打表找规律)
题意:有N堆石子,每堆有s[i]个,Alice和Bob两人轮流取石子,可以从一堆中取任意多的石子,也可以把一堆石子分成两小堆 Alice先取,问谁能获胜 思路:首先观察这道题的数据范围 1 ≤ N ...
- Playing With Stones UVALive - 5059 Nim SG函数 打表找规律
Code: #include<cstdio> #include<algorithm> using namespace std; typedef long long ll; ll ...
- 【博弈论】【SG函数】【找规律】Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) E. Game of Stones
打表找规律即可. 1,1,2,2,2,3,3,3,3,4,4,4,4,4... 注意打表的时候,sg值不只与剩下的石子数有关,也和之前取走的方案有关. //#include<cstdio> ...
- Light OJ 1296 - Again Stone Game (博弈sg函数递推)
F - Again Stone Game Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- Light OJ 1199 - Partitioning Game (博弈sg函数)
D - Partitioning Game Time Limit:4000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- 【博弈论】【SG函数】【找规律】Gym - 101147A - The game of Osho
以后这种题还是不能空想,必须打个表看看,规律还是比较好找的……具体是啥看代码.用SG函数暴力的部分就不放了. #include<cstdio> using namespace std; i ...
- Nim or not Nim? hdu3032 SG值打表找规律
Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- Educational Codeforces Round 68 (Rated for Div. 2)D(SG函数打表,找规律)
#include<bits/stdc++.h>using namespace std;int sg[1007];int main(){ int t; cin>>t; while ...
随机推荐
- 微信支付 php兼容问题
总结: php7 已删除 HTTP_RAW_POST_DATA 获取时需要file_get_contents("php://input"); 下面的是兼容方法. //存储微信的回 ...
- 由@Convert注解引出的jackson对枚举的反序列化规则
对于一些状态字段以前时兴用常量,现在时兴用枚举,虽然阅读体验极佳,但是传值的时候还是会有些麻烦,需要设置一下转换器.比如: class A{ @Convert(converter=TestTypeCo ...
- git merge branch
git branch look at your branches git branch newbranch git checkout newbrach do something git check ...
- [IOS微信] PList文件解析,boost数据读取
最近在解析IOS版微信数据中的 mmsetting.archive 文件时,第一次接触到PList文件. 注:mmsetting.archive 不是一个标准的PList文件,其中含有汉字,并且很多 ...
- unity中自制模拟第一人称视角
public float sensitivityX = 5f; public float sensitivityY = 5f; public float sensitivetyKeyBoard = 0 ...
- 解决WDCP3环境gbk网站编码程序乱码问题
因为默认WDCP V3版本环境编码格式是UTF-8版本,如果我们程序采用的是GBK编码肯定都会有乱码问题. 我们到WDCP后台,"网站管理"-"PHP设置",看 ...
- 第三篇 功能实现(3) (Android学习笔记)
第三篇 功能实现(3) ●发一个广播和启动一个隐式的Intent非常像,那么它们之间有什么区别呢? Implicit Intents (sent via startActivity( )) and B ...
- (C/C++学习笔记) 二十. 文件和流
二十. 文件和流 ● 文件的概念 文件(file) 一. C/C++语言将文件作为字节序列(sequence of characters)来对待,但从编码角度,或说从对字节信息的解释来看,文件分为:文 ...
- SQL-40 表中新增一列
题目描述 存在actor表,包含如下列信息:CREATE TABLE IF NOT EXISTS actor (actor_id smallint(5) NOT NULL PRIMARY KEY,fi ...
- SQL-37 创建索引
题目描述 针对如下表actor结构创建索引:CREATE TABLE IF NOT EXISTS actor (actor_id smallint(5) NOT NULL PRIMARY KEY,fi ...