题意:给你n个数,一次操作可以选一个数delete,代价为x;或者选一个数+1,代价y。你可以进行这两种操作任意次,让你在最小的代价下,使得所有数的GCD不为1(如果全删光也视作合法)。

我们从1到max(ai)枚举最后都变成的gcd是多少,假设为g,那么所有数都必须变成一个比g大的最小的g的倍数k·g。枚举k,然后在一个区间[(k-1)*g+1,k*g]里,一定存在一个分界点f,使得小于等于f的数全都删去,因为删除的代价小于把它们都变成kg的代价;大于f的数全都变成kg。因为x<=(kg-ai)*y,所以显然这个分界点是kg-ceil(x/y)。不过分界点不一定落在区间里,要讨论一下。

要预处理前缀和cnt(i)表示小于等于i的数的个数,sum(i)表示小于等于i的数的和。

特殊情况:一开始全是1。

  1. #include<cstdio>
  2. #include<cmath>
  3. #include<algorithm>
  4. using namespace std;
  5. typedef long long ll;
  6. int cnt[2000005],n,x,y,a[500005],N;
  7. ll sum[2000005],ans=9000000000000000000ll,nowans;
  8. int main(){
  9. //freopen("a.in","r",stdin);
  10. scanf("%d%d%d",&n,&x,&y);
  11. for(int i=1;i<=n;++i){
  12. scanf("%d",&a[i]);
  13. ++cnt[a[i]];
  14. sum[a[i]]+=(ll)a[i];
  15. }
  16. N=*max_element(a+1,a+n+1);
  17. if(N==1){
  18. printf("%I64d\n",(ll)n*(ll)min(x,y));
  19. return 0;
  20. }
  21. for(int i=2;i<=N*2;++i){
  22. cnt[i]+=cnt[i-1];
  23. sum[i]+=sum[i-1];
  24. }
  25. for(int i=2;i<=N;++i){
  26. nowans=0;
  27. for(int j=i;j<=N+i;j+=i){
  28. int R=j;
  29. int L=R-i+1;
  30. int upd=R-(int)(ceil((double)x/(double)y)+0.5);
  31. if(upd>=L){
  32. nowans+=(ll)x*(ll)(cnt[upd]-cnt[L-1]);
  33. }
  34. else{
  35. upd=L-1;
  36. }
  37. nowans+=(ll)y*((ll)(cnt[R]-cnt[upd])*(ll)R-(sum[R]-sum[upd]));
  38. }
  39. ans=min(ans,nowans);
  40. }
  41. printf("%I64d\n",ans);
  42. return 0;
  43. }

【前缀和】【枚举倍数】 Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017) D. Arpa and a list of numbers的更多相关文章

  1. Codeforces Round #432 (Div. 1, based on IndiaHacks Final Round 2017) D. Tournament Construction(dp + 构造)

    题意 一个竞赛图的度数集合是由该竞赛图中每个点的出度所构成的集合. 现给定一个 \(m\) 个元素的集合,第 \(i\) 个元素是 \(a_i\) .(此处集合已经去重) 判断其是否是一个竞赛图的度数 ...

  2. D. Arpa and a list of numbers Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017)

    http://codeforces.com/contest/851/problem/D 分区间操作 #include <cstdio> #include <cstdlib> # ...

  3. 【推导】【暴力】Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017) C. Five Dimensional Points

    题意:给你五维空间内n个点,问你有多少个点不是坏点. 坏点定义:如果对于某个点A,存在点B,C,使得角BAC为锐角,那么A是坏点. 结论:如果n维空间内已经存在2*n+1个点,那么再往里面添加任意多个 ...

  4. Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017)ABCD

    A. Arpa and a research in Mexican wave time limit per test 1 second memory limit per test 256 megaby ...

  5. 【推导】Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017) B. Arpa and an exam about geometry

    题意:给你平面上3个不同的点A,B,C,问你能否通过找到一个旋转中心,使得平面绕该点旋转任意角度后,A到原先B的位置,B到原先C的位置. 只要A,B,C构成等腰三角形,且B为上顶点.那么其外接圆圆心即 ...

  6. Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017) D

    Arpa has found a list containing n numbers. He calls a list bad if and only if it is not empty and g ...

  7. Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017) C

    You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two poi ...

  8. Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017) B

    Arpa is taking a geometry exam. Here is the last problem of the exam. You are given three points a,  ...

  9. Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017) A

    Arpa is researching the Mexican wave. There are n spectators in the stadium, labeled from 1 to n. Th ...

随机推荐

  1. Exponial (欧拉定理+指数循环定理+欧拉函数+快速幂)

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2021 Description Everybody loves big numbers ...

  2. 【Android XML】Android XML 转 Java Code 系列之 Selector(2)

    今天我们要把drawable下的selector的XML文件转换成Java代码.(打包进jar,不依赖apk) 在转换工具中的代码为: https://github.com/SickWorm/Andr ...

  3. python基础===Sublime Text 3 快捷键

    选择类 Ctrl+D 选中光标所占的文本,继续操作则会选中下一个相同的文本. Alt+F3 选中文本按下快捷键,即可一次性选择全部的相同文本进行同时编辑.举个栗子:快速选中并更改所有相同的变量名.函数 ...

  4. (二十)ubuntu的recovery mode解决用户一些实际问题

    遇到的问题如下: 1.在当前用户下使用sudo来直接修改password等几个文件,一旦修改了passwd,用户名发生了变化,其他的用户组.密码等却没有对应的配置,就再进不了该用户了. 2.忘记用户密 ...

  5. gradle问题总结与理解(一篇文章带你理解android studio 与gradle 的关系)

    前言:近日在网上找了个很不错的安卓二维码美化,由于下载的项目经常出问题,且不方便依赖使用,因此我想把它写个demo,并把源码发布到jcenter中,修改还是很顺利的,运行项目到手机也没问题,发布遇到了 ...

  6. Leetcode 之Binary Tree Postorder Traversal(44)

    后序遍历,比先序和中序都要复杂.访问一个结点前,需要先判断其右孩子是否被访问过.如果是,则可以访问该结点:否则,需要先处理右子树. vector<int> postorderTravers ...

  7. PHP设计模式二-------单例模式

    1.单例模式的介绍 意图:保证一个类仅有一个实例,并提供一个访问它的全局访问点: 主要解决:一个全局使用的类频繁地创建与销毁. 关键代码:构造函数是私有的,克隆方法也是私有的. 1.1 懒汉式//1 ...

  8. 关于时间日期的一些操作--java

    # 原创,转载请留言联系 1.获取当前时间 public static void main(String[] args) { Date d1 = new Date(); System.out.prin ...

  9. hrbust - 2239

    影子模仿术 Time Limit: 500 MS Memory Limit: 32768 K Total Submit: 7(5 users) Total Accepted: 2(2 users) R ...

  10. Decode Ways——动态规划

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...