[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 ...
随机推荐
- AXURE在原型设计中的应用
转: http://uedc.163.com/2248.html 前言 什么是原型呢? 产品原型简单的说就是产品设计成形之前的一个简单框架,对网站来讲,就是将页面模块.元素进行粗放式的排版和布局,深入 ...
- Delphi线程同步(临界区、互斥、信号量)
当有多个线程的时候,经常需要去同步这些线程以访问同一个数据或资源. 例如,假设有一个程序,其中一个线程用于把文件读到内存,而另一个线程用于统计文件的字符数.当然,在整个文件调入内存之前,统计它的计数是 ...
- sdut1598 周游列国【简单模拟题】
周游列国 Time Limit: 1000ms Memory limit: 32768K 有疑问?点这里^_^ 题目描述 题目链接:http://acm.sdut.edu.cn/sdutoj/p ...
- gdo图形引擎中的旋转角
横滚角(Roll) bank.roll 绕y轴 z轴正向为起点逆时针方向:往左为正,往右为负,水平时为0:有效范围:-180度-180度 注:下图是从飞机的尾部-->头部方向观察所得 俯仰角( ...
- <转>Java 常用排序算法小记
排序算法很多地方都会用到,近期又重新看了一遍算法,并自己简单地实现了一遍,特此记录下来,为以后复习留点材料. 废话不多说,下面逐一看看经典的排序算法: 1. 选择排序 选择排序的基本思想是遍历数组的过 ...
- 1-03 Sql Sever 的身份验证模式
身份验证分为: 1:Windows身份验证. 1:Sql Sever身分验证. 每种验证的具体方式: 1Windows的验证方式 点击下拉框,有这两种验证方式,Windows验证只需要启动服务即可. ...
- hdu 4038 2011成都赛区网络赛H 贪心 ***
贪心策略 1.使负数为偶数个,然后负数就不用管了 2.0变为1 3.1变为2 4.2变为3 5.若此时操作数剩1,则3+1,否则填个1+1,然后回到5
- 在ASP.NET 5中如何方便的添加前端库
(此文章同时发表在本人微信公众号“dotNET每日精华文章”,欢迎右边二维码来关注.) 题记:ASP.NET 5和之前的ASP.NET版本有很大的不同,其中之一就是对前端库的管理不再使用Nuget,而 ...
- linux下搭建属于自己的博客(WordPress安装)
转自:http://www.cnblogs.com/xiaofengkang/archive/2011/11/16/2251608.html WordPress简介 WordPress 是一种使用 P ...
- [译]:Orchard入门——安装Orchard
原文链接:Installing Orchard 文章内容基于Orchard 1.8版本 安装Orchard的方式 主要有以下四种方式安装Orchard: 利用Microsoft Web Platfor ...