Codeforces Round #614 (Div. 1) D. Chaotic V.

185 篇文章 0 订阅
116 篇文章 0 订阅

"Everything has been planned out. No more hidden concerns. The condition of Cytus is also perfect.

The time right now… 00:01:12…

It’s time."

The emotion samples are now sufficient. After almost 3 years, it’s time for Ivy to awake her bonded sister, Vanessa.

The system inside A.R.C.'s Library core can be considered as an undirected graph with infinite number of processing nodes, numbered with all positive integers (1,2,3,…). The node with a number 𝑥 (𝑥>1), is directly connected with a node with number 𝑥𝑓(𝑥), with 𝑓(𝑥) being the lowest prime divisor of 𝑥.

Vanessa’s mind is divided into 𝑛 fragments. Due to more than 500 years of coma, the fragments have been scattered: the 𝑖-th fragment is now located at the node with a number 𝑘𝑖! (a factorial of 𝑘𝑖).

To maximize the chance of successful awakening, Ivy decides to place the samples in a node 𝑃, so that the total length of paths from each fragment to 𝑃 is smallest possible. If there are multiple fragments located at the same node, the path from that node to 𝑃 needs to be counted multiple times.

In the world of zeros and ones, such a requirement is very simple for Ivy. Not longer than a second later, she has already figured out such a node.

But for a mere human like you, is this still possible?

For simplicity, please answer the minimal sum of paths’ lengths from every fragment to the emotion samples’ assembly node 𝑃.

Input

The first line contains an integer 𝑛 (1≤𝑛≤10^6) — number of fragments of Vanessa’s mind.

The second line contains 𝑛 integers: 𝑘1,𝑘2,…,𝑘𝑛 (0≤𝑘𝑖≤5000), denoting the nodes where fragments of Vanessa’s mind are located: the 𝑖-th fragment is at the node with a number 𝑘𝑖!.

Output

Print a single integer, denoting the minimal sum of path from every fragment to the node with the emotion samples (a.k.a. node 𝑃).

As a reminder, if there are multiple fragments at the same node, the distance from that node to 𝑃 needs to be counted multiple times as well.

如果去掉恶心的建图逻辑,题意就是给一颗树并给树上的一些点赋上值,现在要求一个根使得根到所有点的加权路径和最小,做法有很多,最简单的做法就是先随便选一个点当根,然后不断尝试调整即可,注意这道题不需要根据题意显式的把图给建出来,标程用了一种比较trick的做法来维护了每次调整根后的子节点的状态。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cstring>
#include <unordered_map>
#include <map>

using namespace std;

typedef long long ll;
typedef vector<int> vci;
typedef pair<int, int> pii;
template <typename T> void UpdateMax(T& a, T b) {if (a < b) a = b;}
template <typename T> void UpdateMin(T& a, T b) {if (a > b) a = b;}
template<typename T>
inline void read(T &x)
{
    x = 0; char c = getchar(); int flag = 1;
    while(!isdigit(c)) {if (c == '-') flag = -1; c = getchar();}
    while(isdigit(c)) {x = x * 10 + c - '0'; c = getchar();}
    x *= flag;
}
template <typename T, typename V> void Print(const pair<T, V>& x) { cout<<x.first<<" @ "<<x.second<<" | "; }
template <typename T> void Print(const T& x) { cout<<x<<" ";  }
template <typename T> void Pcon(const T& x) { cout<<"\nsize: "<<x.size()<<endl; for (const auto& item : x) Print(item); }
const int N = 5005;
int n, x, f[N][N], pre[N], total[N];
ll Count[N], ans;
int main()
{
    read(n);
    for (int i = 0; i < n ;i++)
    {
        read(x);
        UpdateMax(x, 1);
        Count[x]++;
    }
    for (int i = 1; i < N; i++)
    {
        memcpy(f[i], f[i-1], sizeof(f[i]));
        pre[i] = 1;
        for (int j = 2, p = i; j <= i; j++)
        {
            while (p % j == 0)
            {
                f[i][j]++;
                p /= j;
            }
            if (f[i][j]) pre[i] = j;
            ans += f[i][j] * Count[i];
        }
    }
    for (int i = 1; i < N; i++)
    {
        if (!Count[i]) continue;
        total[pre[i]] += Count[i];
    }
    int temp = max_element(total + 1, total + N) - total;
    while (total[temp] && temp > 1)
    {
        if (total[temp] <= n/2) break;
        ans += n - 2*total[temp];
        memset(total, 0, sizeof(total));
        for (int i = 1; i < N; i++)
        {
            if (pre[i] != temp)
            {
                pre[i] = -1;
                continue;
            }
            f[i][pre[i]]--;
            while (!f[i][pre[i]] && pre[i] != 1) pre[i]--;
            if (pre[i] != 1)
            {
                total[pre[i]] += Count[i];
            }
        }
        temp = max_element(total + 1, total + N) - total;
    }
    cout<<ans<<endl;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值