链接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的更多相关文章

  1. [BZOJ2790][Poi2012]Distance

    2790: [Poi2012]Distance Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 225  Solved: 115[Submit][Sta ...

  2. 【BZOJ2790】[Poi2012]Distance 筛素数+调和级数

    [BZOJ2790][Poi2012]Distance Description 对于两个正整数a.b,这样定义函数d(a,b):每次操作可以选择一个质数p,将a变成a*p或a/p, 如果选择变成a/p ...

  3. POI2012题解

    POI2012题解 这次的完整的\(17\)道题哟. [BZOJ2788][Poi2012]Festival 很显然可以差分约束建图.这里问的是变量最多有多少种不同的取值. 我们知道,在同一个强连通分 ...

  4. 洛谷P3533 [POI2012]RAN-Rendezvous

    P3533 [POI2012]RAN-Rendezvous 题目描述 Byteasar is a ranger who works in the Arrow Cave - a famous rende ...

  5. [LeetCode] Total Hamming Distance 全部汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  6. [LeetCode] Hamming Distance 汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

  10. [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 ...

随机推荐

  1. 关于自定义 List集合排序的方法!

    大致流程: 排序是用到排序的接口Comparator<T>你要先建一个类实现比较器Comparator //大致流程public class StuComp implements Comp ...

  2. 阶段3 1.Mybatis_03.自定义Mybatis框架_4.自定义mybatis的编码-解析XML的工具类介绍

    导入xml操作的类和用到的相关包 创建util包,然后把提供好的XMLConfigBuilder.java文件复制3过来 复制过来,里面用到了很多dom4j的东西 打开pom.xml 输入depend ...

  3. Cloudera-JDBC-Driver-for-Apache-Hive

    Cloudera-JDBC-Driver-for-Apache-Hive-Install-Guide.pdf https://github.com/FlowerBirds/flowerbirds.gi ...

  4. Ubuntu vimrc 和 bashrc 配置

    先上效果图,把vimrc 和bashrc 备份一下.. vimrc: map <F9> :call SaveInputData()<CR> func! SaveInputDat ...

  5. 问题记录 | deepin15.10重装nvidia驱动及cuda

    问题描述: nvidia-smi也有显示,显卡驱动是在的,而且nvcc显示出来的cuda版本9.0也没错,不是9.1.不知道问题所在,索性重装全部. sudo tee /proc/acpi/bbswi ...

  6. django中使用原生的orm

    只有用的时候才能执行

  7. 【C语言--数据结构】线性顺序表

    线性表的本质: 1.线性表(List)是零个或者多个数据元素的集合: 2.线性表中的数据元素之间是有顺序的: 3.线性表中的数据元素个数是有限的: 4.线性表中的数据元素的类型必须相同: 定义: 线性 ...

  8. [知乎]ARM 到底是什么

    [小宅按]近期公司推出来基于ARM芯片的服务器,本文就一些基本概念,比如ARM, ARM64, ARMv8, ARM7,ARMv7, 64位等让人费解的概念进行了粗浅地分析,涉及的关键字已用粗体标出. ...

  9. Java——LinkedList使用Demo

    package list; import java.util.Iterator; import java.util.LinkedList; public class LinkedListDemo { ...

  10. 04: DjangoRestFramework使用

    Django其他篇 目录: 1.1 DjangoRestFramework基本使用 1.2 drf认证&权限 模块 1.3 djangorestframework 序列化 1.4 django ...