D. Arpa and a list of numbers
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 n, x and y (1 ≤ n ≤ 5·105, 1 ≤ 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
Note

In example, number 1 must be deleted (with cost 23) and number 16 must increased by 1 (with cost 17).

A gcd (greatest common divisor) of a set of numbers is the maximum integer that divides all integers in the set. Read more about gcd here.

【题目大意】

如果对于一个只包含数字列表,这个列表不为空且其gcd
为1,那么这个列表就为坏的。现在我们有n 个数字组成
的列表,你可以对这个列表进行俩种操作:
▶ 删除一个数字,并且需要花费x。
▶ 把其中一个数字加1,并且花费y。
现在问你最少需要多少花费可以让这个列表变得不坏?

【题解】

▶ 不如枚举d,表示把这些数字的gcd 变成d。
▶ 所以考虑kd 和kd + d 这区间的数字
▶ 1) 我们把[kd + d - ⌊x/y⌋; kd + d] 中间的数字加到
kd + d 比较优
▶ 2) [kd; kd + d - ⌊x/y⌋] 中间的数字删除比较优
▶ 对于1),我们需要计算这些数字距离kd + d 有“多远”

不如计算这些数字的和,假如有m 个数字,用
m(kd + d) 减去这些数字和即可!
▶ 对于2),统计这些区间有多少个数字!
▶ 这些都是离线的,所以可以用差分(前缀和)来统计。
▶ 复杂度是O(n log n),因为
O(n + n/2 + n/3 + n/4 + ::: + n/n) = O(n log n)

(调和级数近似解)

——娄晨耀

另外这个题还有各种各样的细节

比如ma * 100W来让质数变大一点,为了防止TLE我们只需

要算一个>最大值的质数即可

于是各种细节麻烦的要死,。。。。

还有爆过程量。。。需要判一下。。。

交了20边才过。。。

Codeforces 851D Arpa and a list of numbers的更多相关文章

  1. 【Codeforces 851D Arpa and a list of numbers】

    Arpa的数列要根据GCD变成好数列. ·英文题,述大意:      给出一个长度为n(n<=5000000)的序列,其中的元素a[i]<=106,然后输入两个数x,y(x,y<=1 ...

  2. Codeforces Codeforces Round #432 (Div. 2 D ) Arpa and a list of numbers

    D. Arpa and a list of numbers time limit per test   2 seconds memory limit per test     256 megabyte ...

  3. codeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(启发式合并)

    codeforces 741D Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...

  4. 构造 Codeforces Round #107 (Div. 2) B. Phone Numbers

    题目传送门 /* 构造:结构体排个序,写的有些啰嗦,主要想用用流,少些了判断条件WA好几次:( */ #include <cstdio> #include <algorithm> ...

  5. 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> # ...

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

    qtmd的复习pat,老子不想看了,还不如练几道cf 这题首先可以很容易想到讨论最后的共因子为素数 这个素数太多了,1-1e6之间的素数 复杂度爆炸 所以使用了前缀和,对于每个素数k的每个小区间 (k ...

  7. 【前缀和】【枚举倍数】 Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017) D. Arpa and a list of numbers

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

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

    [链接]h在这里写链接 [题意] 定义bad list是一个非空的.最大公约数为1的序列.给定一个序列,有两种操作:花费x将一个元素删除.花费y将一个元素加1,问你将这个序列变为good list所需 ...

  9. Codeforces Round #181 (Div. 2) C. Beautiful Numbers 排列组合 暴力

    C. Beautiful Numbers 题目连接: http://www.codeforces.com/contest/300/problem/C Description Vitaly is a v ...

随机推荐

  1. JavaScript中数组的基础知识和相关方法

      数组基础 数组是大多数语言里面最常见的一种数据结构,它是一个有序的值列表. 创建数组 1.创建字面量数组 let arr=[]; 2.创建构造函数数组 let arr=new Array(); 注 ...

  2. Android基础控件Button的使用

    1.相关属性 Android的按钮有Button和ImageButton(图像按钮),Button extends TextView, ImageButton extends ImageView! a ...

  3. <el-tag></el-tag>部分属性与vue版本的兼容问题

    [01]标签使用按钮样式<el-tag effect="dark" v-if="myhotelinfo.runstatus=='T'" type=&quo ...

  4. 看了这么N多天DELPHI,突然我有个感觉

    感觉DELPHI很像Win32汇编   语法上不谈,就编写格式.形式上,很像 delphi的一个函数   procedure TForm1.btn1Click(Sender: TObject);beg ...

  5. axios接口封装

    axios封装 import JsonP from 'jsonp' import axios from 'axios' import { Modal } from 'antd' export defa ...

  6. 深入浅出 Java Concurrency (13): 锁机制 part 8 读写锁 (ReentrantReadWriteLock) (1)[转]

    从这一节开始介绍锁里面的最后一个工具:读写锁(ReadWriteLock). ReentrantLock 实现了标准的互斥操作,也就是一次只能有一个线程持有锁,也即所谓独占锁的概念.前面的章节中一直在 ...

  7. 匿名/局部内部类访问局部变量时,为什么局部变量必须加final

    我们都知道方法中的匿名/局部内部类是能够访问同一个方法中的局部变量的,但是为什么局部变量要加上一个final呢? 首先我们来研究一下变量生命周期的问题,局部变量的生命周期是当该方法被调用时在栈中被创建 ...

  8. 推荐大家一个个人觉得超级好的Java学习网站

    https://how2j.cn?p=16567 这是网址,网站里有Java基础,Javaweb.框架.数据库.工具.面试题等等的,站长一直在更新新的知识,很好用哦

  9. python intern(字符串驻留机制)

    python 中为了提高字符串的使用用效率和节约内存,对于由 字母.数字.下划线组成的标识符采用了 intern 机制,即对于短字符串,将其赋值给多个对象时,内存中只有一个副本,多个对象共享这个副本. ...

  10. angular报错:angular.min.js:118Error: [ng:areq] http://errors.angularjs.org/1.5.8/ng/areq

    报错代码如下: <div ng-controller="HelloAngular"> <p>{{greeting.text}},angular</p& ...