codeforces 850B Arpa and a list of numbers

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

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;
}
/*
4
5 1
1 17 17 17
*/


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值