zoukankan      html  css  js  c++  java
  • codeforces 851D. Arpa and a list of numbers(枚举gcd)

    题目链接

    D. Arpa and a list of numbers
    time limit per test2 seconds
    memory limit per test256 megabytes
    inputstandard input
    outputstandard output
    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.

    题意:给出一个长度为 (n) 的数组,以及 (x,y) ,然后 (n) 个数 (a_i) ,可以进行一下操作:

    1.删除某个数,花费x元。

    2.将某个数加1,花费y元。

    问要使得整个序列的 (gcd) 大于1,求最少花费。

    题解:我们定义 (cost(g)) 为当 (gcd) 包含质因子 (g) 时的最小花费,那么我们对 (10^6) 内所有的质因子求一遍 (cost(g)) 即可,不过不预处理每次处理得 (O(n)),铁定超时,那么我们需要预处理一下,定义一下两个函数:

    1.(cnt(l,r)) :计算数值范围在 ([l,r]) 以内的数的个数。

    2.(sum(l,r)):计算数值在范围 ([l,r]) 以内的所有数的和。

    那么当我们枚举到质数 (g) 时,对于 (g) 的倍数 (k),我们能够找到一个临界值 (f) 使得将区间((k-g,k]) 分成两段((k-g,k-f))([k-f,f]) 使得我们对前一段做删除操作更优,对后一段做加1操作加至 (k) 更优。其实,经过计算可得 (f=g-min(g,x/y)),那么,对于区间((k-g,f]) ,我们能够通过上面的预处理加速得到该区间的花费:

    (cnt(k-g+1,k-f-1) imes x+((cnt(k-f,k) imes k-sum(k-f,k)) imes y).

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<vector>
    #include<queue>
    #include<stack>
    using namespace std;
    #define rep(i,a,n) for (int i=a;i<n;i++)
    #define per(i,a,n) for (int i=n-1;i>=a;i--)
    #define pb push_back
    #define fi first
    #define se second
    #define dbg(...) cerr<<"["<<#__VA_ARGS__":"<<(__VA_ARGS__)<<"]"<<endl;
    typedef vector<int> VI;
    typedef long long ll;
    typedef pair<int,int> PII;
    const ll inf=1e17;
    const int maxn=5e5+10;
    int a[maxn],cnt[maxn*4];
    ll sum[maxn*4];
    int prime[maxn*2];
    void getPrim()
    {
        for(int i=2;i<1e6;i++)
        {
            if(!prime[i])
                prime[++prime[0]] = i;
            for(int j=1;(j<=prime[0])&&(i*prime[j]<1e6);j++)
            {
                prime[prime[j]*i] = 1;
                if(i%prime[j]==0) break;
            }
        }
    }
    
    int main()
    {
        getPrim();
        int n,x,y;
        scanf("%d%d%d",&n,&x,&y);
        int mx=0;
        rep(i,1,n+1)
        {
            scanf("%d",&a[i]);
            cnt[a[i]]++;
            sum[a[i]]+=1ll*a[i];
            mx=max(mx,a[i]);
        }
        rep(i,1,maxn*4)
        sum[i]+=sum[i-1],cnt[i]+=cnt[i-1];
        ll ans=inf;
        rep(i,1,prime[0]+1)
        {
            int t=prime[i];
            int p=min(x/y,t-1);
            ll res=0;
            for(int j=1;;j++)
            {
                int k=t*j;
                int f=k-p;
                res+=1ll*(cnt[f-1]-cnt[k-t])*x;
                res+=1ll*(1ll*(cnt[k]-cnt[f-1])*k-(sum[k]-sum[f-1]))*y;
                if(k>mx)  break;
            }
            ans=min(ans,res);
            if(t>mx) break;
            //if(ans<0&&i==1) dbg(i);
        }
        printf("%lld
    ",ans);
        return 0;
    }
    
    
  • 相关阅读:
    mysql8 JDBC连接注意事项
    Flask上下文管理源码分析
    python基础学习day5
    python基础学习day4
    python基础学习day3
    python基础学习day2
    python基础学习day1
    简单的名片管理系统
    pycharm教程
    Python:知识目录
  • 原文地址:https://www.cnblogs.com/tarjan/p/7482245.html
Copyright © 2011-2022 走看看