Codeforces #432 Div2 D
#432 Div2 D
题意
给出一些数字,如果这些数字的的 \(gcd\) 不为1则称这些数字 \(good\)。
可以有两种操作:
- 花费 x 删掉一个数
- 花费 y 将一个数加 1
问使这些数 \(good\) 的最小花费。
分析
一直找不到这题的重点。其实仔细想想与 \(gcd\) 有关,或者说与一个数列所有数的 \(gcd\) 有关,应该考虑到枚举所有的因子(或素因子),枚举因子对于求一系列数的 \(gcd\) 时貌似是很常见的。
对于本题,考虑枚举所有的素因子,设某个素因子为 \(p\),对于 \((j, j + p]\) \((j=k*p,k \in \mathbb{Z},k \geq 0)\) 这个区间,考虑这个区间的哪些数应该被删掉,哪些数应该增加至 \(j+p\)。
设 \(d\) 表示区间内某数与区间右端点的差值,如果 \(d*y>x\) ,那么应该删去。
有 \(d=\left \lceil \frac{x}{y} \right \rceil\),那么区间 \((j, j + p - d]\)里的数应该被删去,\((j+p-d, j + p]\)里的数增加至 \(j+p\)。
可以通过预处理出的前缀和以及数字出现次数的前缀和来高效的计算出答案。
code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e6 + 10;
int a[MAXN];
ll s[MAXN], sum[MAXN];
int notprime[MAXN];
vector<int> prime;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
for(int i = 2; i < MAXN; i++) if(!notprime[i]) {
prime.push_back(i);
for(ll j = 1LL * i * i; j < MAXN; j += i) notprime[j] = 1;
}
int n, x, y;
cin >> n >> x >> y;
for(int i = 0; i < n; i++) {
cin >> a[i];
s[a[i]]++;
}
for(int i = 1; i < MAXN; i++) {
sum[i] = sum[i - 1] + s[i] * i;
s[i] += s[i - 1];
}
ll ans = 1e18;
int d = ceil(1.0 * x / y);
for(int p : prime) {
ll res = 0;
for(int j = 0; j < MAXN; j += p) {
if(p <= d) {
res += ((s[min(MAXN - 1, j + p)] - s[j]) * (j + p) - (sum[min(MAXN - 1, j + p)] - sum[j])) * y;
} else {
res += (s[min(MAXN - 1, j + p - d)] - s[j]) * x;
if(j + p - d + 1 > MAXN) break;
res += ((s[min(MAXN - 1, j + p)] - s[j + p - d]) * (j + p) - (sum[min(MAXN - 1, j + p)] - sum[j + p - d])) * y;
}
}
ans = min(ans, res);
}
cout << ans << endl;
return 0;
}
Codeforces #432 Div2 D的更多相关文章
- Codeforces #180 div2 C Parity Game
// Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)
Problem Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)
Problem Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...
- 【Codeforces #312 div2 A】Lala Land and Apple Trees
# [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...
- codeforces round#432 div2
C:这道题没做出来...写了个类似极角排序的东西被卡掉了...事实上暴力就行了,因为如果在二维平面内那么最多只能有4个点,因为每个象限只能有一个点,然后这里拓展一下就是最多只能有2*k个点,k是维数, ...
- Codeforces #263 div2 解题报告
比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注冊的时候非常想好好的做一下,可是网上喝了个小酒之后.也就迷迷糊糊地看了题目,做了几题.一觉醒来发现r ...
- codeforces #round363 div2.C-Vacations (DP)
题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...
随机推荐
- 关于JS中array对象的push( )
push()的参数传的是指针,不是值. var arr = new Array(); var item = 5; arr.push(item); var item = 6; 运行以上代码,arr中的元 ...
- 洛谷 P3084 [USACO13OPEN]照片Photo 解题报告
[USACO13OPEN]照片Photo 题目描述 农夫约翰决定给站在一条线上的\(N(1 \le N \le 200,000)\)头奶牛制作一张全家福照片,\(N\)头奶牛编号\(1\)到\(N\) ...
- 运动目标前景检测之ViBe源代码分析
一方面为了学习,一方面按照老师和项目的要求接触到了前景提取的相关知识,具体的方法有很多,帧差.背景减除(GMM.CodeBook. SOBS. SACON. VIBE. W4.多帧平均……).光流(稀 ...
- [fzu 2282]置换不动点大于等于k的排列数
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2282 编号1~n的置换,不动点个数大于等于k的方案数. 参考百度百科错排公式,可以知道长度为n,每个数都不在自 ...
- 安卓中使用iconfont
https://www.cnblogs.com/dongweiq/p/5730212.html
- inflate
LayoutInflater是用 来找res/layout/下的xml布局文件,并且实例化 https://www.cnblogs.com/savagemorgan/p/3865831.html
- Codeforces Round #487 (Div. 2) A Mist of Florescence (暴力构造)
C. A Mist of Florescence time limit per test 1 second memory limit per test 256 megabytes input stan ...
- 7月17号day9总结
今天学习过程和小结 今天学习了如何使用idea操作hdfs. public class HDFSTest { Configuration configuration; FileSystem ...
- 使用vue开发webApp,安卓手机自带回退键的问题解决
首先,我先为大家说明,为什么我要写这篇随笔: 因为我们写的webapp,在安卓手机上,按一次回退键,就会退出app,回到桌面,而不是像原生app一样,会有一个提示,例如,“再按一次退出应用”的这种提示 ...
- bzoj 3100 排列
题目大意: 给你长度为 \(1e6\) 的序列, 求最大的 \(K\) 使得序列中含有一个 \(K\) 的排列 做法: 性质: 区间包含1, 元素不重, 区间最大值=区间长度 枚举一个 \(1\) 让 ...