题目链接:传送门

游戏规则:

没次能够将一堆分成两堆 x = a*b (a!=1&&b!=1)x为原来堆的个数,a,b为新堆的个数。

也能够将原来的堆的个数变成原来堆的约数y。y!=x。进行最后一次操作的人获胜。

分析:

也是一个去石头的游戏,因此我们仅仅须要将全部情况的sg值异或起来就好了。

我们首先来考虑一堆。设这一堆的个数为x;

那么全部的情况就是

(a1,x/a1), (a2,x/a2),...,(an,x/an);或者(a1),(a2),..,(an)。

由于数据量比較大,我们朴素的找约数肯定会超时。

然后细致分析一下这个问题。由于我

们都是环绕着约数来进行操作。那么也就相当于在对他的素因子的个数进行操作。

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>
#include <algorithm>
using namespace std; const int maxn = 5000010; int prime[maxn],cnt;
bool isprime[maxn];
int fac_num[maxn];
int min_fac[maxn]; int sg[100]; void GetPirme(){
cnt=0;
memset(isprime,0,sizeof(isprime));
memset(fac_num,-1,sizeof(fac_num));
memset(min_fac,-1,sizeof(min_fac));
for(int i=2;i<maxn;i++){
if(!isprime[i]){
prime[cnt++]=i;
for(int j=i+i;j<maxn;j+=i){
isprime[j]=1;
if(min_fac[j]==-1)
min_fac[j]=i;
}
min_fac[i]=i;
}
}
} int get_num(int x){
if(x==1) return 0;
if(fac_num[x]!=-1) return fac_num[x];
return fac_num[x]=get_num(x/min_fac[x])+1;
} int Get_Sg(int x){
if(sg[x]!=-1) return sg[x];
bool vis[100];
memset(vis,0,sizeof(vis));
for(int i=1;i<=x;i++) vis[Get_Sg(x-i)]=1;
for(int i=1;i<=x/2;i++)
vis[Get_Sg(i)^Get_Sg(x-i)]=1;
for(int i=0;;i++){
if(!vis[i]){
return sg[x]=i;
}
}
} void init(){
GetPirme();
memset(sg,-1,sizeof(sg));
sg[0]=0;
} int main()
{
init();
int n;
while(~scanf("%d",&n)){
int x ,ans=0;
for(int i=0;i<n;i++){
scanf("%d",&x);
//cout<<"num: "<<get_num(x)<<endl;
ans^=Get_Sg(get_num(x));
}
if(ans) puts("Alice");
else puts("Bob");
}
return 0;
}

ACdream 1112 Alice and Bob (博弈&amp;&amp;素数筛选优化)的更多相关文章

  1. ACdream 1112 Alice and Bob(素筛+博弈SG函数)

    Alice and Bob Time Limit:3000MS     Memory Limit:128000KB     64bit IO Format:%lld & %llu Submit ...

  2. ACdream 1112 Alice and Bob (sg函数的变形+素数筛)

    题意:有N个数,Alice 和 Bob 轮流对这些数进行操作,若一个数 n=a*b且a>1,b>1,可以将该数变成 a 和 b 两个数: 或者可以减少为a或b,Alice先,问谁能赢 思路 ...

  3. ZOJ 3529 A Game Between Alice and Bob 博弈好题

    A Game Between Alice and Bob Time Limit: 5 Seconds      Memory Limit: 262144 KB Alice and Bob play t ...

  4. UVaLive 5760 Alice and Bob (博弈 + 记忆化搜索)

    题意:有 n 堆石子,有两种操作,一种是从一堆中拿走一个,另一种是把两堆合并起来,Alice 先拿,谁不能拿了谁输,问谁胜. 析:某些堆石子数量为 1 是特殊,石子数量大于 1 个的都合并起来,再拿, ...

  5. HDU 4111 Alice and Bob (博弈+记忆化搜索)

    题意:给定 n 堆石头,然后有两种操作,一种是把从任意一堆拿走一个,另一种是把一个石子放到另一堆上. 析:整体看,这个题真是不好做,dp[a][b] 表示有 a 堆1个石子,b个操作,操作是指把其他的 ...

  6. BZOJ 3895 3895: 取石子 / Luogu SP9934 ALICE - Alice and Bob (博弈 记忆化搜索)

    转自PoPoQQQ大佬博客 题目大意:给定n堆石子,两人轮流操作,每个人可以合并两堆石子或拿走一个石子,不能操作者输,问是否先手必胜 直接想很难搞,我们不妨来考虑一个特殊情况 假设每堆石子的数量都&g ...

  7. ACdream群赛1112(Alice and Bob)

    题意:http://acdream.info/problem?pid=1112 Problem Description Here  is Alice and Bob again ! Alice and ...

  8. Alice and Bob(博弈)

    Alice and Bob Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users ...

  9. Foj 2296 Alice and Bob(博弈、搜索)

    Foj 2296 Alice and Bob 题意 两个人博弈,规则如下:轮流取0~9中的数字,最后Alice所得的数字个数为1~n中,数位在Alice所取集合中出现奇数次的. 双方想获得尽量多,问A ...

随机推荐

  1. 【转】MySQL存储引擎中的MyISAM和InnoDB区别详解

    转自:http://www.jb51.net/article/62457.htm MyISAM是MySQL的默认数据库引擎(5.5版之前),由早期的ISAM(Indexed Sequential Ac ...

  2. 使用Jquery.form.js ajax表单提交插件弹出下载提示框

    现象: 使用jquery的from做ajax表单提交的时候,后台处理完毕返回json字符串,此时浏览器提示下载一个json文件而不是在success里面继续解析该json对象. 具体的原因: 浏览器兼 ...

  3. ios9 -3dtouch 手势添加到app上

    模拟器实现3dtouch参考以下网站: http://my.oschina.net/u/2340880/blog/511509 - (BOOL)application:(UIApplication * ...

  4. CentOS6.5下简单的MySQL数据库操作

    1.登录成功之后退出的话,直接输入quit或者exit即可.

  5. python gdal 数组生成图片

    cols = array.shape[1]rows = array.shape[0]originX = rasterOrigin[0]originY = rasterOrigin[1]driver = ...

  6. brew update失败提示:/System/Library/Frameworks/Ruby.framework/的解决方法

    本文由@ray 出品,转载请注明出处.  文章链接:http://www.cnblogs.com/wolfray/p/8040701.html 想用brew安装wget,但是提示失败,然后想先 bre ...

  7. 使用Hexo搭建个人博客配置全过程

    大致过程分为: 1.搭建Node.js 环境 2. 搭建Git 环境 3.安装配置Hexo 4.GitHub 注册和配置 5. 关联Hexo 与 GitHub Pages 7.Hexo的常用操作 下面 ...

  8. 控制台——EventLog实现事件日志操作

    我们应该如何通过写代码的方式向其中添加“日志”呢? 在操作之前,先明确几个概念: 1:事件日志名(logName):“事件查看器”中的每一项,如“应用程序”.“Internet Explorer”.“ ...

  9. Windows Server 2008无法远程连接

    Server 2008 R2依次配置好之后,重启发现总是远程桌面时而连接不上.具体现象如下: 偶尔可以通过桌面远程连接连接到Server.以为是防火墙的问题,各种设置——甚至关闭,依然无法连接.反复重 ...

  10. hdu 4876

    ZCC loves cards Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...