ACdream 1112 Alice and Bob(素筛+博弈SG函数)
Time Limit:3000MS Memory Limit:128000KB 64bit IO Format:%lld & %llu
Description
Here is Alice and Bob again !
Alice and Bob are playing a game. There are several numbers.
First, Alice choose a number n.
Then he can replace n (n > 1) with one of its positive factor but not itself or he can replace n with a and b. Here a*b = n and a > 1 and b > 1.
For example, Alice can replace 6 with 2 or 3 or (2, 3).
But he can't replace 6 with 6 or (1, 6). But you can replace 6 with 1.
After Alice's turn, it’s Bob's turn. Alice and Bob take turns to do so. Who can’t do any replace lose the game.
Alice and Bob are both clever enough. Who is the winner?
Input
This problem contains multiple test cases. The first line contains one number n(1 ≤ n ≤ 100000).
The second line contains n numbers.
All the numbers are positive and less than of equal to 5000000.
Output
For each test case, if Alice can win, output “Alice”, otherwise output “Bob”.
Sample Input
2
2 2
3
2 2 4
Sample Output
Bob
Alice
题意:给定n堆石子,每次可以按照规则将该堆石子的个数分为两堆或者将该堆石子的个数减少,
谁不能继续操作了谁就输。
题解:规则如下,若n=a*b且a>1,b>1,可以将石子分为a,b两堆;或者可以减少为a或b。
比如n=6,可以分为(2,3),2,3这三种情况,但是不能分为(1,6),1,6这三种情况。
和HDU3032是一个问题,就是一个取石子问题,这里的8=2*2*2就是那道题里的3,这里的
6=2*3就是那道题里的2。如果对这道题理解感到困难可以先阅读我的上上篇博客:
http://www.cnblogs.com/Ritchie/p/5627242.html
也就是说这道题是对每个数的素因子个数进行操作,即素数可以看做是一个1。
设一个数x=a1^r1*a2^r2*...*an^rn;设sum = r1+r2+...+rn.
然后所有的情况就可以表示为:
(1,sum-1),(2,sum-2),...(sum/2,sum-sum/2)或者(1),(2),...(n-1)
然后在计算sum的时候我们可以这样计算。
设一个数为x,他的最小的素因子为y.则sum[x] = sum[x/y] + 1;
所以我们在素筛打表的时候要记录每个数的最小素因子以用于上述公式。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
#define maxn 5000050
int prime[maxn],cnt=;
bool isprime[maxn];
int m[maxn];
int a[maxn];
int sg[];
void getprime()
{
memset(isprime,,sizeof(isprime));
memset(m,,sizeof(m));
memset(a,,sizeof(a));
for(int i=; i<=maxn; i++)
{
if(!isprime[i])
{
prime[cnt++]=i;
for(int j=i+i; j<=maxn; j+=i)
{
isprime[j]=;
if(!m[j]) m[j]=i;
}
m[i]=i;
}
}
for(int i=; i<=maxn; i++)
a[i]=a[i/m[i]]+;
}
void getsg()
{
memset(sg,,sizeof(sg));
sg[]=;
sg[]=;
bool vis[]; //放到循环里定义也一样需要每次都初始化
for(int i=; i<=; i++)
{
memset(vis,,sizeof(vis)); //注意每次都要初始化
for(int j=; j<i; j++) //取石子
vis[sg[j]]=;
for(int j=; j<i; j++) //拆分
vis[sg[j]^sg[i-j]]=;
for(int x=;; x++)
if(!vis[x])
{
sg[i]=x;
break;
}
}
}
void get()
{
getsg();
getprime();
}
void solve()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int data,ans=;
for(int i=; i<n; i++)
{
scanf("%d",&data);
ans^=sg[a[data]];
}
if(ans)
printf("Alice\n");
else
printf("Bob\n");
}
}
int main()
{
get();
solve();
}
ACdream 1112 Alice and Bob(素筛+博弈SG函数)的更多相关文章
- ACdream 1112 Alice and Bob (sg函数的变形+素数筛)
题意:有N个数,Alice 和 Bob 轮流对这些数进行操作,若一个数 n=a*b且a>1,b>1,可以将该数变成 a 和 b 两个数: 或者可以减少为a或b,Alice先,问谁能赢 思路 ...
- Alice and Bob HDU - 4111 (SG函数)
Alice and Bob are very smart guys and they like to play all kinds of games in their spare time. The ...
- ACdream 1112 Alice and Bob (博弈&&素数筛选优化)
题目链接:传送门 游戏规则: 没次能够将一堆分成两堆 x = a*b (a!=1&&b!=1)x为原来堆的个数,a,b为新堆的个数. 也能够将原来的堆的个数变成原来堆的约数y.y!=x ...
- S-Nim HDU 1536 博弈 sg函数
S-Nim HDU 1536 博弈 sg函数 题意 首先输入K,表示一个集合的大小,之后输入集合,表示对于这对石子只能去这个集合中的元素的个数,之后输入 一个m表示接下来对于这个集合要进行m次询问,之 ...
- hdu 4111 Alice and Bob(中档博弈题)
copy VS study 1.每堆部是1的时候,是3的倍数时输否则赢: 2.只有一堆2其他全是1的时候,1的堆数是3的倍数时输否则赢: 3.其他情况下,计算出总和+堆数-1,若为偶数,且1的堆数是偶 ...
- hdu 3032(博弈sg函数)
题意:与原来基本的尼姆博弈不同的是,可以将一堆石子分成两堆石子也算一步操作,其它的都是一样的. 分析:由于石子的堆数和每一堆石子的数量都很大,所以肯定不能用搜索去求sg函数,现在我们只能通过找规律的办 ...
- Light OJ 1199 - Partitioning Game (博弈sg函数)
D - Partitioning Game Time Limit:4000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- LightOJ 1315 - Game of Hyper Knights(博弈sg函数)
G - Game of Hyper Knights Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & ...
- Light OJ 1296 - Again Stone Game (博弈sg函数递推)
F - Again Stone Game Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
随机推荐
- spring c3p0数据库连接池连接配置
c3p0连接池配置 xml文件内容如下: C3P0 通过这些属性,可以对数据源进行各种有效的控制 lc_biz_datasource_c3p0.properties 配置: lc_biz_dataso ...
- eclipse启动tomcat错误:A Java Exception has occurred
在tomcat bin目录下执行startup.bat可以正常启动,但在eclipse下安装了tomcat插件并且配置tomcat路径后启动且报错:A Java Exception has occur ...
- ExtJS入门教程04,这是一个超级好用的grid
今天进行extjs入门教程的第四篇:grid. 来一份grid尝尝 小伙伴们都知道extjs的grid功能强大,更清楚功能强大的东西用起来必然会复杂.今天我们就从最简单的grid开始讲解. 先来一个最 ...
- MyEclipse------各种问题解决方法
1.汉化后如何变为英文版:找到myeclipse.ini文件,改为:language=enlanguage=zh为中文 2.解决版本不匹配问题:http://blog.sina.com.cn/s/bl ...
- matlab学习笔记 bsxfun函数
matlab学习笔记 bsxfun函数 最近总是遇到 bsxfun这个函数,前几次因为无关紧要只是大概看了一下函数体去对比结果,今天再一次遇见了这个函数,想想还是有必要掌握的,遂查了些资料总结如下. ...
- 锋利的jQuery-1--解决jquery库和其他库的冲突
在jquery中,$(美元符号)就是jquery的别名,也就是说使用$和使用jquery是一样的,在很多时候我们命名空间时,正是因为这 个$而产生的冲突的发生.比如说:$('#xmlas')和JQue ...
- 安装 RPM 包或者安装源码包
安装 RPM 包或者安装源码包 在windows下安装一个软件很轻松,只要双击.exe的文件,安装提示连续“下一步”即可,然而linux系统下安装一个软件似乎并不那么轻松了,因为我们不是在图形界面下. ...
- MongoDB的快速手动安装
上一篇文章<MongoDB.WebIDE:升级版的Mongodb管理工具>漏了点东西:就是关于MongoDB主从库的安装配置和启动.网上关于MongoDB的安装有大量的文章供大家学习.我这 ...
- windows下安装redis以及简单的事例
1.安装服务端下载地址:http://code.google.com/p/servicestack/wiki/RedisWindowsDownload我下载了一个 redis-2.0.0服务器包,解压 ...
- 简单聊下Unicode和UTF-8
今晚听同事分享提到这个,简单总结下. ## Unicode字符集 Unicode的出现是因为ASCII等其他编码码不够用了,比如ASCII是英语为母语的人发明的,只要一个字节8位就能够表示26个英文字 ...