CF851 D 枚举 思维
2024-09-21 06:08:36
给出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;
}
最新文章
- 1Z0-053 争议题目解析212
- vue+sass 下sass不能运行问题
- DataRow映射实体
- 【前端】Node.js学习笔记
- PSP第九周
- <;<;Design Patterns>;>; Gang of Four
- centos的安装,网络的调试
- 【读书笔记】iOS-GCD-block-后台运行
- 集群之LVS(负载均衡)详解
- WebService之Axis2(5):会话(Session)管理
- IEF could not decode Chinese character in IE history well
- C++ 析构方法
- 基于BaseHTTPServer的简单存储服务器
- 洛谷 P1858 多人背包
- HDU4607 - Park Visit(树的直径)
- 【Windows 8】pid为4的system进程占用80端口的解决办法
- 在Mac OS 中显示和隐藏系统文件
- C-JAVA 论坛
- Java课程设计--GUI密码生成器201521123033
- MySQL协议学习(1):准备工作
热门文章
- 使用rand替换random模块
- (一)MySQL基础篇
- 将java开发的wordcount程序提交到spark集群上运行
- 如何在java中实现跨线程的通讯
- SQL优化套路
- Vue使用,异步获取日期时间后格式成";/Date(1333245600000+0800)/"; 转换成正常格式
- POJ3709_K-Anonymous Sequence
- Java Servlet异步处理、非阻塞I/O和文件上传
- http://www.pythonchallenge.com/ 网站题解
- Django之CSS,JS静态文件的配置