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 ...
随机推荐
- P1419 寻找段落
题目描述 给定一个长度为n的序列a_i,定义a[i]为第i个元素的价值.现在需要找出序列中最有价值的“段落”.段落的定义是长度在[S,T]之间的连续序列.最有价值段落是指平均值最大的段落, 段落的平均 ...
- Git 删除服务器的远程分支
git push origin :分支名 可能会出现,在A机子操作,刷新下成功删除,但在B机子上还显示,再用下命令提示不存在该分支,只要再推送一个任意分支即可正常显示
- [Leetcode] Palindrome number 判断回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- CF858F Wizard's Tour 解题报告
题目描述 给定一张 \(n\) 个点 \(m\) 条边的无向图,每条边连接两个顶点,保证无重边自环,不保证连通. 你想在这张图上进行若干次旅游,每次旅游可以任选一个点 \(x\) 作为起点,再走到一个 ...
- CF763C Timofey and Remoduling
题目戳这里. 这道题目纯粹是考思维. 若\(2N \le M\),由于答案肯定是\(s,s+d,\dots,s+(N-1)d\),我们任意枚举两个数\(a,b\),不妨设\(b\)在数列中出现在\(a ...
- Git命令文本手册
git init # 初始化本地git仓库(创建新仓库) git config --global user.name "xxx" # 配置用户名 git config --glob ...
- C# new override
A -> virtual Fun B : A -> override Fun C : B -> override Fun D : C -> new virtual Fun E ...
- CSS3学习之linear-gradient(线性渐变)
转自:http://www.cnblogs.com/rainman/p/5113242.html CSS3 渐变(gradients)可以让你在两个或多个指定的颜色之间显示平稳的过渡. 以前,你必须使 ...
- L3-003. 社交集群(并查集)
L3-003. 社交集群 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 在社交网络平台注册时,用户通常会输入自己的兴趣爱好, ...
- 河南省第十届省赛 Plumbing the depth of lake (模拟)
title: Plumbing the depth of lake 河南省第十届省赛 题目描述: There is a mysterious lake in the north of Tibet. A ...