Arpa and a list of numbers
time limit per test2 seconds
memory limit per test256 megabytes
Arpa has found a list containing n numbers. He calls 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 perform two 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 apply these operations to as many numbers as he wishes, and he is allowed to apply the second operation arbitrarily many times on the same number.
Help Arpa to find the minimum possible cost to make the list good.
Input
First line contains three integers n, 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 line contains n integers a1, a2, …, an (1 ≤ ai ≤ 106) — the elements of the list.
Output
Print a single integer: the minimum possible 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, number 1 must be deleted (with cost 23) and number 16 must increased by 1 (with cost 17).
A gcd (greatest common divisor) of a set of numbers is the maximum integer that divides all integers in the set. Read more about gcd here.
杰霸学长用了前缀和,一开始想错了没理解,还以为很复杂结果并没有
这个思路是比较基础的,把前缀和处理的方法在下下面
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn = 500010;
const int max_num = 1000010;
ll pri[max_num];
ll a[maxn];
ll vis[max_num];
ll cnt, n;
ll x, y;
int main()
{
scanf("%I64d%I64d%I64d", &n, &x, &y);
for(ll i = 0; i < n; i++){
scanf("%I64dd", &a[i]);
vis[a[i]] ++;
}
long long ans = 1e16;
for(ll i = 2; i <= max_num; i++){
ll cnt = 0;
if(!pri[i]){
for(ll j = i; j <= max_num; j+=i){
cnt += vis[j];
pri[j] = 1;
}
if((n-cnt)*min(x, y) < ans){
long long rec = 0;
for(ll j = 0; j < n; j++){
ll tmp = a[j] % i;
if(tmp)
rec += min(x, y*(i-tmp));
}
//printf(" %I64d %d\n", ans, i);
ans = min(rec, ans);
}
}
}
printf("%I64d\n", ans);
return 0;
}
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;
const int maxn=2e6+10;
long long num[maxn],sum[maxn];
int n;
int x,y;
int main()
{
cin>>n>>x>>y;
int p=x/y;
for (int i=0;i<n;i++)
{
int a;
scanf("%d",&a);
num[a]++;
sum[a]+=(long long)a;
}
for (int i=1;i<maxn;i++)
{
num[i]+=num[i-1];
sum[i]+=sum[i-1];
}
long long ans=(long long)n*x;
for (int i=2;i<=2000000;i++)
{
long long temp=0;
for (int j=i;j<=2000000;j+=i)
{
int pos=max(j-i,j-p-1);
temp+=((num[j]-num[pos])*j-(sum[j]-sum[pos]))*y;
temp+=(num[pos]-num[j-i])*x;
}
ans=min(ans,temp);
}
printf("%I64d\n",ans);
return 0;
}
tarjan算法
http://www.cnblogs.com/JVxie/p/4854719.html
http://blog.csdn.net/jeryjeryjery/article/details/52829142?locationNum=4&fps=1