Zoj 3529 A Game Between Alice and Bob 数论+博弈Nim 快速求数中有多少个素数因子
本题涉及博弈论中的Nim游戏博弈。
Nim游戏博弈详解链接:
http://www.cnblogs.com/exponent/articles/2141477.html
本题解题报告详解链接:
http://blog.csdn.net/woshi250hua/article/details/7824609
我的代码,写的较挫,4000多ms,压着时间过,时间限制是5s.我预处理了所有1-5*10^6的数的素因子个数,用的是dp
的思想,先预处理1-5*10^6的数的最小素因子,存在a数组中,然后dp[i] = dp[i/a[i] ]+1;
我觉得我代码最挫的地方是在求最小素因子时费时太多。。。。
#include <cstdio>
#define N 5000005
#define INF 0x7fffffff
int p[N];//每个数有多少个素数因子
int a[N];//每个数最小的素数因子
int prime[];//存素数表
void init()
{
//打素数表,求出1-5005以内的素数和每个数的最小质因子
for(int i=; i<; ++i)
prime[i] = INF;
prime[] = ;
int num = ;
for(int i=; i<; ++i)
{
int x = ;
while(i%prime[x] && prime[x] <= i) ++x;
if( !(i%prime[x]) )
a[i] = prime[x];
else
{
prime[++num] = i;
a[i] = i;
}
}
a[] =;
for(int i=; i< N; ++i)
{
int x = ;
while(i%prime[x] && prime[x] <= i) ++x;
if( !(i%prime[x]) )
a[i] = prime[x];
else
a[i] = i;
}
p[] = ;
for(int i=; i <N; ++i)
p[i] = p[i/a[i]] + ;
}
int main()
{
// freopen("in.cpp","r",stdin);
init();
int n;
int b[];
int t=;
while(scanf("%d",&n) != EOF)
{
int ans ;
for(int i=; i<n; ++i)
{
scanf("%d",&b[i]);
if(i)
ans ^= p[b[i]];
else
ans = p[b[i]];
}
printf("Test #%d: ",++t);
if(ans)
{
int index;
for(int i=; i<n; ++i)
if( (p[b[i]]^ans) < p[b[i]] )
{
index = i+;
break;
}
printf("Alice %d\n",index);
}
else printf("Bob\n");
}
return ;
}
这是改进后的代码,用线性筛法在打素数表的同时求出最小素因子····
初始化时每个数的最小素因子就是本身。
#include <cstdio>
#define N 5000005
#define INF 0x7fffffff
int p[N];//每个数有多少个素数因子
bool v[N]; //是否为素数
int a[N];//每个数最小的素数因子
int prime[N/];//素数表
//用打素数表的筛法求每个数的最小质因子
void init()
{
for(int i=; i<N; ++i)
a[i] = i;
int num=-;
for(int i=; i<N; ++i)
{
if(!v[i]) prime[++num] = i;
for(int j=; j<=num && i*prime[j] < N; ++j)
{
int t = i*prime[j];
v[t] =;
if(a[t] > prime[j]) a[t] = prime[j];
if(i%prime[j] == ) break;
}
}
p[] = ;
for(int i=; i <N; ++i)
p[i] = p[i/a[i]] + ;
} int main()
{
// freopen("in.cpp","r",stdin);
init();
int n;
int b[];
int t=;
while(scanf("%d",&n) != EOF)
{
int ans ;
for(int i=; i<n; ++i)
{
scanf("%d",&b[i]);
if(i)
ans ^= p[b[i]];
else
ans = p[b[i]];
}
printf("Test #%d: ",++t);
if(ans)
{
int index;
for(int i=; i<n; ++i)
if( (p[b[i]]^ans) < p[b[i]] )
{
index = i+;
break;
}
printf("Alice %d\n",index);
}
else printf("Bob\n");
}
return ;
}
Zoj 3529 A Game Between Alice and Bob 数论+博弈Nim 快速求数中有多少个素数因子的更多相关文章
- ZOJ 3529 A Game Between Alice and Bob(博弈论-sg函数)
ZOJ 3529 - A Game Between Alice and Bob Time Limit:5000MS Memory Limit:262144KB 64bit IO For ...
- 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 ...
- zoj 3529 A Game Between Alice and Bob 博弈论
思路:每个数的SG值就是其质因子个数,在进行nim博弈 代码如下: #include<iostream> #include<cstdio> #include<cmath& ...
- 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 ...
- HDU 5708 Alice and Bob (博弈,找规律)
题意: 一个无限大的棋盘,一开始在1,1,有三种移动方式,(x+1,y)(x,y+1) (x+k,y+k)最后走到nm不能走了的人算输.. 析:.我们看成一开始在(n,m),往1,1,走,所以自然可以 ...
- 2011 ACM-ICPC 成都赛区A题 Alice and Bob (博弈动规)
题目大意: 有K堆石子,每堆有Ki个.两人的操作能够是: 1 从某一堆拿走一个 假设该堆在此之后没有石子了.就消失 2 合并两个堆 求是否 ...
- ZOJ 3757 Alice and Bob and Cue Sports(模拟)
题目链接 题意 : 玩台球.Alice 和 Bob,一共可以进行m次,Alice 先打.有一个白球和n个标有不同标号的球,称目标球为当前在桌子上的除了白球以外的数值最小的球,默认白球的标号为0.如果白 ...
- 2014 Super Training #6 A Alice and Bob --SG函数
原题: ZOJ 3666 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3666 博弈问题. 题意:给你1~N个位置,N是最 ...
随机推荐
- eclipse安装adt插件后工具栏不显示android相关图标
到eclipse官网下载luna(开发android,推荐此版本,当前最新版4.4.2)版本的eclipse http://ftp.jaist.ac.jp/pub/eclipse/technology ...
- WPF中的动画
动画无疑是WPF中最吸引人的特色之一,其可以像Flash一样平滑地播放并与程序逻辑进行很好的交互.这里我们讨论一下故事板. 在WPF中我们采用Storyboard(故事板)的方式来编写动画,为了对St ...
- Vladik and Entertaining Flags CodeForces - 811E (并查集,线段树)
用线段树维护每一块左右两侧的并查集, 同色合并时若不连通则连通块数-1, 否则不变 #include <iostream> #include <algorithm> #incl ...
- layui的富文本编辑器怎么赋值
除了上面讲的方法外,还可以使用layedit自带的方法赋值/*** 设置编辑器内容* @param {[type]} index 编辑器索引* @param {[type]} content 要设置的 ...
- Python基础--列表、元组
一.什么是列表.元组 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. Python有6个序列的内置类型,但最常见 ...
- 秒杀多线程第六篇 经典线程同步 事件Event
原文地址:http://blog.csdn.net/morewindows/article/details/7445233 上一篇中使用关键段来解决经典的多线程同步互斥问题,由于关键段的“线程所有权” ...
- sql截取字符串后面四位
方法1: select substr('123456789',length('123456789')-6+1,6) from dual; 方法2: SELECT name, RIGHT(certid, ...
- learning uboot how to set ddr parameter in qca4531 cpu
DDR工作频率 在600MHZ. include/configs/board953x.h #define CFG_PLL_FREQ CFG_PLL_650_600_200 #d ...
- json批量设置DIV属性
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- JProfiler性能分析
之前已经介绍过如何调试本地的JBoss.现在额外一篇文章关于如何远程调试Tomcat的,其实远程和本地的区别不大,主要区别只是,JProfiler的GUI运行在你本地,而JProfiler的Agent ...