Codeforces Codeforces Round #432 (Div. 2 D ) Arpa and a list of numbers
D. Arpa and a list of numbers
time limit per test 2 seconds
memory limit per test 256 megabytes
Arpa has found alist containingn numbers. Hecalls a list bad if and only if it is not empty and gcd (see notes section for more information) of numbers in the list is 1.
Arpa can performtwo types of operations:
· Choose a number and delete it with cost x.
· Choose a number and increase it by 1 with cost y.
Arpa can applythese operations to as many numbers as he wishes, and he is allowed to applythe second operation arbitrarily many times on the same number.
Help Arpa tofind the minimum possible cost to make the list good.
Input
First linecontains three integersn,x and y (1 ≤ n ≤ 5·105,1 ≤ x, y ≤ 109) — the number of elements in the list and the integers x and y.
Second linecontainsn integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the elements of the list.
Output
Print a single integer: the minimumpossible cost to make the list good.
Examples
Input
4 23 17
1 17 17 16
Output
40
Input
10 6 2
100 49 71 73 66 96 8 60 41 63
Output
10
Note
In example,number1 must be deleted (with cost 23) and number 16 must increased by 1 (with cost 17).
Agcd (greatest common divisor) of a set of numbers is the maximum integer thatdivides all integers in the set. Read more about gcdhere.
【题意】
给出n个数,以及x,y。现在你可以对这n个数进行两种操作。
- 把任意一个数删除,花费为x。
- 把任意一个数加一,花费为y。
现在要求操作后所有数gcd不为1(不互质),求最小花费。
【思路】
显然可以想到去枚举gcd,考虑到时间上的优化,我们可以枚举素数,因为每个不为1的数一定是一个素数或者一个素数的倍数。(事实上直接暴力枚举gcd也能卡过)
素数用素筛预处理一下即可。
假设我们枚举的素数为prime。那么我们需要把所有数变为这个素数或其倍数或者将其中一个(或几个)删除。
显然如果我们要改变某个数的值,我们一定是增加到离它最近的是prime的倍数的那个数。所以我们应该提前预处理一下前缀和sum[i]、num[i],sum[i]表示小于等于i的数的和,num[i]表示小于等于i的数的个数,然后在区间内进行操作。
假设现在枚举的区间为【 k * prime[i] , (k+1) * prime[i]),那么我们先应该在区间内找一个分界点limit,一个数小于等于limit时删除它花费更少,否则把它一直加到k * prime[i]的花费更少。
limit可由x,y的大小得到。
limit=max( (j+1) * prime[i]-rate-1 , j * prime[i]);
然后就对区间里的数根据limit分成的两部分分别计算。
对于删除的那部分,计算方法为删除个数*x,即:
num[ limit ]-num[ j * prime[i]] * x
而对于加上的那部分,计算方法为(数的个数*加上后所得到的那个素数的倍数-原来的数的和) * y,即:
((num[(j+1) * prime[i]]-num[limit]) * ((j+1) * prime[i])-(sum[(j+1) * prime[i]]-sum[limit])) * y
然后不断更新最小值即可。
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- using namespace std;
- #define mst(a,b) memset((a),(b),sizeof(a))
- #define rush() int T;scanf("%d",&T);while(T--)
- typedef long long ll;
- const int maxn = *;
- const ll mod = 1e9+;
- const int INF = 0x3f3f3f3f;
- const double eps = 1e-;
- int prime[maxn]= {};
- int num_prime=;
- bool isprime[maxn]= {,};
- ll num[maxn],sum[maxn];
- void init()
- {
- for(int i=;i<maxn;i++)
- {
- if(!isprime[i]) prime[num_prime++]=i;
- for(int j=;j<num_prime&&i*prime[j]<maxn;j++)
- {
- isprime[i*prime[j]]=;
- if(i%prime[j]==) break;
- }
- }
- }
- int main()
- {
- int o;
- int n,x,y;
- init();
- while(~scanf("%d%d%d",&n,&x,&y))
- {
- mst(num,);
- mst(sum,);
- int Max=;
- for(int i=;i<n;i++)
- {
- scanf("%d",&o);
- num[o]++;
- sum[o]+=o;
- Max=max(Max,o);
- }
- for(int i=;i<=Max*;i++) //预处理前缀和
- {
- num[i]+=num[i-];
- sum[i]+=sum[i-];
- }
- int rate=x/y;
- ll ans=1e18;
- for(int i=;i<num_prime&&prime[i-]<=Max;i++)
- {
- ll cnt=;
- for(int j=;j*prime[i]<=Max;j++)
- {
- int limit=max((j+)*prime[i]-rate-,j*prime[i]);
- ll num1=num[limit]-num[j*prime[i]]; //删除
- ll sum2=sum[(j+)*prime[i]]-sum[limit]; //加上
- ll num2=num[(j+)*prime[i]]-num[limit];
- cnt+=num1*x;
- cnt+=(num2*((j+)*prime[i])-sum2)*y;
- if(cnt>ans) break; //优化
- }
- ans=min(ans,cnt);
- }
- printf("%I64d\n",ans);
- }
- return ;
- }
Codeforces Codeforces Round #432 (Div. 2 D ) Arpa and a list of numbers的更多相关文章
- Codeforces Round #432 (Div. 1) B. Arpa and a list of numbers
qtmd的复习pat,老子不想看了,还不如练几道cf 这题首先可以很容易想到讨论最后的共因子为素数 这个素数太多了,1-1e6之间的素数 复杂度爆炸 所以使用了前缀和,对于每个素数k的每个小区间 (k ...
- Codeforces Round #432 Div. 1 C. Arpa and a game with Mojtaba
首先容易想到,每种素数是独立的,相互sg就行了 对于一种素数来说,按照的朴素的mex没法做... 所以题解的简化就是数位化 多个数同时含有的满参数因子由于在博弈中一同变化的,让他们等于相当于,那么这样 ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- Codeforces Beta Round #77 (Div. 2 Only)
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
- Codeforces Beta Round #76 (Div. 2 Only)
Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...
- Codeforces Beta Round #75 (Div. 2 Only)
Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...
- Codeforces Beta Round #74 (Div. 2 Only)
Codeforces Beta Round #74 (Div. 2 Only) http://codeforces.com/contest/90 A #include<iostream> ...
随机推荐
- 自动创建数据库(DELPHI+SQL SERVER)
procedure TForm1.Btn_OKClick(Sender: TObject); var sqlconn:string; begin Sqlconn:='Provider=SQLOLEDB ...
- configparser模块 subprocess 模块,xlrd 模块(表格处理)
今日内容: 1.configparser模块 2.subprocess模块 3.xlrd(读),xlwt(写) 表格处理 configparser模块 import configparser # co ...
- Linux C/C++基础——文件(上)
1.文件指针 FILE* fp=NULL; fp指针,只调用了fopen(),在堆区分配空间,把地址返回给fp fp指针不是指向文件,fp指针和文件关联,fp内部成员保存在文件的状态 操作fp指针,不 ...
- 【Linux开发】彻底释放Linux线程的资源
Linux系统中程序的线程资源是有限的,表现为对于一个程序其能同时运行的线程数是有限的.而默认的条件下,一个线程结束后,其对应的资源不会被释放,于是,如果在一个程序中,反复建立线程,而线程又默认的退出 ...
- 记录几个常用的Css样式效果
1.更改字体,图标大小小于12px无效的问题 若我们设置font-size:10px是不会有效果的,需要使用 transform: scale(0.68); 更改字体最小大小 2.设置div边框虚化, ...
- 西安邀请赛-D(带权并查集+背包)
题目链接:https://nanti.jisuanke.com/t/39271 题意:给定n个物品,m组限制,每个物品有个伤害值,现在让两个人取完所有物品,要使得两个人取得物品伤害值之和最接近,输出伤 ...
- [转帖]通俗易懂的Docker 入门教程
看完此文,妈妈还会担心你docker入不了门? http://www.17coding.info/article/24 上周对象突然心血来潮说想养个小宠物,我问想养啥她又说随便,你看着办!!!这我 ...
- 阿里云服务器挖矿脚本bioset攻击解决
1.问题出现 一大早刚起床,阿里云就给我发了一条短信,提醒我服务器出现紧急安全事件:挖矿程序 阿里云“贴心”地提供了解决方法,不过需要购买企业版的安全服务,本着能自己动手就不花钱原则自己搞了起来 于是 ...
- linux 三剑客之awk总结
AWK 1.begin end使用 cat /tmp/passwd |awk -F ':' 'BEGIN {print "hello"} {print $1"\t&quo ...
- 第一次编译ffmpeg
今天开始玩ffmpeg了. 从官网下载来的压缩包,不会编译诶,于是我开始研究起来了. 下面就是实时记录的随笔: 首先是从官网下载来的ffmpeg,就是下面这个版本,目前的最新版吧. http://ff ...