Codeforces Round #757 (Div. 2)D1. Divan and Kostomuksha (easy version)

1.题目描述

给定a[n]数组,重新排列a[n],使得最大,求最大值。

2.状态表示:

2.1目标:

若最终答案对应的数组a'[n],

gcd(a'[1]) = a'[1],记作i。

则最终答案 = dp[i] + c[i] * i;(c[i]为i的倍数的出现的次数)

 2.2状态表示

参考2.1;

3.状态转移:

3.1转移形式:一个已知状态应该更新出哪些后续阶段的状态。

3.2具体解释:

已知状态:dp[i]

遍历后续阶段的状态:i 的倍数(除了i自己),记作j;

更新:dp[j] = max(dp[j], dp[i] + (c[i] - c[j]) * i);
 

4.代码

#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define x first
#define y second
typedef pair<int,int> PII;
typedef long long LL;
typedef unsigned long long ULL;
const int mod = 1e9 + 7;
const int N = 5e6 + 10, M = 2 * N, P =  131;


int n;
int a[N];
LL dp[N];
int c[N];

void solve()
{

    cin >> n;
	int maxv = 0;
	for(int i = 1; i <= n; i ++)
    {
		cin >> a[i];
		maxv = max(a[i], maxv);
		c[a[i]] ++;
	}

	for(int i = 1; i <= maxv; i ++)
		for(int j = 2 * i; j <= maxv; j += i)
			c[i] += c[j];

	LL ans = 0;
	for(int i = 1; i <= maxv; i ++)
		for(int j = i; j <= maxv; j += i)
		{
			dp[j] = max(dp[j], dp[i] + (LL)(c[i] - c[j]) * (LL)i);
			ans = max(ans, dp[j] + (LL)c[j] * j);
		}

	cout << ans << endl;

}
int main(){
std::ios::sync_with_stdio(false);std::cin.tie(0);
solve();}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值