Codeforces Round #432 (Div. 1): B. Arpa and a list of numbers

来源:互联网 发布:php print echo 区别 编辑:程序博客网 时间:2024/05/21 09:32

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


题意:

给你n个数,你有两个操作:

①将某个数删掉,消耗为x

②将某个数+1,消耗为y,这个操作可以对某个数多次使用

而你的目的是让最后n个数的Gcd不为1,求最小消耗


题解:

对于所有数求前缀和,cnt[i]表示小于等于i的数有多少个,pri[i]表示小于等于i的数之和

暴力Gcd,然后枚举倍数,判断所有的数是删掉更划算还是不停加1更划算


#include<stdio.h>#include<algorithm>using namespace std;#define LL long longLL cnt[2000025], pre[2000025];int main(void){LL temp, x, y, ans;int n, i, c, t, d, st;scanf("%d%lld%lld", &n, &x, &y);for(i=1;i<=n;i++){scanf("%d", &t);cnt[t] ++;pre[t] += t;}ans = x*n;c = x/y;for(i=1;i<=2000005;i++){cnt[i] += cnt[i-1];pre[i] += pre[i-1];}for(d=2;d<=1000000;d++){temp = 0;for(i=d;i<2000005;i+=d){if(i-c>i-d+1)temp += (cnt[i-c-1]-cnt[i-d])*x;st = max(i-c, i-d+1);temp += ((cnt[i-1]-cnt[st-1])*i-(pre[i-1]-pre[st-1]))*y;}ans = min(ans, temp);}printf("%lld\n", ans);return 0;}

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