• Codeforces 851D Arpa and a list of numbers


    D. Arpa and a list of numbers
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 n, x and y (1 ≤ n ≤ 5·105, 1 ≤ x, y ≤ 109) — the number of elements in the list and the integers x and 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).

    A 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.

    【题目大意】

    如果对于一个只包含数字列表,这个列表不为空且其gcd
    为1,那么这个列表就为坏的。现在我们有n 个数字组成
    的列表,你可以对这个列表进行俩种操作:
    ▶ 删除一个数字,并且需要花费x。
    ▶ 把其中一个数字加1,并且花费y。
    现在问你最少需要多少花费可以让这个列表变得不坏?

    【题解】

    ▶ 不如枚举d,表示把这些数字的gcd 变成d。
    ▶ 所以考虑kd 和kd + d 这区间的数字
    ▶ 1) 我们把[kd + d - ⌊x/y⌋; kd + d] 中间的数字加到
    kd + d 比较优
    ▶ 2) [kd; kd + d - ⌊x/y⌋] 中间的数字删除比较优
    ▶ 对于1),我们需要计算这些数字距离kd + d 有“多远”

    不如计算这些数字的和,假如有m 个数字,用
    m(kd + d) 减去这些数字和即可!
    ▶ 对于2),统计这些区间有多少个数字!
    ▶ 这些都是离线的,所以可以用差分(前缀和)来统计。
    ▶ 复杂度是O(n log n),因为
    O(n + n/2 + n/3 + n/4 + ::: + n/n) = O(n log n)

    (调和级数近似解)

    ——娄晨耀

    另外这个题还有各种各样的细节

    比如ma * 100W来让质数变大一点,为了防止TLE我们只需

    要算一个>最大值的质数即可

    于是各种细节麻烦的要死,。。。。

    还有爆过程量。。。需要判一下。。。

    交了20边才过。。。

  • 相关阅读:
    顺序查找
    折半查找
    KMP
    php长时间的脚本,报502
    AcWing 27. 数值的整数次方
    acwing 25. 剪绳子
    Best Cow Line <挑战程序设计竞赛> 习题 poj 3617
    acwing 23. 矩阵中的路径
    AcWing 34. 链表中环的入口结点
    AcWing 33. 链表中倒数第k个节点
  • 原文地址:https://www.cnblogs.com/huibixiaoxing/p/7647960.html
Copyright © 2020-2023  润新知