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 ...
随机推荐
- LeetCode 300——最长上升子序列
1. 题目 2. 解答 2.1. 动态规划 我们定义状态 state[i] 表示以 nums[i] 为结尾元素的最长上升子序列的长度,那么状态转移方程为: \[state[i] = max(state ...
- C# App.config 自定义 配置节
1)App.config <?xml version="1.0" encoding="utf-8" ?><configuration> ...
- 阶段3 1.Mybatis_12.Mybatis注解开发_5 mybatis注解建立实体类属性和数据库表中列的对应关系
创建新项目,一对多 复制刚才关闭的项目的文件 复制到们的新项目里面 复制包的依赖 删减相关代码.只保留这三个查询的方法 模糊查询改成传统的占位符的方式 之前是可以自定义实体类的属性字段,和数据库的字典 ...
- SAP屏幕事件的控制
1. INITALIZATION事件 该事件在屏幕未显示之前执行,对程序设置值及屏幕元素进行初始化赋值. REPORT Y001. PARAMETERS QUAL_DAY TYPE D DEFAUL ...
- vs2010发布网站时有些文件没有发布出去的解决办法。
项目中包含了一些ttf字体文件做为图标使用,可是发布时发现生成的目录中没有这个文件,这种情况这么设置一下就可以解决: 1,在文件上点击右键,选择“属性”. 2,在弹出的属性窗口中,更改“生成操作”为“ ...
- excel 导入导出测试点
目前,为方便操作,很多系统都会增加批量导入导出的功能.文件导入导出一般格式都是excel.由于用户直接在excel在填写内容,无法控制填写的格 式,加上excel解析比较困难,所以一般涉及到excel ...
- Django 自带 user 字段扩展及头像上传
django 及 rest_framework 笔记链接如下: django 入门笔记:环境及项目搭建 django 入门笔记:数据模型 django 入门笔记:视图及模版 django 入门笔记:A ...
- 无法获得锁 /var/lib/dpkg/lock-frontend - open (11: 资源暂时不可用)
ubuntu更新软件时 apt-get upgrade 遇到 E: 无法获得锁 /: 资源暂时不可用) E: Unable to acquire the dpkg frontend lock (/va ...
- Nginx代理自动上线下线
Nginx基于连接探测,如果发现后端异常,在单位周期为fail_timeout设置的时间,中达到max_fails次数,这个周期次数内,如果后端同一个节点不可用,那么接将把节点标记为不可用,并等待下一 ...
- HttpURLConnection 发送http请求帮助类
java 利用HttpURLConnection 发送http请求 提供GET / POST /上传文件/下载文件 功能 import java.io.*; import java.net.*; im ...