CF850B Arpa and a list of numbers【思路】
来源:互联网 发布:生日祝福网页源码 编辑:程序博客网 时间:2024/05/19 16:32
题意:有一串数,我们用两个操作让它们的gcd不为1,1.直接删除,花费x,2.给它加一,花费y
思路:每个数都能分解成负数,我们枚举素数。关于操作的贪心:x小于等于y的话,直接删除;否则,计算x/y,可以加这么多个1,还不能满足条件的话,不如删除。
对于一个素数,有许多这样的区间[p,2p],我们考虑哪些数直接删除划算,哪些数+1到2p划算。分界点就是2p-x/y。用have[n] 记录前缀1~n中有几个数,sum[n] 记录have[n]中的这些数的和。
直接删除的数:区间起点 到 分界点前的那个点 中有几个数
加1的数:每个点都要加到2p,减去它们本来的值
#include<stdio.h>#include<iostream>#include<string.h>#include<string>#include<stdlib.h>#include<math.h>#include<vector>#include<list>#include<map>#include<stack>#include<queue>#include<algorithm>#include<numeric>#include<functional>using namespace std;typedef long long ll;typedef pair<int,int> pii;const int maxn = 1e6+5;ll have[maxn],sum[maxn];int vis[maxn]; int main(void){int n,x,y,s;while(scanf("%d%d%d",&n,&x,&y)!=EOF){memset(vis,0,sizeof vis);memset(have,0,sizeof have);memset(sum,0,sizeof sum);for(int i = 1; i <= n; i++){int a;scanf("%d",&a);have[a]++;sum[a]+=a;}for(int i = 2; i <= 1000000; i++){have[i] += have[i-1];sum[i] += sum[i-1];}s = x/y;ll ans = 0x3f3f3f3f3f3f3f3f; for(int i = 2; i <= 1000000; i++){if(vis[i]) continue;ll temp = 0;int j;for(j = i; j <= 1000000; j += i){vis[j] = 1;int p = max(j-i+1,j-s);temp += (have[p-1] - have[j-i]) * x;temp += ( (have[j] - have[p-1]) * j - (sum[j] - sum[p-1])) * y;}//以下3行,可以不特殊处理,只要把以上每个for循环的1000000 改成 2000000int p = max(j-i+1,j-s);temp += (have[min(p-1,1000000)] - have[j-i]) * x;temp += ( (have[1000000] - have[min(p-1,1000000)]) * j - (sum[1000000] - sum[min(p-1,1000000)])) * y;ans = min(ans,temp);}printf("%lld\n",ans);}return 0;}
阅读全文
0 0
- CF850B Arpa and a list of numbers【思路】
- CF850B-Arpa and a list of numbers
- CF_850B Arpa and a list of numbers
- codeforces 850B Arpa and a list of numbers
- Codeforces 851D. Arpa and a list of numbers
- Codeforces851D Arpa and a list of numbers(素数筛)
- Codeforces Round #432 B. Arpa and a list of numbers
- Codeforces Round #432 (Div. 2) D. Arpa and a list of numbers E. Arpa and a game with Mojtaba
- codeforces 851 D. Arpa and a list of numbers(前缀和+bruteforce)
- 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 Codeforces Round #432 (Div. 2 D ) Arpa and a list of numbers
- Codeforces 851D Arpa and a list of numbers【思维+前缀和】
- Codeforces Round #432 (Div. 1): B. Arpa and a list of numbers
- Codeforces 432 Div. 2-D-Arpa and a list of numbers(枚举倍数求GCD)
- Codeforces 851 D. Arpa and a list of numbers(技巧)
- Codeforces Round #432 (Div. 2) D 850B Arpa and a list of numbers(gcd 枚举)
- Codeforces 851D Arpa and a list of numbers Round #432 (Div. 2
- 【codeforces 750A】New Year and Hurry
- 2.理解merge和pushStack可以更好的了解jQuery原码
- druid 元数据接口查询
- 2018年9月份华为面试
- 电子邮件收发原理和JavaMail开发
- CF850B Arpa and a list of numbers【思路】
- java复习1 基本数据类型 常量变量命名 运算优先级
- 系统启动流程(六)
- 程序员的八重境界
- 最近有点爆炸
- 【1】TFTP软件的开发
- Event loop的理解
- 在Intellij IDEA中使用Debug
- Lua 的简介