Codeforces Round #432 B. Arpa and a list of numbers
来源:互联网 发布:风力太阳能路灯 知乎 编辑:程序博客网 时间:2024/05/18 23:55
Arpa has found a list containing n numbers. He calls a list bad if and only if it is not empty andgcd (see notes section for more information) of numbers in the list is1.
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 integersx 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 171 17 17 16
40
10 6 2100 49 71 73 66 96 8 60 41 63
10
In example, number 1 must be deleted (with cost 23) and number 16 must increased by 1 (with cost 17).
这个题我居然错了5次。全是1错了一次,于是加了个if;超时了一次,把cin换成了scanf并且在if里加了等号。
这题的意思是,坏序列:非空且最大公约数为1.我们可以对每个数执行两种操作,加1或者删除。可重复加1,每种操作分别花费a,b,现在在花费最小的情况下让当前数组不是坏序列。
这题没什么巧的。两重循环少不了。既然要让最大公约数不为1,那么我们就从2开始到最大可能值一个一个试吧。如果当前最大公约数是2,那么对每一个数选择最优处理,加起来得到一个代价。对不同最大公约数的最小代价即为所求。这里我们考虑一次剪枝,如果所有不合格的数花费的最小代价依然没有当前结果小,那么就不用计算了。
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;typedef long long LL;const LL maxn=1000086;LL n,a,b;LL val[maxn];LL flag[maxn];LL asd[maxn];int main(){ cin>>n>>a>>b; memset(val,0,sizeof(val)); memset(flag,0,sizeof(flag)); LL mi = __INT64_MAX__ ; LL ma = 0; for(LL i=0;i<n;i++) { scanf("%d",&asd[i]); flag[asd[i]]++; if(asd[i]<mi&&asd[i]!=1) mi=asd[i]; if(asd[i]>ma) ma=asd[i]; } if(ma==1) { cout<< min(a,b)*n <<endl; } else{ LL ans = __INT64_MAX__; for(LL i=2;i<=ma;i++) { if(val[i]) continue; LL tem = flag[i]; for(LL j = i+i;j<=ma;j+=i) { val[j]=1; tem+=flag[j]; } //这里考虑一下剪枝 if( (n-tem)*min(a,b) >= ans ) continue; LL ca=0; for(LL j=0;j<n;j++) if(asd[j]%i) ca+=(a>b*(i-asd[j]%i)) ? b*(i-asd[j]%i) : a; ans = min(ans,ca); } cout<<ans<<endl; } return 0;}
- Codeforces Round #432 B. Arpa and a list of numbers
- Codeforces Round #432 (Div. 1): B. Arpa and a list of numbers
- Codeforces Round #432 (Div. 2) D 850B Arpa and a list of numbers(gcd 枚举)
- Codeforces Round #432 (Div. 1) B. Arpa and a list of numbers
- codeforces 850B Arpa and a list of numbers
- 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 E. Arpa and a game with Mojtaba
- 解题报告:Codeforces Round #432 (Div. 2) D. Arpa and a list of numbers 暴力
- Codeforces 851D Arpa and a list of numbers Round #432 (Div. 2
- Codeforces Round #432 -思维&素数筛魔改-D-Arpa and a list of numbers
- codeforces 580B Arpa and a list of numbers 前缀和+思维+分块 (调和级数)
- Codeforces 851D. Arpa and a list of numbers
- Codeforces 432 Div. 2-D-Arpa and a list of numbers(枚举倍数求GCD)
- CF850B-Arpa and a list of numbers
- CF_850B Arpa and a list of numbers
- codeforces 851 D. Arpa and a list of numbers(前缀和+bruteforce)
- Codeforces 851D Arpa and a list of numbers【思维+前缀和】
- StringUtils的isBlank与isEmply
- Kotlin语言学习之AndroidStudio集成Kotlin开发环境
- ANR异常知识点
- Windows 远程桌面连接Ubuntu16.04图像界面
- 在linux系统上安装多个内核
- Codeforces Round #432 B. Arpa and a list of numbers
- Leetcode: 70. Climbing Stairs(Week1, Easy)
- GUI
- Lucene:基于Java的全文检索引擎简介
- C# List基本用法
- 高精度加法运算~
- 我漂亮有才,我爹还是丞相,为什么不能当太子妃?
- 使用foreach循环输出数组中的元素
- 要实现读写分离需要根据用户的操作连接不同的数据库