codeforces 850B Arpa and a list of numbers

来源:互联网 发布:mac怎么关闭软件 编辑:程序博客网 时间:2024/05/19 02:03

B. 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 nx and y (1 ≤ n ≤ 5·1051 ≤ 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 171 17 17 16
output
40
input
10 6 2100 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.

题解:
这道题目还是很有意思的,感觉很有启发性。
我们的思路非常的简单,枚举素数p,并且认为该素数是最终序列的gcd的因数。
我们这样考虑,对于数列中的数x,如果它不被删掉,那么它就会被增加到最近一个p的倍数kp,耗费为(kp - x)*y,而如果它被删除的话,耗费为x。
如果我们把原序列按p的倍数划分成一个个区间的话,也就是
[0,p] [p+1,2p].....[kp+1,(k+1)p]
那么区间内的数距离右端点的差delta*y > x意味着应当把这个数删除,而该数字右边部分的数字则应当被增加到右端点的值。
这样的话我们需要记录前缀和以及前缀中数字出现的次数。
代码:


#include <bits/stdc++.h>using namespace std;typedef long long LL;const int MAXN = 2e6;const LL INF = 1e18;int a[MAXN];LL s[MAXN],used[MAXN];int main(){LL n,x,y;cin>>n>>x>>y;int p = x/y;for(int i = 1;i <= n;i++){int tmp;scanf("%d",&tmp); a[tmp]++;s[tmp] += (LL)tmp;}LL ans = INF;for(int i = 1;i < MAXN;i++)a[i] += a[i-1],s[i] += s[i-1];for(int i = 2;i <= 1000000;i++){if(used[i]) continue;LL tmp = 0;for(int j = i;j <= 1000000 + i;j += i){used[j] = 1;int k = max(j - i + 1,j - p );tmp +=((LL)(a[j] - a[k-1])*j - (s[j] - s[k-1]))*y + (LL)(a[k-1] - a[j-i])*x;}ans = min(ans,tmp);}cout<<ans<<endl;return 0;}/*45 11 17 17 17*/


阅读全文
1 0
原创粉丝点击