[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 ...
随机推荐
- java创建线程的几种方式
1.继承Thread类 /** * @author Ash * @date: 2016年8月6日 下午10:56:45 * @func: 通过继承Thread类来实现多线程 * @email 4086 ...
- angularJS 二
angularJS 2.1 ngForm <!DOCTYPE html> <html lang="zh-cn" ng-app> <head> ...
- [Tools] 使用XP远程登录Win8系统
[背景] 完成最基本的设置后,发现xp依然不能远程访问win8桌面,搜索后发现需要进一步设置 [开工] 按照参考资料进行设置,下面的参考资料已经写的很详细了,只是参考资料2中的文件名: redss ...
- 第十八篇:在SOUI中实现PreTranslateMessage
在MFC中,通常可以通过重载CWnd::PreTranslateMessage这样一个虚函数来实现对一些窗口消息的预处理.多用于tooltip的显示控制. 在SOUI中也实现了类似的机制. 要在SOU ...
- 工厂模式/factory模式/创建型模式
工厂模式 普通工厂模式 原本需要new出来的对象,通过一个类的方法去搞定,Factory.build(parameter),类似这种. public interface Sender { public ...
- 实现Activity刷新 (转)
目前刷新Acitivity,只想到几种方法.仅供参考,如果您有更好的方法,请赐教. 程序界面: 点击refresh view可以刷新界面,点击write content可以在EditText中自动写入 ...
- LayoutInflater(一)
相信接触Android久一点的朋友对于LayoutInflater一定不会陌生,都会知道它主要是用于加载布局的.而刚接触Android的朋友可能对LayoutInflater不怎么熟悉,因为加载布局的 ...
- 软引用SoftReference异步加载图片
HashMap<String, SoftReference<Drawable>> imageCache 关于SoftReference这个类多少知道些机制,会用就ok了. 机制 ...
- 关于MFC OpenGL环境配置的一点总结
复制include时要小心..看vs给你load哪一个..名字一样..东西可不一定一样哦 http://www.cppblog.com/wicbnu/archive/2010/09/30/128123 ...
- BroadCast,BroadCastReceiver
效果是:一个MainActivity动态注册一个BroadcastReceiver,BActivity发送一个标准广播,MainActivity接收到广播后,将广播中的消息显示在MainActivit ...