zoukankan      html  css  js  c++  java
  • [P3911] 最小公倍数之和

    Description

    对于(A_1,A_2,cdots,A_N),求 (sum_{i=1}^Nsum_{j=1}^N lcm(A_i,A_j))

    Solution

    经过一波推导得到

    [sum_{i=1}^Nsum_{j=1}^N lcm(A_i,A_j) = sum_{d=1}^N d (sum_{i=1}^{N/d} i C_{id})^2 sum_{k|d}kmu(k) ]

    暴力计算即可

    #include <bits/stdc++.h>
    using namespace std;
    
    #define int long long
    const int N = 100005;
    const int MAXN = 100005;
    
    bool isNotPrime[MAXN + 1];
    int mu[MAXN + 1], phi[MAXN + 1], primes[MAXN + 1], cnt;
    inline void euler() {
        isNotPrime[0] = isNotPrime[1] = true;
        mu[1] = 1;
        phi[1] = 1;
        for (int i = 2; i <= MAXN; i++) {
            if (!isNotPrime[i]) {
                primes[++cnt] = i;
                mu[i] = -1;
                phi[i] = i - 1;
            }
            for (int j = 1; j <= cnt; j++) {
                int t = i * primes[j];
                if (t > MAXN) break;
                isNotPrime[t] = true;
                if (i % primes[j] == 0) {
                    mu[t] = 0;
                    phi[t] = phi[i] * primes[j];
                    break;
                } else {
                    mu[t] = -mu[i];
                    phi[t] = phi[i] * (primes[j] - 1);
                }
            }
        }
    }
    
    int n,c[N];
    
    signed main() {
        euler();
        ios::sync_with_stdio(false);
        cin>>n;
        for(int i=1;i<=n;i++) {
            int tmp;
            cin>>tmp;
            c[tmp]++;
        }
        int ans=0;
        for(int d=1;d<=N;d++) {
            int a1=0,a2=0;
            for(int i=1;i<=N/d;i++) {
                a1+=i*c[i*d];
            }
            vector <int> fac;
            for(int i=1;i*i<=d;i++) {
                if(d%i==0) {
                    fac.push_back(i);
                    if(i*i!=d) fac.push_back(d/i);
                }
            }
            for(int k:fac) {
                a2+=k*mu[k];
            }
            ans+=d*a1*a1*a2;
        }
        cout<<ans;
    }
    
    
  • 相关阅读:
    BZOJ 4245: [ONTAK2015]OR-XOR
    BZOJ 2535: [Noi2010]Plane 航空管制2
    COGS 2551. 新型武器
    cogs2550. 冰桥,升起来了!
    大数模板
    uva 1513(线段树)
    uva 11525(线段树)
    poj 3368(RMQ模板)
    hdu 4686 Arc of Dream(矩阵快速幂)
    poj 3321 Apple Tree(树状数组)
  • 原文地址:https://www.cnblogs.com/mollnn/p/13185826.html
Copyright © 2011-2022 走看看