new file: CF1614D1.cpp

This commit is contained in:
hanyixuanten committed 2026-10-28 09:45:35 +00:00
1 parent e8764c0ce7
commit 4bf4f072db
2 files changed
+113

No files matched your search

+45
View File
@@ -0,0 +1,45 @@
#include<bits/stdc++.h>
using namespace std;
int n;
const int maxa = 20000000;
vector<int> prime;
bool vis[20000005];
int cnt[20000005];
long long f[20000005];
int a[100005];
int main() {
for (int i = 2; i <= 20000000; ++i) {
if (!vis[i]) prime.push_back(i);
for (int p : prime) {
if (1ll * i * p > maxa) break;
vis[i * p] = true;
if (i % p == 0) break;
}
}
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
}
for(int i = 1; i <= n; ++i) {
for(int j = 1; j * j <= a[i]; ++j) {
if(a[i] % j == 0) {
cnt[j]++;
if(j * j != a[i]) ++cnt[a[i] / j];
}
}
}
for(int i = maxa; i >= 1; --i) {
f[i] = 1ll * i * cnt[i];
for(int j = 0; j < prime.size() && i * prime[j] <= maxa; ++j) {
f[i] = max(f[i], f[i * prime[j]] + 1ll * i * (cnt[i] - cnt[i * prime[j]]));
}
}
long long ans = 0;
for(int i = 1; i <= maxa; ++i) {
if(cnt[i] == n) {
ans = max(ans, f[i]);
}
}
printf("%lld\n", ans);
return 0;
}
+68
View File
@@ -0,0 +1,68 @@
# CF1614D1 Divan and Kostomuksha (easy version)
## 题目描述
这是该问题的简单版本。唯一的区别在于 $a_i$ 的最大值。
有一次在 Kostomuksha,Divan 发现了一个由正整数组成的数组 $a$。现在他想要重新排列 $a$ 的元素,使下列函数的值最大化:
$$
\sum_{i=1}^n \operatorname{gcd}(a_1,\, a_2,\, \dots,\, a_i),
$$
其中 $\operatorname{gcd}(x_1, x_2, \ldots, x_k)$ 表示整数 $x_1, x_2, \ldots, x_k$ 的[最大公约数](https://en.wikipedia.org/wiki/Greatest_common_divisor),且对于任意整数 $x$,有 $\operatorname{gcd}(x) = x$。
重新排列数组元素意味着可以任意改变数组中元素的顺序,也可以保持初始顺序不变。
当然,Divan 能解决这个问题。不过他觉得这个问题很有趣,所以决定分享给你。
## 输入格式
第一行包含一个整数 $n$($1 \leq n \leq 10^5$),表示数组 $a$ 的大小。
第二行包含 $n$ 个整数 $a_{1},\, a_{2},\, \dots,\, a_{n}$($1 \le a_{i} \le 5 \cdot 10^6$),表示数组 $a$。
## 输出格式
输出通过重新排列数组 $a$ 能得到的函数最大值。
## 输入输出样例 #1
### 输入 #1
```
6
2 3 1 2 6 2
```
### 输出 #1
```
14
```
## 输入输出样例 #2
### 输入 #2
```
10
5 7 10 3 1 10 100 3 42 54
```
### 输出 #2
```
131
```
## 说明/提示
在第一个样例中,将给定数组的元素按如下顺序排列是最优的:$[6,\, 2,\, 2,\, 2,\, 3,\, 1]$:
$$
\operatorname{gcd}(a_1) + \operatorname{gcd}(a_1,\, a_2) + \operatorname{gcd}(a_1,\, a_2,\, a_3) + \operatorname{gcd}(a_1,\, a_2,\, a_3,\, a_4)\\ + \operatorname{gcd}(a_1,\, a_2,\, a_3,\, a_4,\, a_5) + \operatorname{gcd}(a_1,\, a_2,\, a_3,\, a_4,\, a_5,\, a_6)\\= 6 + 2 + 2 + 2 + 1 + 1 = 14。
$$
可以证明无法得到更优的答案。
在第二个样例中,将给定数组的元素按如下顺序排列是最优的:$[100,\, 10,\, 10,\, 5,\, 1,\, 3,\, 3,\, 7,\, 42,\, 54]$。
由 ChatGPT 4.1 翻译