#432 Div2 D

题意

给出一些数字,如果这些数字的的 \(gcd\) 不为1则称这些数字 \(good\)。

可以有两种操作:

  1. 花费 x 删掉一个数
  2. 花费 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的更多相关文章

  1. Codeforces #180 div2 C Parity Game

    // Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...

  2. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  3. Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)

    Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...

  4. Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

  5. Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)

    Problem   Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...

  6. 【Codeforces #312 div2 A】Lala Land and Apple Trees

    # [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...

  7. codeforces round#432 div2

    C:这道题没做出来...写了个类似极角排序的东西被卡掉了...事实上暴力就行了,因为如果在二维平面内那么最多只能有4个点,因为每个象限只能有一个点,然后这里拓展一下就是最多只能有2*k个点,k是维数, ...

  8. Codeforces #263 div2 解题报告

    比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注冊的时候非常想好好的做一下,可是网上喝了个小酒之后.也就迷迷糊糊地看了题目,做了几题.一觉醒来发现r ...

  9. codeforces #round363 div2.C-Vacations (DP)

    题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...

随机推荐

  1. 左侧导航条+中间显示内容+右侧菜单配置,Bootstrap+AdminLTE+Jquery

    1.最近做个导航页面,找了一大堆UI,最终选了AdminLTE,这个UI也是以bootstrap为基础,简单实用,中间内容用jquery的load加载,简单暴力,非常适合快速开发. 2.效果图如下: ...

  2. win8.1 host被删,host无法修改,host无法复制进去解决方案

    1.C:\Windows\System32\drivers\etc\hosts  复制到桌面 2.删除C:\Windows\System32\drivers\etc\hosts 3.右键编辑文本--& ...

  3. [NOI2010]能量采集 解题报告

    [NOI2010]能量采集 题目描述 栋栋有一块长方形的地,他在地上种了一种能量植物,这种植物可以采集太阳光的能量.在这些植物采集能量后,栋栋再使用一个能量汇集机器把这些植物采集到的能量汇集到一起. ...

  4. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) D

    D. Bear and Two Paths time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  5. codeforces 792CDivide by Three(两种方法:模拟、动态规划

    传送门:https://codeforces.com/problemset/problem/792/C 题意:给你一个字符串,要求让你删除最少个数的元素,使得最终答案是没有前导0并且是3的倍数. 题解 ...

  6. Kafka配置文档

    http://kafka.apache.org/08/configuration.html

  7. 在Maven中怎么配置外部Jar

    转摘自:http://liugang594.iteye.com/blog/1677712 假设我们有一个Maven的project,其中有些Jar包不是来自Maven库的,是存在本地的某些Jar文件, ...

  8. 【Foreign】异色弧 [树状数组]

    异色弧 Time Limit: 20 Sec  Memory Limit: 256 MB Description Input Output 仅一行一个整数表示答案. Sample Input 8 1 ...

  9. 百练3383:Cell Phone Network

    传送门:http://bailian.openjudge.cn/practice/3383/ [题解] 题目就是最小支配集. 学习了最小支配集的解法: 树形dp(有空可以推一推) 贪心:DFS遍历后逆 ...

  10. CentOS 7 主机加固手册-下

      CentOS 7 主机加固手册-上 CentOS 7 主机加固手册-中 CentOS 7 主机加固手册-下 0x1f 删除禁用非必要的服务 删除非必要的服务 # Remove yum remove ...