Codeforces 850B
题意:
给出一个序列,两种操作:
1.删除一个数,代价为x
2.给一个数+1,代价为y
求最小代价,使这个序列不为空,且所有的数的gcd>1
n<=5e5,a[i]<=1e6
其实思路还是很简单的。
可以发现a[i]只有1e6,那么我们直接暴力枚举修改后的数列的gcd(为下文方便,我们假设挡当前枚举的数为i)。
那么对于一个不是i的倍数的数,它就是要么删,要么就是加到最接近它的i的倍数。
所以接下来就是暴力枚举i的倍数。
如果我们当前的倍数为j,那么易发现大小[ji-1,ji-x/y]的数,就是累加到ji,大小[(j-1)i+1,ji-x/y-1]的数就是删掉。
那这个代价怎么算?
用个桶+前缀和搞下就行了。
#include<cstdio>
#include<ctime>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
#define rg register
#define il inline
#define vd void
#define ll long long
#define maxn 2000010
#define N 500010
#define For(i,x,y) for (rg int i=(x);i<=(y);i++)
#define Dow(i,x,y) for (rg int i=(x);i>=(y);i--)
#define cross(i,k) for (rg int i=first[k];i;i=last[i])
using namespace std;
il ll max(ll x,ll y){return x>y?x:y;}
il ll min(ll x,ll y){return x<y?x:y;}
il ll read(){
ll x=;int ch=getchar(),f=;
while (!isdigit(ch)&&(ch!='-')&&(ch!=EOF)) ch=getchar();
if (ch=='-'){f=-;ch=getchar();}
while (isdigit(ch)){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
}
int n,Max,a[N];
ll k,l,r,x,y,z,ans,Sum,cnt[maxn],sum[maxn];
int main(){
n=read(),x=read(),y=read(),z=x/y;
For(i,,n) a[i]=read(),Max=max(Max,a[i]),cnt[a[i]]++;
For(i,,Max*) sum[i]=sum[i-]+cnt[i]*i,cnt[i]+=cnt[i-];
ans=1000000000000000ll;
For(i,,max(Max,)){
Sum=;
if (Max%i==) k=Max/i;
else k=Max/i+;
if (i<=z)
For(j,,k){
l=(j-)*i,r=j*i;
Sum+=(r*(cnt[r-]-cnt[l])-sum[r-]+sum[l])*y;
}
else
For(j,,k){
l=(j-)*i,r=j*i;
Sum+=(cnt[r-z-]-cnt[l])*x+(r*(cnt[r-]-cnt[r-z-])-sum[r-]+sum[r-z-])*y;
}
ans=min(ans,Sum);
}
printf("%lld",ans);
}
Codeforces 850B的更多相关文章
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
- CodeForces - 148D Bag of mice
http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...
随机推荐
- 关于反序列化时抛出java.io.EOFException异常
https://www.cnblogs.com/ouhaitao/p/7683568.html https://blog.csdn.net/mym43210/article/details/40081 ...
- [Unity]在Shader中获取摄像机角度、视线的问题
又踩了一坑,好在谷歌到了之前的一个人遇到相同的问题,顺利解决. 先说说问题背景,我目前的毕设是体数据渲染,实现的办法是raycast.最基本的一点就是在fragment program里,获取rayc ...
- SQL注入文件读取通过from for分页读取
http://103.238.227.13:10088/?id=1 在读取文件的时候发现不能够一下子全部读取出来.经过百度学习了一下,看到别人使用from for说实在此前真不知道这操作. 先来看一下 ...
- AlertDialog.Builder 显示为白色 蓝色字
AlertDialog.Builder dialog = new AlertDialog.Builder( getActivity(),AlertDialog.THEME_HOLO_LIGHT);
- 448D - Codeforces
D. Multiplication Table time limit per test 1 second memory limit per test 256 megabytes Bizon the C ...
- BP神经网络-- 基本模型
转载:http://www.cnblogs.com/jzhlin/archive/2012/07/28/bp.html BP 神经网络中的 BP 为 Back Propagation 的简写,最早它 ...
- python基础===修改属性的值
可以以三种不同的方式修改属性的值:直接通过实例进行修改:通过方法进行设置:通过方法进行递增(增加特定的值).下面依次介绍这些方法. class Car(): def __init__(self, ma ...
- python基础===requests学习笔记
这里有一个新的学习requests网站:http://docs.python-requests.org/zh_CN/latest/user/quickstart.html2017/11/30 Requ ...
- 安装openssl-0.9.8报错out range of signed 32bit displacement .
安装openssl-0.9.8报错out range of signed 32bit displacement http://blog.csdn.net/wangtingyao1990/article ...
- js-callee,call,apply概念
JS - caller,callee,call,apply 概念[转载] 在提到上述的概念之前,首先想说说javascript中函数的隐含参数:arguments Arguments : 该对象代表正 ...