[BZOJ2790][Poi2012]Distance
2790: [Poi2012]Distance
Time Limit: 40 Sec Memory Limit: 128 MB
Submit: 225 Solved: 115
[Submit][Status][Discuss]
Description
对于两个正整数a、b,这样定义函数d(a,b):每次操作可以选择一个质数p,将a变成a*p或a/p,
如果选择变成a/p就要保证p是a的约数,d(a,b)表示将a变成b所需的最少操作次数。例如d(69,42)=3。
现在给出n个正整数A1,A2,...,An,对于每个i (1<=i<=n),求最小的j(1<=j<=n)使得i≠j且d(Ai,Aj)最小。
Input
第一行一个正整数n (2<=n<=100,000)。第二行n个正整数A1,A2,...,An (Ai<=1,000,000)。
Output
输出n行,依次表示答案。
Sample Input
1
2
3
4
5
6
Sample Output
1
1
2
1
2
HINT
Source
很容易推出来 $d(x,y)=g(x)+g(y)-2*g(\gcd(x,y))$ 其中g(x)表示x是几个质数的乘积
$g$函数只需要一个线性筛就能求出来,对于$a_i$,$g(a_i)$是固定的,重点在于最小化$g(y)-2*g(gcd(x,y))$
可以枚举$a_i$的因子$x$,用$f[x]$表示是$x$的倍数的$a[j]$使得$g[a[j]]$最小的数
因为要求$i≠j$,所以得维护最小值和次小值,时间复杂度$O(n\sqrt m+m)$ $m=\max{a_i}$
PS:今天BZOJ居然卡住了,管理员今天不在吗?
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 100010
#define M 1000010
int prime[M],tot,g[M];
inline void pre(int t)
{
g[]=1e9;
for(int i=;i<=t;i++)
{
if(!g[i])prime[++tot]=i,g[i]=;
for(int j=;j<=tot&&i*prime[j]<=t;j++)
{
g[i*prime[j]]=g[i]+;
if(!(i%prime[j]))break;
}
}
}
int n,a[N],f[M][],maxn;
inline void update(int x,int i)
{
if(g[a[x]]<=g[a[f[i][]]])
f[i][]=f[i][],f[i][]=x;
else if(g[a[x]]<=g[a[f[i][]]])
f[i][]=x;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]),maxn=max(a[i],maxn);
pre(maxn);
for(int i=n;i;i--)
{
for(int j=;j*j<=a[i];j++)
if(!(a[i]%j))
{
update(i,j);
if(j*j!=a[i])
update(i,a[i]/j);
}
}
for(int i=;i<=n;i++)
{
int ans=1e9,tmp=1e9;
for(int j=;j*j<=a[i];j++)
if(!(a[i]%j))
{
int k=j,x;
if(f[k][]!=i)x=f[k][];
else x=f[k][];
int t=g[a[x]]-*g[k];
if(t<ans||(t==ans&&x<tmp))
ans=t,tmp=x;
k=a[i]/j;
if(f[k][]!=i)x=f[k][];
else x=f[k][];
t=g[a[x]]-*g[k];
if(t<ans||(t==ans&&x<tmp))
ans=t,tmp=x;
}
printf("%d\n",tmp);
}
}
[BZOJ2790][Poi2012]Distance的更多相关文章
- 【BZOJ2790】[Poi2012]Distance 筛素数+调和级数
[BZOJ2790][Poi2012]Distance Description 对于两个正整数a.b,这样定义函数d(a,b):每次操作可以选择一个质数p,将a变成a*p或a/p, 如果选择变成a/p ...
- POI2012题解
POI2012题解 这次的完整的\(17\)道题哟. [BZOJ2788][Poi2012]Festival 很显然可以差分约束建图.这里问的是变量最多有多少种不同的取值. 我们知道,在同一个强连通分 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- 洛谷P3533 [POI2012]RAN-Rendezvous
P3533 [POI2012]RAN-Rendezvous 题目描述 Byteasar is a ranger who works in the Arrow Cave - a famous rende ...
- [LeetCode] Total Hamming Distance 全部汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [LeetCode] Hamming Distance 汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [LeetCode] Rearrange String k Distance Apart 按距离为k隔离重排字符串
Given a non-empty string str and an integer k, rearrange the string such that the same characters ar ...
- [LeetCode] Shortest Distance from All Buildings 建筑物的最短距离
You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...
- [LeetCode] Shortest Word Distance III 最短单词距离之三
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...
随机推荐
- CSS3学习
1.CSS3边框 border-radius:创建圆角边框 border-radius:25px; -moz-border-radius:25px; /* 老的 Firefox */ box-shad ...
- ExcelReport第三篇:扩展元素格式化器
导航 目 录:基于NPOI的报表引擎——ExcelReport 上一篇:ExcelReport源码解析 概述 上篇中已介绍了ExcelRepor的架构,本篇将通过例子讲述如何扩展元素格式化器以满足 ...
- 一个通过网络转换Ico到Png图片的小小程序(Ico2Png)
做软件界面需要用到ico文件, 结果皮肤库不支持ico格式的图标, 所以就想到了把ico转换成png. 网上ico转png的软件貌似并不多, 反倒是png转ico很大一片~~~~~~~~~ 要转换ic ...
- 攻城狮在路上(贰) Spring(三)--- Spring 资源访问利器Resource接口
Spring为了更好的满足各种底层资源的访问需求.设计了一个Resource接口,提供了更强的访问底层资源的能力.Spring框架使用Resource装载各种资源,包括配置文件资源.国际化属性文件资源 ...
- Linux重置root密码步骤
1.开机时任意按一个方向键,进入界面,选择linux系统,按e键进入2.然后用上下键选择kerner(内核)那一行,按e键进入编辑界面,编辑界面最后一行显示如下:(grub edit> kern ...
- hdu 4007 暴力or线段树 ***
尼玛,INF不能定义太大,找标程对拍了好久 #include<cstdio> #include<iostream> #include<algorithm> #inc ...
- RTP timestamp与帧率及时钟频率的关系
转自:http://blog.csdn.net/jasonhwang/article/details/7316128 RTP timestamp是用时钟频率(clock rate)计算而来表示时间的. ...
- POJ 2114 Boatherds 树分治
Boatherds Description Boatherds Inc. is a sailing company operating in the country of Trabantust ...
- 【SQL Server】数据库是单个用户的 无法顺利进行操作 怎么解决
1.打开数据库 2.新建查询 ,输入以下的SQL 语句 DECLARE @SQL VARCHAR(MAX); SET @SQL='' SELECT @SQL=@SQL+'; KILL '+RTRIM( ...
- android 获取文件夹、文件的大小 以B、KB、MB、GB 为单位
android 获取文件夹.文件的大小 以B.KB.MB.GB 为单位 public class FileSizeUtil { public static final int SIZETYPE_B ...