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.
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.
Print a single integer: the minimum possible cost to make the list good.
4 23 17 1 17 17 16
40
10 6 2 100 49 71 73 66 96 8 60 41 63
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 long
LL 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;
}