POI2012 ODL-Distance
链接P3532 [POI2012]ODL-Distance
设\(f_{i,j}\)表示他给定的函数,\(g_i\)表示\(i\)的质因数个数
那么$$f_{i,j}=g_{\frac {i*j}{gcd^2}}$$
考虑线性筛\(g_i\)。
那么对于每一个数\(w_i\)考虑枚举他的因子作为\(gcd\)。
也就是枚举\(x\),对于\(x\),枚举所有\(x\)的倍数\(y\)。
所以我们预处理出\(h_x\)表示\(x\)和他的倍数中,\(f_x\)的最小值的编号。
但是我们在计算\(i\)这个数的答案的时候,不能用\(i\)来更新本身。
所以在设\(p_x\)表示\(x\)和他的倍数中,\(f_x\)的次小值的编号。
所以在更新每个位置的答案的时候,我们枚举\(gcd\),然后再枚举\(gcd\)的\(h_x\)和\(p_x\)。
假设当前值是\(a\),如果\(h_x\)是本身,就用\(p_x\)和\(a\)去更新答案,否则就用\(h_x\)去更新答案。
即\(f_{i,j}=g_i+g_j-2*g_{gcd}\)
这样虽然不能保证\(x\)是\(a,b\)的\(gcd\),但是这样一定不是最优的,\(a,b\)的\(gcd\)一定会被枚举到并且成为最优解,所以答案一定会被更新成最优的。
#include<bits/stdc++.h>
#define R register int
#define ll long long
using namespace std;
const int N=1000001;
const int inf=2e9;
int n,Mx,w[N],ans[N];
int tot,c[N],f[N],g[N],cnt[N],Mark[N],prm[N>>2];
int gi(){
R x=0,k=1;char c=getchar();
while((c<'0'||c>'9')&&c!='-')c=getchar();
if(c=='-')k=-1,c=getchar();
while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+c-'0',c=getchar();
return x*k;
}
void init(){
for(R i=2;i<=Mx;++i){
if(!Mark[i])prm[++tot]=i,c[i]=1;
for(R j=1;j<=tot&&prm[j]*i<=Mx;++j){
Mark[i*prm[j]]=1,c[i*prm[j]]=c[i]+1;
if(i%prm[j]==0)break;
}
}
}
int main(){
freopen("9.in","r",stdin);
freopen("s.out","w",stdout);
n=gi();
for(R i=1;i<=n;++i)w[i]=gi(),Mx=max(Mx,w[i]);
init(),c[0]=2e9;
for(R i=1;i<=n;++i){
for(R j=1;j*j<=w[i];++j){
if(w[i]%j)continue;
R k=w[i]/j;
if(c[w[i]]<c[w[f[j]]])g[j]=f[j],f[j]=i;
else if(c[w[i]]<c[w[g[j]]])g[j]=i;
if(j==k)continue;
if(c[w[i]]<c[w[f[k]]])g[k]=f[k],f[k]=i;
else if(c[w[i]]<c[w[g[k]]])g[k]=i;
}
}
for(R i=1;i<=n;++i){
R ans=0,Mn=2e9;
for(R j=1;j*j<=w[i];++j){
if(w[i]%j)continue;
R k=w[i]/j,x=0;
if(f[j]==i)x=g[j];
else x=f[j];
if(Mn>c[w[i]]+c[w[x]]-2*c[j]||(Mn==c[w[i]]+c[w[x]]-2*c[j]&&ans>x))
Mn=c[w[i]]+c[w[x]]-2*c[j],ans=x;
if(j==k)continue;
if(f[k]==i)x=g[k];
else x=f[k];
if(Mn>c[w[i]]+c[w[x]]-2*c[k]||(Mn==c[w[i]]+c[w[x]]-2*c[k]&&ans>x))
Mn=c[w[i]]+c[w[x]]-2*c[k],ans=x;
}
printf("%d\n",ans);
}
return 0;
}
POI2012 ODL-Distance的更多相关文章
- [BZOJ2790][Poi2012]Distance
2790: [Poi2012]Distance Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 225 Solved: 115[Submit][Sta ...
- 【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 很显然可以差分约束建图.这里问的是变量最多有多少种不同的取值. 我们知道,在同一个强连通分 ...
- 洛谷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 ...
- [LeetCode] Shortest Word Distance II 最短单词距离之二
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...
随机推荐
- Linux 下ThinkPHP项目出现_STORAGE_WRITE_ERROR_:./Application/Runtime/Cache/Admin/0dfec61edd66f450033aa87c28a760f4.php
在Linux中部署了ThinkPHP项目,访问时却出现了_STORAGE_WRITE_ERROR_:./Application/Runtime/Cache/Admin/0dfec61edd66f450 ...
- Microsoft:Team Foundation Server 20XX Release Notes
ylbtech-Microsoft:Team Foundation Server 2017 Release Notes 1.返回顶部 1. https://docs.microsoft.com/en- ...
- java网络通信:同步阻塞式I/O模型(BIO)
缺点:一个线程只能处理一个客户端连接 服务端: public class TimeServer { public static void main(String[] args) throws IOEx ...
- IDEA maven 阿里云镜像 下载jar失败
我在使用IDEA搭建SpringCloud项目的时候,发现很多spring cloud 包总是下载不了.maven配置了阿里云镜像.在换了一下新版的镜像地址以后就可以下包了. 官方答疑:新版maven ...
- Delphi 快速检测是否联网 判断网线是否拔开。 但是不能判断是否能上网
https://blog.csdn.net/chelen_jak/article/details/50204145 Delphi 快速检测是否联网 2015年12月07日 12:01:26 chele ...
- wcf restful 访问报错 *.svc HTTP error 404.17 - Not Found
安装完成 iisreset,即使不重启也已经可以使用了
- c++ 创建 uuid guid
如果没安装,先安装: [root@localhost]# yum install libuuid-devel #include "uuid/uuid.h" 引用 libuuid.s ...
- 图解 SQL 里的各种 JOIN
约定 下文将使用两个数据库表 Table_A 和 Table_B 来进行示例讲解,其结构与数据分别如下: mysql> SELECT * FROM Table_A ORDER BY PK ASC ...
- 关于android工具链
1 android sdk platform tools 同android platform交互的工具,包括adb.fastboot和systrace. 2 sdk build tools 用于bui ...
- 使用批处理命令注册运行mysql数据库,无需注册mysql服务,可以在任意电脑登录使用
使用批处理命令初始化和开启mysql服务,移植数据库之后可以直接运行访问,对于学习数据库的人来说特别的方便哦. 我们可以从mysql官网下载官方社区版本的mysql: 这里使用之前下载的8.0.15来 ...