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

解决一个算法问题,通过操作使数列的GCD不为1,找到达到目标所需的最小成本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


题意:

给你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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值