zoukankan      html  css  js  c++  java
  • 【枚举】【gcd】Codeforces Round #432 (Div. 2) D. Arpa and a list of numbers

    D. Arpa and a list of numbers
     

    Source

    http://codeforces.com/contest/849/problem/D

    Description

    Arpa has found a list containing n numbers. He calls a list bad if and only if it is not empty and gcd (see notes section for more information) of numbers in the list is 1.

    Arpa can perform two types of operations:

    • Choose a number and delete it with cost x.
    • Choose a number and increase it by 1 with cost y.

    Arpa can apply these operations to as many numbers as he wishes, and he is allowed to apply the second operation arbitrarily many times on the same number.

    Help Arpa to find the minimum possible cost to make the list good.

    Input

    First line contains three integers nx and y (1 ≤ n ≤ 5·105, 1 ≤ x, y ≤ 109) — the number of elements in the list and the integers xand y.

    Second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the elements of the list.

    Output

    Print a single integer: the minimum possible cost to make the list good.

    Examples
    input
    4 23 17
    1 17 17 16
    output
    40
    input
    10 6 2
    100 49 71 73 66 96 8 60 41 63
    output
    10
    Note

    In example, number 1 must be deleted (with cost 23) and number 16 must increased by 1 (with cost 17).

    gcd (greatest common divisor) of a set of numbers is the maximum integer that divides all integers in the set. Read more about gcd here.

    Solution

    题意:定义bad list是一个非空的、最大公约数为1的序列。给定一个序列,有两种操作:花费x将一个元素删除、花费y将一个元素加1,问你将这个序列变为good list所需要的最小花费是多少。

    思路:枚举gcd从2到1e6,然后将1...2e6分成个区间,对于处于某个区间(j*gcd-gcd,j*gcd)的所有数,可以算出一条分界线,(j*gcd-gcd,k)的数字删去的总费用更小,[k,j*gcd)的数字一直加到j*gcd的总费用更小(为什么是2e6而不是1e6?考虑这样一个例子:n = 2,x = 100, y = 1, a[] = {5000001, 1000000})。维护一下前缀和以快速得到某个范围内的数字个数与数字和就可以O(1)得到某个区间的答案。由1+1/2+1/3+1/4+1/5+...≈ln(n)可以得出整个过程总的复杂度为O(nlogn)。

    btw,其实枚举gcd时不用枚举[2,1e6]的所有数,只需枚举其中的质数即可,这样时间复杂度可以进一步减小。

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 typedef long long ll;
     5 ll n,x,y,a,cnt[2000111],sum[2000111],ans = 1e18;
     6 
     7 int main(){
     8     ios::sync_with_stdio(false);
     9     cin >> n >> x >> y;
    10     for(int i = 1;i <= n;++i){
    11         cin >> a;
    12         cnt[a]++;
    13         sum[a] += a;
    14     }
    15     for(int i = 1;i <= 2000000;++i)
    16         cnt[i] += cnt[i-1], sum[i] += sum[i-1];
    17 
    18     for(int i = 2;i <= 1000000;++i){
    19         ll res = 0;
    20         for(int j = i;j <= 2000000;j += i){
    21                 int k = max(j-x/y,j-i+1LL);
    22                 res += x * (cnt[k-1]-cnt[j-i]);
    23                 res += y * ((cnt[j-1]-cnt[k-1]) * j - (sum[j-1]-sum[k-1]));
    24             }
    25         ans = min(ans,res);
    26     }
    27 
    28     cout << ans;
    29 
    30     return 0;
    31 }
  • 相关阅读:
    js 图片压缩
    C#.NET中对称和非对称加密、解密方法汇总--亲测可用
    最精美详尽的 HTTPS 原理图
    swagger ui
    python爬取百度谷歌搜索结果
    Gitlab 11.0.3 迁移简明笔记
    ELK stack 7.6.x + kafka构建日志平台
    Nginx 内存占用高排查
    运维机器人 hubot 集成 jenkins
    运维机器人hubot,解放运维的神器
  • 原文地址:https://www.cnblogs.com/doub7e/p/7480030.html
Copyright © 2011-2022 走看看