#757 (Div. 2) D. Divan and Kostomuksha(约数+dp)

题目描述

This is the hard version of the problem. The only difference is maximum value of ai.
Once in Kostomuksha Divan found an array a consisting of positive integers. Now he wants to reorder the elements of a to maximize the value of the following function:
∑i=1ngcd(a1,a2,…,ai),
where gcd(x1,x2,…,xk) denotes the greatest common divisor of integers x1,x2,…,xk, and gcd(x)=x for any integer x.
Reordering elements of an array means changing the order of elements in the array arbitrary, or leaving the initial order.
Of course, Divan can solve this problem. However, he found it interesting, so he decided to share it with you.

Input

The first line contains a single integer n (1≤n≤105) — the size of the array a.
The second line contains n integers a1,a2,…,an (1≤ai≤2⋅107) — the array a.

Output

Output the maximum value of the function that you can get by reordering elements of the array a.

Examples

input
6
2 3 1 2 6 2
output
14
input
10
5 7 10 3 1 10 100 3 42 54
output
131

Note

In the first example, it’s optimal to rearrange the elements of the given array in the following order: [6,2,2,2,3,1]:
gcd(a1)+gcd(a1,a2)+gcd(a1,a2,a3)+gcd(a1,a2,a3,a4)+gcd(a1,a2,a3,a4,a5)+gcd(a1,a2,a3,a4,a5,a6)=6+2+2+2+1+1=14.
It can be shown that it is impossible to get a better answer.
In the second example, it’s optimal to rearrange the elements of a given array in the following order: [100,10,10,5,1,3,3,7,42,54].

题目分析

easy version

这 道 题 我 们 可 以 通 过 d p 来 做 这道题我们可以通过dp来做 dp

状 态 表 示 : 设 f [ i ] 表 示 以 i 为 开 头 , 所 能 得 到 的 答 案 状态表示:设f[i]表示以i为开头,所能得到的答案 f[i]i

状 态 计 算 : 状态计算:
对 于 f [ i ] 来 说 , 将 i 放 在 开 头 , 我 们 首 先 应 该 把 序 列 中 所 有 i 的 倍 数 放 在 i 后 面 , 这 样 才 能 取 得 最 优 解 。 对于f[i]来说,将i放在开头,我们首先应该把序列中所有i的倍数放在i后面,这样才能取得最优解。 f[i]iii

当 从 状 态 i 转 移 到 状 态 j 时 , 我 们 可 以 发 现 只 有 j 是 i 的 倍 数 时 , 转 移 才 有 意 义 。 当从状态i转移到状态j时,我们可以发现 只有j是i的倍数时,转移才有意义。 ijji

设 j 为 i 的 倍 数 , 我 们 可 以 发 现 : 因 为 j 是 i 的 倍 数 , 所 以 j 的 倍 数 同 时 也 是 i 的 倍 数 , 并 且 c n t [ j ] < = c n t [ i ] ( c n t [ x ] 表 设j为i的倍数,我们可以发现:因为j是i的倍数,所以j的倍数同时也是i的倍数,并且cnt[j]<=cnt[i](cnt[x]表 jijijicnt[j]<=cnt[i]cnt[x] 示 序 列 中 为 x 的 整 数 倍 的 数 的 个 数 ) 。 示序列中为x的整数倍的数的个数)。 x

对 于 f [ j ] 来 说 , 前 c n t [ j ] 个 数 的 贡 献 为 j , 而 对 于 f [ i ] 来 说 , 前 c n t [ i ] 个 数 的 贡 献 为 i , 因 此 要 从 f [ i ] 转 移 到 f [ j ] , 需 要 对于f[j]来说,前cnt[j]个数的贡献为j,而对于f[i]来说,前cnt[i]个数的贡献为i,因此要从f[i]转移到f[j],需要 f[j]cnt[j]jf[i]cnt[i]if[i]f[j] 加 上 ( j − i ) ∗ c n t [ j ] 的 差 值 加上(j-i)*cnt[j]的差值 (ji)cnt[j]

即 : f [ j ] = m a x ( f [ j ] , f [ i ] + ( j − i ) ∗ c n t [ j ] ) 即:f[j]=max(f[j],f[i]+(j-i)*cnt[j]) f[j]=max(f[j],f[i]+(ji)cnt[j])

代码如下
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <algorithm>
#include <iomanip>
#define LL long long
#define ULL unsigned long long
#define PII pair<int,int>
#define PLL pair<LL,LL>
#define PDD pair<double,double>
#define x first
#define y second
using namespace std;
const int N=5e6+5,mod=1e9+7;
int a[N],cnt[N];
LL f[N]; 
int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n;
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i],cnt[a[i]]++;
	int m=*max_element(a+1,a+1+n);			//求出a[]中的最大值m
	for(int i=1;i<=m;i++)					//计算cnt[]数组
		for(int j=i*2;j<=m;j+=i)
			cnt[i]+=cnt[j];

	f[1]=n;					//初始化,以1为开头的序列答案为n
	for(int i=1;i<=m;i++)							//枚举每个数i
		for(int j=i*2;j<=m;j+=i)					//枚举i的倍数
			f[j]=max(f[j],f[i]+(LL)(j-i)*cnt[j]);	//状态转移方程
	LL ans=0;
	for(int i=1;i<=m;i++) ans=max(ans,f[i]);	//找到最大值并输出
	cout<<ans<<endl; 
	return 0;
}
hard version

困 难 版 本 中 , 增 大 了 序 列 最 大 值 , 因 此 如 果 直 接 d p 的 话 会 超 时 , 因 此 我 们 要 找 方 法 进 行 优 化 。 困难版本中,增大了序列最大值,因此如果直接dp的话会超时,因此我们要找方法进行优化。 dp

我 们 可 以 发 现 , 序 列 的 最 大 值 是 远 远 大 于 序 列 长 度 的 。 因 此 c n t [ ] 中 会 存 在 大 量 的 空 位 。 我 们 可 以 用 一 个 数 组 我们可以发现,序列的最大值是远远大于序列长度的。因此cnt[]中会存在大量的空位。我们可以用一个数组 cnt[] 记 录 一 下 那 些 位 置 是 有 用 的 , 在 状 态 转 移 的 时 候 如 果 某 个 位 置 没 用 到 就 直 接 c o n t i n u e 掉 即 可 记录一下那些位置是有用的,在状态转移的时候如果某个位置没用到就直接continue掉即可 continue

注 : 这 题 卡 常 卡 的 比 较 厉 害 注:这题卡常卡的比较厉害

代码如下
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <algorithm>
#include <iomanip>
#define LL long long
#define ULL unsigned long long
#define PII pair<int,int>
#define PLL pair<LL,LL>
#define PDD pair<double,double>
#define x first
#define y second
using namespace std;
const int N=2e7+5,mod=1e9+7;
bool vis[N];
LL cnt[N],f[N];
int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		int x;
		cin>>x;
		for(int j=1;j*j<=x;j++)				//求cnt[]数组
		{
			if(x%j==0)
			{
				cnt[j]++;
				if(j*j!=x) cnt[x/j]++;
				vis[j]=vis[x/j]=1;			//用到j和x/j这两个位置,记录一下
			}
		}
	}
	f[1]=n;
	LL ans=0;
	for(int i=1;i<N;i++)
	{
		if(!vis[i]) continue;					//如果该位置没有用到,直接continue
		for(int j=i*2;j<N;j+=i)
		{
			if(!vis[j]) continue;
			f[j]=max(f[j],f[i]+(j-i)*cnt[j]);	//状态转移方程
		}
		ans=max(ans,f[i]);						//记录最大值
	}
	cout<<ans<<endl;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lwz_159

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值