Codeforces 851 D Arpa and a list of numbers(前缀和)
来源:互联网 发布:matlab求协方差矩阵 编辑:程序博客网 时间:2024/05/20 07:35
题目地址
题意:你有2种操作,用最小的代价使得给定的序列为空或者gcd>1。
- 操作1:删除一个点的代价为x
- 操作2:让当前数+1的代价为y
思路:枚举每个数为最后gcd的数所需要的代价。因为有两种代价,如果这个数变成所需要成的数的代价大于x的话还不如删除。所以我们先计算出最多改变多少次,也就是最多可以比想要的数小cs。然后去算出代价就好了。算代价就用前缀和的思想,定义2个数组,一个记录比当前数小于等于的数有多少个,另一个记录比当前数小于等于的数总和为多少。 这样的话如果代价小于cs这些数就可以乘以y其他的就是乘以x。这样就是当前gcd的结果条件下最小的代价。
#include <iostream>#include <cstring>#include <string>#include <queue>#include <vector>#include <map>#include <set>#include <stack>#include <cmath>#include <cstdio>#include <algorithm>#include <iomanip>#define N 2000005#define M 4000010#define LL __int64#define inf 0x7f7f7f7f#define lson l,mid,ans<<1#define rson mid+1,r,ans<<1|1#define getMid (l+r)>>1#define movel ans<<1#define mover ans<<1|1using namespace std;const double PI = acos(-1.0);LL num[N], sum[N];int main() { cin.sync_with_stdio(false); LL n, m, x, y; while (cin >> n >> x >> y) { memset(num, 0, sizeof(num)); memset(sum, 0, sizeof(sum)); for (int i = 0; i < n; i++) { cin >> m; num[m]++; sum[m] += m; } for (LL i = 1; i < N; i++) { num[i] += num[i - 1]; sum[i] += sum[i - 1]; } LL cs = (x + y - 1) / y; LL ans = n*x; for (LL i = 2; i < N; i++) {//枚举每个因子 LL cnt = 0; for (LL j = i; j < N&&cnt < ans; j += i) { LL k = max(j - i, j - cs); cnt += ((num[j] - num[k])*j - (sum[j] - sum[k]))*y + (num[k] - num[j - i])*x; } ans = min(ans, cnt); } cout << ans << endl; } return 0;}
阅读全文
0 0
- Codeforces 851 D Arpa and a list of numbers(前缀和)
- codeforces 851 D. Arpa and a list of numbers(前缀和+bruteforce)
- Codeforces 851D Arpa and a list of numbers【思维+前缀和】
- Codeforces 851D. Arpa and a list of numbers
- codeforces 580B Arpa and a list of numbers 前缀和+思维+分块 (调和级数)
- Codeforces 851 D. Arpa and a list of numbers(技巧)
- Codeforces 851D Arpa and a list of numbers (枚举+分段)
- Codeforces 851D Arpa and a list of numbers Round #432 (Div. 2
- Codeforces 432 Div. 2-D-Arpa and a list of numbers(枚举倍数求GCD)
- Codeforces Round #432 (Div. 2) D. Arpa and a list of numbers E. Arpa and a game with Mojtaba
- Codeforces Codeforces Round #432 (Div. 2 D ) Arpa and a list of numbers 爆搜+剪枝
- Codeforces Codeforces Round #432 (Div. 2 D ) Arpa and a list of numbers
- 解题报告:Codeforces Round #432 (Div. 2) D. Arpa and a list of numbers 暴力
- Codeforces Round #432 (Div. 2) D 850B Arpa and a list of numbers(gcd 枚举)
- Codeforces Round #432 -思维&素数筛魔改-D-Arpa and a list of numbers
- codeforces 850B Arpa and a list of numbers
- Codeforces Round #432 B. Arpa and a list of numbers
- CF850B-Arpa and a list of numbers
- 第一章 Java Web工作原理
- 一直青蛙可以跳上一级台阶,也可以跳上两级台阶,求一个青蛙跳上N级台阶总共有多少种跳法
- 代码大全2 第7部分 软件工艺 mindmap
- Centos下安装RabbitMQ
- java小白需知
- Codeforces 851 D Arpa and a list of numbers(前缀和)
- Zookeeper客户端ZkClient
- 线程知识总结
- jeect 上传文件
- 将一篇文章转化为语音朗读的实践
- POJ 2396 Budget 有上下界的可行网络流
- eclipse快捷键 包括查找类、方法、变量
- RxJava
- codeforces 850C Arpa and a game with Mojtaba(博弈,bitmask)