Codeforces 851 D. Arpa and a list of numbers(技巧)

网友投稿 586 2022-08-26

Codeforces 851 D. Arpa and a list of numbers(技巧)

Codeforces 851 D. Arpa and a list of numbers(技巧)

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 n, x and y (1 ≤ n ≤ 5·10^5, 1 ≤ x, y ≤ 10^9) — the number of elements in the list and the integers x and y.Second line contains n integers a1, a2, …, an (1 ≤ ai ≤ 10^6) — the elements of the list.

Output

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

Examples input

4 23 171 17 17 16

Examples output

40

题意

有一个序列,我们有两种操作:

删除某个数,代价为 x将某个数的值加一,代价为 y

现在我们想让这个序列所有数的 gcd 大于 1

思路

要让序列所有数的 gcd 大于 1

对于每一个素因子 i ,计算当前情况下所需要的最小代价,假设有一个数为 x ,且 i 不是 x 的因子,当 1×x>(i−x%i)×y 时显然我们可以选择将 x 加到 i

找最小值即可。

不过这里我们还需要做点剪枝,当不含有 i 这个因子的数的个数乘以 min(x,y)

显然, min(x,y) 中的 x 相当于删除所有数,而 y 相当于我们对序列中的每一个数执行最多一次操作让其变成偶数(或者特殊的奇数),此时的 gcd 至少为 2

AC 代码

#includeusing namespace std;const int maxn = 1e6 + 10;typedef __int64 LL;#define INF __INT64_MAX__LL n,x,y;LL sum[maxn];LL a[maxn];bool vis[maxn];int main(){ cin>>n>>x>>y; for(int i=1; i<=n; i++) { cin>>a[i]; sum[a[i]]++; } LL ans = INF; for(int i=2; iy*(i-a[j]%i))?y*(i-a[j]%i):x; ans = min(ans,val); } } } cout<

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Codeforces 849 C. From Y to Y (技巧)
下一篇:多态的成员特点(多态的两种类别及各自的特征)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~