LCMs

题面描述

题解

\(\sum_{i=1}^{n-1}\sum_{j=i+1}^{n}lcm(a_i,a_j)=\sum_{i=1}^{n-1}\sum_{j=i+1}^{n}\frac{a_ia_j}{gcd(a_i,a_j)}\)
\(f(d)=\sum_{i=1}^{n-1}\sum_{j=i+1}^{n}a_ia_j\cdot[gcd(a_i,a_j)=d]\)
为方便计算设\(f(d)=\sum_{i=1}^{n}\sum_{j=1}^{n}a_ia_j\cdot[gcd(a_i,a_j)=d]\)
\(\:\:\:\:g(d)=\sum_{d|d'}f(d')=(\sum_{i=1}^{n}a_i\cdot[d|a_i])^2\)
根据莫比乌斯反演
\(f(d)=\sum_{d|d'}\mu(\frac{d'}{d})g(d')\)
\(ans=\sum_{i=1}^{max(a_1,a_2,···,a_n)}\frac{1}{i}\sum_{i|i'}\mu(\frac{i'}{i})g(i')\)
因为为了方便计算,算重了一些
\(ans=\frac{ans-\sum_{i=1}^{n}a_i*a_i}{2}\)

点击查看代码
#include<functional>
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<deque>
#define ll long long 
using namespace std;
const int maxn=1e6+101;
const int MOD=998244353;
const int inf=2147483447;
int read(){
    int x=0,f=1;char ch=getchar();
    for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1;
    for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';
    return x*f;
}
int mu[maxn],a[maxn],tot,is[maxn],prime[maxn];
int n,k;
ll book[maxn],g[maxn];
void get_mu(){
    mu[1]=1;
    for(int i=2;i<=maxn;i++){
        if(!is[i])prime[++tot]=i,mu[i]=-1;
        for(int j=1;j<=tot && prime[j]<=maxn/i;j++){
            is[i*prime[j]]=1;
            if(i%prime[j]==0)break;
            mu[i*prime[j]]=-mu[i];
        }
    }
}
ll ww(ll x){return (x%MOD+MOD)%MOD;}
ll power(ll x,ll y){
    ll ans=1;
    while(y){
        if(y&1)ans=ans*x%MOD;
        y>>=1;x=x*x%MOD;
    }
    return ans;
}
int main(){
    n=read();get_mu();int maxx=-1;
    for(int i=1;i<=n;i++){
        a[i]=read();
        book[a[i]]++;
        maxx=max(maxx,a[i]);
    }
    for(int i=1;i<=maxx;i++){
        ll sum=0;
        for(int j=i;j<=maxx;j+=i)sum=(sum+book[j]*j%MOD)%MOD;
        g[i]=ww(sum*sum%MOD);
    }
    ll ans=0;
    for(int i=1;i<=maxx;i++){
        ll num=0;
        for(int j=i;j<=maxx;j+=i)num=ww(num+(ll)mu[j/i]*g[j]%MOD)%MOD;
        num=ww(num*power(i,MOD-2)%MOD);
        ans=ww(ans+num)%MOD;
        if(book[i])ans=ww(ans-i*book[i]%MOD)%MOD;
    }
    printf("%lld",(ans*power(2,MOD-2)%MOD+MOD)%MOD);
    return 0;
}
posted @ 2021-11-03 21:05  I_N_V  阅读(49)  评论(0编辑  收藏  举报