题意:

给出一个序列,两种操作:

1.删除一个数,代价为x

2.给一个数+1,代价为y

求最小代价,使这个序列不为空,且所有的数的gcd>1

n<=5e5,a[i]<=1e6

其实思路还是很简单的。

可以发现a[i]只有1e6,那么我们直接暴力枚举修改后的数列的gcd(为下文方便,我们假设挡当前枚举的数为i)。

那么对于一个不是i的倍数的数,它就是要么删,要么就是加到最接近它的i的倍数。

所以接下来就是暴力枚举i的倍数。

如果我们当前的倍数为j,那么易发现大小[ji-1,ji-x/y]的数,就是累加到ji,大小[(j-1)i+1,ji-x/y-1]的数就是删掉。

那这个代价怎么算?

用个桶+前缀和搞下就行了。

  1. #include<cstdio>
  2. #include<ctime>
  3. #include<iostream>
  4. #include<algorithm>
  5. #include<cmath>
  6. #include<string>
  7. #define rg register
  8. #define il inline
  9. #define vd void
  10. #define ll long long
  11. #define maxn 2000010
  12. #define N 500010
  13. #define For(i,x,y) for (rg int i=(x);i<=(y);i++)
  14. #define Dow(i,x,y) for (rg int i=(x);i>=(y);i--)
  15. #define cross(i,k) for (rg int i=first[k];i;i=last[i])
  16. using namespace std;
  17. il ll max(ll x,ll y){return x>y?x:y;}
  18. il ll min(ll x,ll y){return x<y?x:y;}
  19. il ll read(){
  20. ll x=;int ch=getchar(),f=;
  21. while (!isdigit(ch)&&(ch!='-')&&(ch!=EOF)) ch=getchar();
  22. if (ch=='-'){f=-;ch=getchar();}
  23. while (isdigit(ch)){x=(x<<)+(x<<)+ch-'';ch=getchar();}
  24. return x*f;
  25. }
  26. int n,Max,a[N];
  27. ll k,l,r,x,y,z,ans,Sum,cnt[maxn],sum[maxn];
  28. int main(){
  29. n=read(),x=read(),y=read(),z=x/y;
  30. For(i,,n) a[i]=read(),Max=max(Max,a[i]),cnt[a[i]]++;
  31. For(i,,Max*) sum[i]=sum[i-]+cnt[i]*i,cnt[i]+=cnt[i-];
  32. ans=1000000000000000ll;
  33. For(i,,max(Max,)){
  34. Sum=;
  35. if (Max%i==) k=Max/i;
  36. else k=Max/i+;
  37. if (i<=z)
  38. For(j,,k){
  39. l=(j-)*i,r=j*i;
  40. Sum+=(r*(cnt[r-]-cnt[l])-sum[r-]+sum[l])*y;
  41. }
  42. else
  43. For(j,,k){
  44. l=(j-)*i,r=j*i;
  45. Sum+=(cnt[r-z-]-cnt[l])*x+(r*(cnt[r-]-cnt[r-z-])-sum[r-]+sum[r-z-])*y;
  46. }
  47. ans=min(ans,Sum);
  48. }
  49. printf("%lld",ans);
  50. }

Codeforces 850B的更多相关文章

  1. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  2. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  3. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  4. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  5. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  6. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  7. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  8. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

  9. CodeForces - 148D Bag of mice

    http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...

随机推荐

  1. 第一章: 文件句柄转化为 typeglob/glob 与文件句柄检测

    #为了使在子例程中传递文件句柄不出问题 #我们要把文件句柄转为glob或typeglob #转为glob $fd = *MY_FILE; #转为typeblog $fd = \*MY_FILE; #两 ...

  2. mysql之基本数据库操作(二)

    环境信息 数据库:mysql-5.7.20 操作系统:Ubuntu-16.04.3 mysql的启动.退出.重启 # 启动 $ sudo service mysqld start # 停止 $ sud ...

  3. Linux实际用户(组)ID,有效用户(组)ID,设置用户(组)ID

    实际用户(组)ID: 标识用户是谁,这两个字段在登录时取自口令文件中的登录项. 有效用户(组)ID: 决定了对文件的访问权限,通常有效用户(组)ID等于实际用户(组)ID,谁运行有效ID就等于谁的实际 ...

  4. 自动化测试===requests+unittest+postman的接口测试

    postman是一个跨平台的接口测试工具,下载链接在这里:https://www.getpostman.com/ unittest是一个单元测试框架,python中安装:pip install uni ...

  5. (十八)Linux开机启动管理---systemd使用

    常用命令 使某服务自动启动 systemctl enable httpd.service 使某服务不自动启动 systemctl disable httpd.service 检查服务状态 system ...

  6. <摘录>Fedora添加国内源和本地源

    <摘录>Fedora添加国内源和本地源 http://www.cnblogs.com/hummersofdie/p/3915070.html fedora的软件源信息文件(*.repo)都 ...

  7. 集合类---set

    定义:一个不包含重复元素的collection.set 不包含满足 e1.equals(e2) 的元素对 e1 和 e2,并且最多包含一个 null 元素,不保证集合里元素的顺序. 方法使用详解: 1 ...

  8. codevs 1038 一元三次方程求解 NOIP2001提高组

    题目链接:http://codevs.cn/problem/1038/ 题解: 嗯,exm?才知道二分隶属搜索专题…… 对-100到100枚举,按照题目中的提示,当当fi*fi+1<0时,二分深 ...

  9. EOS.IO技术学习

    如今很火的项目EOS的学习,以下主要的内容是基于白皮书 参考: http://chainx.org/paper/index/index/id/20.html EOS.IO软件引入了一种新的块链架构,旨 ...

  10. JS面试题第一弹

    1.javascript的typeof返回哪些数据类型  alert(typeof [1, 2]); //object     alert(typeof 'leipeng'); //string   ...