Atcoder AGC038 C- LCMs
We have an integer sequence of length
Find the following sum:
#include<bits/stdc++.h> using namespace std; const int N=1e6+5; const int p=998244353; typedef long long ll; int n,a[N],f[N]={0}; short int mu[N]; ll inv[N],w[N]; int main(){ mu[1]=1;inv[1]=1; for(int i=2;i<N;i++)inv[i]=(p-p/i)*inv[p%i]%p; for(int i=1;i<N;i++) for(int j=i+i;j<N;j+=i)mu[j]-=mu[i]; for(int i=1;i<N;i++) for(int j=i;j<N;j+=i)w[j]+=mu[j/i]*inv[i],w[j]=(w[j]%p+p)%p; cin>>n; for(int i=1;i<=n;i++)scanf("%d",&a[i]),f[a[i]]++; ll ans=0; for(int d=1;d<N;d++){ ll res=0,ret=0; for(int i=d;i<N;i+=d){ if(f[i]){ ret+=1LL*i*((res*f[i]%p+1LL*f[i]*(f[i]-1)/2%p*i%p)%p)%p; res+=1LL*i*f[i]%p; ret%=p,res%=p; } } ans+=w[d]*ret%p; ans%=p; } printf("%lld\n",ans); }