ACdream 1112 Alice and Bob (博弈&&素数筛选优化)
题目链接:传送门
游戏规则:
没次能够将一堆分成两堆 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 (博弈&&素数筛选优化)的更多相关文章
- ACdream 1112 Alice and Bob(素筛+博弈SG函数)
Alice and Bob Time Limit:3000MS Memory Limit:128000KB 64bit IO Format:%lld & %llu Submit ...
- ACdream 1112 Alice and Bob (sg函数的变形+素数筛)
题意:有N个数,Alice 和 Bob 轮流对这些数进行操作,若一个数 n=a*b且a>1,b>1,可以将该数变成 a 和 b 两个数: 或者可以减少为a或b,Alice先,问谁能赢 思路 ...
- 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 ...
- UVaLive 5760 Alice and Bob (博弈 + 记忆化搜索)
题意:有 n 堆石子,有两种操作,一种是从一堆中拿走一个,另一种是把两堆合并起来,Alice 先拿,谁不能拿了谁输,问谁胜. 析:某些堆石子数量为 1 是特殊,石子数量大于 1 个的都合并起来,再拿, ...
- HDU 4111 Alice and Bob (博弈+记忆化搜索)
题意:给定 n 堆石头,然后有两种操作,一种是把从任意一堆拿走一个,另一种是把一个石子放到另一堆上. 析:整体看,这个题真是不好做,dp[a][b] 表示有 a 堆1个石子,b个操作,操作是指把其他的 ...
- BZOJ 3895 3895: 取石子 / Luogu SP9934 ALICE - Alice and Bob (博弈 记忆化搜索)
转自PoPoQQQ大佬博客 题目大意:给定n堆石子,两人轮流操作,每个人可以合并两堆石子或拿走一个石子,不能操作者输,问是否先手必胜 直接想很难搞,我们不妨来考虑一个特殊情况 假设每堆石子的数量都&g ...
- ACdream群赛1112(Alice and Bob)
题意:http://acdream.info/problem?pid=1112 Problem Description Here is Alice and Bob again ! Alice and ...
- Alice and Bob(博弈)
Alice and Bob Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users ...
- Foj 2296 Alice and Bob(博弈、搜索)
Foj 2296 Alice and Bob 题意 两个人博弈,规则如下:轮流取0~9中的数字,最后Alice所得的数字个数为1~n中,数位在Alice所取集合中出现奇数次的. 双方想获得尽量多,问A ...
随机推荐
- POJ 1873 计算几何
思路: 二进制枚举一下要删哪些点 求个凸包,算一下贡献 //By SiriusRen #include <cmath> #include <cstdio> #include & ...
- flask中的蓝图(BluePrint)
蓝图,听起来就是一个很宏伟的东西 在Flask中的蓝图 blueprint 也是非常宏伟的 它的作用就是将 功能 与 主服务 分开怎么理解呢? 比如说,你有一个客户管理系统,最开始的时候,只有一个查看 ...
- 【Leetcode 220】 Contains Duplicate III
问题描述:判断数组中是否存在<ai aj> abs(ai - aj)<=t && abs(i - j) <=k: 问题分析:需要一个数据结构来维护满足条件k. ...
- MVC系列学习(四)-初识Asp.NetMVC框架
注:本文章从伯乐那盗了两张图,和一些文字: 1.MVC设计模式 与 Asp.Net Mvc框架 a.MVC设计模式 MVC设计模式 是一种 软件设计模式,将业务逻辑 与 界面显示 分离,并通过某种方式 ...
- 初学者Android studio安装
学习过java基础,最近趁着大量课余时间想学习Android开发.百度很多资料Android studio,由Google开发的开发工具,那就不需要再多说.对于初学者的我来说,一定足够用了.此文主要介 ...
- CSS——层级
层级问题:选中的盒子显示的效果并不完整,右边的边框并没有显示红色,原因是其右边的盒子压了它的边框. <!DOCTYPE html> <html lang="en" ...
- MFC CAD控制权问题
begineditorcommand(); 隐藏对话框 把控制权交给CAD completeeditorcommand(); 完成交互返回到应用程序 canceleditorcommand CAD被 ...
- JSON,对象..的数据格式
[此案例为自动产生的随机数] 对象: {a1:180,a2:721, a3:574} 序列化传值:将对象转化为Json字符串 public ActionResult Val2() { Random r ...
- vue刷新本页面
顶层app.vue页面 <template> <div id="app"> <router-view v-if="isRouterAlive ...
- sysbench基准测试工具
一.简介SysBench是一个模块化的.跨平台.多线程基准测试工具,主要用于评估测试各种不同系统参数下的数据库负载情况.当前功能允许测试的系统参数有:file I/O performance (文件I ...