zoukankan      html  css  js  c++  java
  • Codeforces Round #432 (Div. 1) B. Arpa and a list of numbers

    qtmd的复习pat,老子不想看了,还不如练几道cf

    这题首先可以很容易想到讨论最后的共因子为素数
    这个素数太多了,1-1e6之间的素数
    复杂度爆炸
    所以使用了前缀和,对于每个素数k的每个小区间
    (kg, k(g + 1)]是可以直接求这个区间的最佳方案的

    #include<iostream>
    #include<map>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<set>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    const int N = 1e6+5;
    #define MS(x,y) memset(x,y,sizeof(x))
    #define MP(x, y) make_pair(x, y)
    const int INF = 0x3f3f3f3f;
    
    ll sum[N];
    int cnt[N];
    int isprime[N];
    
    int j;
    ll Sum(int x) {
        if(x <= j) return sum[j];
        else if(x >= N) return sum[N-1];
        else return sum[x]; 
    }
    int Cnt(int x) {
        if(x <= j) return cnt[j];
        else if(x >= N) return cnt[N-1];
        else return cnt[x];
    }
    int main() {
        int n, x, y;
        while(~scanf("%d %d %d", &n, &x, &y)) {
            int ed = x/y; 
            for(int i = 0; i < n; ++i) {
                int a; scanf("%d", &a);
                sum[a] += a;
                cnt[a] ++;
            }
            for(int i = 1; i < N; ++i) {
                sum[i] += sum[i-1];
                cnt[i] += cnt[i-1];
            }
            ll ans = 1e18;
    
            for(int i = 2; i < N; ++i) {
                if(!isprime[i]) {
                    ll tmp = 0;
                    for(j = 0; j < N; j += i) {
                        isprime[j] = 1;
    
                        int fr1 = j + i - 1; int to1 = j + i - ed - 1;   int fr2 = j + i - ed - 1; int to2 = j;
    
                        ll t1 = Sum(fr1) - Sum(to1); int t2 = Cnt(fr1) - Cnt(to1);
                        int t3 = Cnt(fr2) - Cnt(to2);
                    //  if(i == 17 && j < 20) printf("%d %lld %d %d %lld
    ", j, t1, t2, t3, tmp);
    
                        tmp += 1ll*(1ll*t2*(j+i) - t1) * y + 1ll* t3 * x;
                    }
                    ans = min(ans, tmp);
                }
            }
            printf("%lld
    ", ans);
        }
        return 0;
    }
  • 相关阅读:
    【JZOJ6409】困难的图论
    学习LCT小结
    jzoj5432. 【NOIP2017提高A组集训10.28】三元组
    jzoj6367. 【NOIP2019模拟2019.9.25】工厂(factory)
    jzoj6366. 【NOIP2019模拟2019.9.25】化学(chem)
    jzoj5433. 【NOIP2017提高A组集训10.28】图
    学习拓展中国剩余定理小结
    jzoj6300. Count
    jzoj3736. 【NOI2014模拟7.11】数学题
    jzoj6276. 【noip提高组模拟1】树
  • 原文地址:https://www.cnblogs.com/Basasuya/p/8433689.html
Copyright © 2011-2022 走看看