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 ...
随机推荐
- 配置 setting镜像在nexus私服上下载
在你的本地仓库上 setting文件中配置,一旦nexus服务关闭是无法下载的 1 配置nexus镜像 <mirror> <id>central1</id> < ...
- python读取文件时遇到非法字符的处理 UnicodeDecodeError: 'gbk' codec can't decode bytes in position
报错UnicodeDecodeError: 'gbk' codec can't decode bytes in position ipath = 'D:/学习/语料库/SogouC.mini/Samp ...
- 阶段3 1.Mybatis_02.Mybatis入门案例_1.mybatis的入门
H:\BaiDu\黑马传智JavaEE57期 2019最新基础+就业+在职加薪\讲义+笔记+资料\主流框架\31.会员版(2.0)-就业课(2.0)-Mybatis\mybatis\mybatis_d ...
- Mysql-5.7 x64安装
首先在官网下载Mysql:https://dev.mysql.com/downloads/mysql/ 选择ZIP Archive下载. 下载安装之后配置环境变量: 编辑现有环境变量Path: PS: ...
- Linx下Keepalived做成服务
在/usr目录下面执行: find -name keepalived 返回如下: ./sbin/keepalived ./local/sbin/keepalived ./local/etc/keepa ...
- Selenium学习之==>WebDriverApi接口详解
浏览器操作 driver.back() # 后退 driver.forward() # 前进 driver.refresh() # 刷新 窗口操作 driver.get_window_size() # ...
- 基于Python对象引用、可变性和垃圾回收详解
基于Python对象引用.可变性和垃圾回收详解 下面小编就为大家带来一篇基于Python对象引用.可变性和垃圾回收详解.小编觉得挺不错的,现在就分享给大家,也给大家做个参考. 变量不是盒子 在示例所示 ...
- 【Spring】的【Bean】管理(注解)【四个相同功能的注解】
[Spring]的[Bean]管理(注解)[四个相同功能的注解] 注解:代码里面特殊的标记,使用注解也可以完成一些相关的功能. 注解写法:@注解名称(属性名称=属性值) 注解使用在类.方法.属性上面 ...
- flutter SnackBar异常Another exception was thrown: Scaffold.of() called with a context that does not contain a Scaffold
代码如下: import 'package:flutter/material.dart'; void main() { runApp(MaterialApp( title: 'Returning Da ...
- 操作系统安全 - 提权 - Windows提权 - 汇总
CVE_2019-1388 Date: -- 影响范围: SERVER ====== Windows 2008r2 ** link OPENED AS SYSTEM ** Windows 2012r2 ...