Codeforces 851 D. Arpa and a list of numbers(技巧)
来源:互联网 发布:linux 开机卡在进度条 编辑:程序博客网 时间:2024/05/20 08:21
Description
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 n, x and y (1 ≤ n ≤ 5·10^5, 1 ≤ x, y ≤ 10^9) — the number of elements in the list and the integers x and y.
Second line contains n integers a1, a2, …, an (1 ≤ ai ≤ 10^6) — 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
Examples output
40
题意
有一个序列,我们有两种操作:
- 删除某个数,代价为
x 。 - 将某个数的值加一,代价为
y 。
现在我们想让这个序列所有数的
思路
要让序列所有数的
对于每一个素因子
找最小值即可。
不过这里我们还需要做点剪枝,当不含有
显然,
AC 代码
#include<bits/stdc++.h>using namespace std;const int maxn = 1e6 + 10;typedef __int64 LL;#define INF __INT64_MAX__LL n,x,y;LL sum[maxn];LL a[maxn];bool vis[maxn];int main(){ cin>>n>>x>>y; for(int i=1; i<=n; i++) { cin>>a[i]; sum[a[i]]++; } LL ans = INF; for(int i=2; i<maxn; i++) //枚举素数 { if(!vis[i]) //素数筛法 { LL cnt = sum[i]; for(int j=i+i; j<maxn; j+=i) { vis[j]=true; cnt+=sum[j]; } if((n-cnt)*min(x,y)<ans) //剪 { LL val = 0; for(int j=1; j<=n; j++) if(a[j]%i) val+=(x>y*(i-a[j]%i))?y*(i-a[j]%i):x; ans = min(ans,val); } } } cout<<ans<<endl; return 0;}
- Codeforces 851 D. Arpa and a list of numbers(技巧)
- Codeforces 851D. Arpa and a list of numbers
- Codeforces 851 D Arpa and a list of numbers(前缀和)
- Codeforces 851D Arpa and a list of numbers (枚举+分段)
- codeforces 851 D. Arpa and a list of numbers(前缀和+bruteforce)
- Codeforces 851D Arpa and a list of numbers【思维+前缀和】
- Codeforces 851D Arpa and a list of numbers Round #432 (Div. 2
- Codeforces 432 Div. 2-D-Arpa and a list of numbers(枚举倍数求GCD)
- Codeforces Round #432 (Div. 2) D. Arpa and a list of numbers E. Arpa and a game with Mojtaba
- Codeforces Codeforces Round #432 (Div. 2 D ) Arpa and a list of numbers 爆搜+剪枝
- Codeforces Codeforces Round #432 (Div. 2 D ) Arpa and a list of numbers
- 解题报告:Codeforces Round #432 (Div. 2) D. Arpa and a list of numbers 暴力
- Codeforces Round #432 (Div. 2) D 850B Arpa and a list of numbers(gcd 枚举)
- Codeforces Round #432 -思维&素数筛魔改-D-Arpa and a list of numbers
- codeforces 850B Arpa and a list of numbers
- Codeforces Round #432 B. Arpa and a list of numbers
- codeforces 580B Arpa and a list of numbers 前缀和+思维+分块 (调和级数)
- CF850B-Arpa and a list of numbers
- CentOS下安装jdk
- 解析最外层为数组的json串
- Spring BcryptPasswordEncorder Log Rounds参数说明
- SpringAop中的相关概念
- tensorflow:提示找不到Adam或者RMSProp变量,Did you mean to set reuse=None in VarScope?
- Codeforces 851 D. Arpa and a list of numbers(技巧)
- 3DSlicer25:Report an Error
- hdu 5773 策略+最长上升子序列
- ZOJ
- 什么是ajax?
- WebUploader 上传图片并提交表单(一)
- iText7---Adding low-level content添加低层级内容
- java每日一练
- 小悟:Object.assign() “深拷贝”