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只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...
随机推荐
- POj 2104 K-th Number (分桶法+线段树)
题目链接 Description You are working for Macrohard company in data structures department. After failing ...
- quartz的简介
1. 介绍 Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源的任务调度框架,是完全由java开发的一个开源的任务日程管理系统,“任务进度管理器”就是一个在预 ...
- perl模拟登录(1)
use WWW::Mechanize; my $ua = WWW::Mechanize->new(); $ua->post('http://localhost/dvwa/DVWA-mast ...
- mysql执行load_fle返回NULL的解决方法
mysql 版本: 5.7.18 问题: 在执行mysql 函数load_file时,该函数将加载指定文件的内容,存储至相应字段.如: SELECT LOAD_FILE("D:\aa.txt ...
- STM32-内存管理
转载:http://www.cnblogs.com/guozhikai/p/6031904.html #ifndef __MALLOC_H #define __MALLOC_H #include &q ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 动态树
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 题意:加边,删边,查询到根的距离. #include <bits/stdc++ ...
- c json实战引擎四 , 最后❤跳跃
引言 - 以前那些系列 长活短说, 写的最终 scjson 纯c跨平台引擎, 希望在合适场景中替代老的csjon引擎, 速度更快, 更轻巧. 下面也是算一个系列吧. 从cjson 中得到灵感, 外加 ...
- web优化的方法
缓存(减小对服务器.数据库的压力) 生成静态页面(区别于缓存,数据量太大用“缓存”不利) URL重写(SEO,搜索引擎的优化) ajax的优化(SEO) <meta content=“” nam ...
- 修改centos地址连接为自动连接
1.进入目录/etc/sysconfig/network-scripts/ 2.修改ifcfg-etn0 文件 (即你的网卡标识命名的配置文件) 3.将ONBOOT=no改成yes 4.保存后重启 ...
- php性能的问题
一.影响php性能的常见原因 1.php自身语法使用不当 2.php做了不擅长的时期() 3.php的周边环境(服务器Linux,磁盘:文件存储,数据库,缓存:内存,网络:带宽) 4.php自身的短板 ...