给出n个数,你可以对每个数把它变为0,或者增加1,分别需要花费x, y。问把所有数的GCD变为不为1的最小花费是多少。

n的范围5x1e5,a[i]的范围1e6。

开始想通过枚举最终gcd值,然后通过判左右个数以及消费来二分,显然是愚蠢的想法,因为一个数在不同模数下余数并不单调阿!

实际上是枚举gcd值,首先a[i]只有1e6范围,预处理前缀和:cnt[i]表示前a[] < i的个数和,sum[i] 比i小的所有a[]的和。

这样在枚举gcd的倍数值时,只要找到gcd范围内的一个划分,小于该划分的数的余数使用消去消费<增加该数到gcd的倍数的消费,那么只要计算gcd的所有倍数,就能得到该gcd作为最终因子的花费了。

/** @Date    : 2017-09-06 19:32:17
* @FileName: D.cpp
* @Platform: Windows
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version : $Id$
*/
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int ,int>
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e6+20;
const double eps = 1e-8; LL n, x, y;
LL sum[N*2], cnt[N*2];
int main()
{
while(cin >> n >> x >> y)
{
MMF(cnt);
MMF(sum);
for(int i = 0; i < n; i++)
{
LL t;
scanf("%lld", &t);
cnt[t]++;
sum[t] += t;
/*if(n <= 1)
{
printf("%d\n", t==1?min(x,y):0);
return 0;
}*/
} for(int i = 1; i < N*2; i++)
cnt[i] += cnt[i - 1], sum[i] += sum[i - 1]; LL ans = 1e16;
for(LL i = 2; i <= 1000000; i++)
{
LL t = 0;
for(LL j = i; j < 1000000 + i; j+=i)
{
LL ma = max(j - i + 1, j - x / y);
t += (cnt[ma - 1] - cnt[j - i]) * x;//直接消去
t += ((cnt[j] - cnt[ma - 1]) * j - (sum[j] - sum[ma - 1])) * y;
}
if(t < ans && t >= 0)
ans = t; }
printf("%lld\n", ans);
}
return 0;
}

最新文章

  1. 1Z0-053 争议题目解析212
  2. vue+sass 下sass不能运行问题
  3. DataRow映射实体
  4. 【前端】Node.js学习笔记
  5. PSP第九周
  6. &lt;&lt;Design Patterns&gt;&gt; Gang of Four
  7. centos的安装,网络的调试
  8. 【读书笔记】iOS-GCD-block-后台运行
  9. 集群之LVS(负载均衡)详解
  10. WebService之Axis2(5):会话(Session)管理
  11. IEF could not decode Chinese character in IE history well
  12. C++ 析构方法
  13. 基于BaseHTTPServer的简单存储服务器
  14. 洛谷 P1858 多人背包
  15. HDU4607 - Park Visit(树的直径)
  16. 【Windows 8】pid为4的system进程占用80端口的解决办法
  17. 在Mac OS 中显示和隐藏系统文件
  18. C-JAVA 论坛
  19. Java课程设计--GUI密码生成器201521123033
  20. MySQL协议学习(1):准备工作

热门文章

  1. 使用rand替换random模块
  2. (一)MySQL基础篇
  3. 将java开发的wordcount程序提交到spark集群上运行
  4. 如何在java中实现跨线程的通讯
  5. SQL优化套路
  6. Vue使用,异步获取日期时间后格式成&quot;/Date(1333245600000+0800)/&quot; 转换成正常格式
  7. POJ3709_K-Anonymous Sequence
  8. Java Servlet异步处理、非阻塞I/O和文件上传
  9. http://www.pythonchallenge.com/ 网站题解
  10. Django之CSS,JS静态文件的配置