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 ...
随机推荐
- 506C Mr. Kitayuta vs. Bamboos
分析 代码 #include<bits/stdc++.h> using namespace std; #define int long long ],h[],now[],cnt[]; in ...
- Apache配置文件介绍
一.配置文件存放位置 apache配置文件名为httpd.conf 1.yum安装 yum安装后,apache配置文件httpd.conf存放在目录/etc/httpd/conf下 2.源码编译安装 ...
- WinFrom控件双向绑定
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- spring(二) JDBC
一.配置 bean.xml , 链接数据库. c3p0数据库连接池 <?xml version="1.0" encoding="UTF-8"?> & ...
- LeetCode.868-二进制距离(Binary Gap)
这是悦乐书的第333次更新,第357篇原创 01看题和准备 今天介绍的是LeetCode算法题中Easy级别的第203题(顺位题号是868).给定正整数N,找到并返回N的二进制表示中两个连续1之间的最 ...
- hdu5993/2016icpc青岛L
zz:https://www.cnblogs.com/ytytzzz/p/9674661.html 题意:给一棵树,每次询问删掉两条边,问剩下的三棵树的最大直径点10W,询问10W,询问相互独立 So ...
- linux下 sleep() 与 usleep()
usleep() 将进程挂起一段时间, 单位是微秒(百万分之一秒): 头文件: unistd.h 语法: void usleep(int micro_seconds); 返回值: 无 内容说明:本函数 ...
- 使用itchat完成微信自动回复
import itchat from itchat.content import * # 微信自动回复 @itchat.msg_register([TEXT]) def text_reply(msg) ...
- HttpURLConnection 发送http请求帮助类
java 利用HttpURLConnection 发送http请求 提供GET / POST /上传文件/下载文件 功能 import java.io.*; import java.net.*; im ...
- 【Linux 环境搭建】ubuntu 的samba配置
在/etc/samba/smb.conf的文件末尾增加下面的内容然后重启samba [home] comment = James Harden path = / browseable = yes wr ...