• 欢迎光临~

# 「题解」AGC038C LCMs

(i)(j) 不对称很烦，求 (sum_isum_jmathrm{lcm}(A_i,A_j)) 再减去 (sum_i A_i) 再除 (2) 即可得到答案。现在来考虑 (i)(j) 取值均为 (0sim N-1) 的式子：

[begin{aligned} &sum_isum_jmathrm{lcm}(A_i,A_j) \ =&sum_isum_jfrac{A_iA_j}{gcd(A_i,A_j)} \ =&sum_dfrac{1}{d}sum_isum_j A_iA_j[gcd(A_i,A_j)=d] end{aligned} ]

([gcd(A_i,A_j)=d]) 我们束手无策，但 ([gcd(i,j)=d]) 我们熟能生巧

[begin{aligned} &sum_{d}frac{1}{d}sum_{d|i}sum_{d|j}B_iB_jij[gcd(i,j)=d] \=&sum_{d}frac{1}{d}sum_{i,j}B_{id}B_{jd}ijd^2sum_{e|i,e|j}mu(e) \=&sum_{d}dsum_emu(e)left(sum_{e|i}B_{id}iright)^2 \=&sum_{d}dsum_emu(e)left(sum_{de|i}B_{i}frac{i}{d}right)^2 \=&sum_{d}frac{1}{d}sum_emu(e)left(sum_{de|i}B_{i}iright)^2 end{aligned} ]

[begin{aligned} sum_s f(s)sum_{d|s}frac{1}{d}mu(frac{s}{d}) end{aligned} ]

(m=max(A))(f,g) 的预处理暴力实现均为 (mathcal{O}(mlog m))

``````#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<ctime>
#include<random>
#include<assert.h>
#define pb emplace_back
#define mp make_pair
#define fi first
#define se second
#define dbg(x) cerr<<"In Line "<< __LINE__<<" the "<<#x<<" = "<<x<<'n';
#define dpi(x,y) cerr<<"In Line "<<__LINE__<<" the "<<#x<<" = "<<x<<" ; "<<"the "<<#y<<" = "<<y<<'n';
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>pii;
typedef pair<ll,int>pli;
typedef pair<ll,ll>pll;
typedef pair<int,ll>pil;
typedef vector<int>vi;
typedef vector<ll>vll;
typedef vector<pii>vpii;
typedef vector<pil>vpil;
template<typename T>T cmax(T &x, T y){return x=x>y?x:y;}
template<typename T>T cmin(T &x, T y){return x=x<y?x:y;}
char buf[1<<21],*p1=buf,*p2=buf;
inline void read(int& r){
r=0;bool w=0;char ch=getchar();
while(ch<'0'||ch>'9')w=ch=='-'?1:0,ch=getchar();
while(ch>='0'&&ch<='9')r=r*10+(ch^48),ch=getchar();
r=w?-r:r;
}
const int N=1000010;
const int mod=998244353;
int gcd(int a,int b){
return !b?a:gcd(b,a%b);
}
inline void cadd(int &x,int y){
x=(x+y>=mod)?(x+y-mod):(x+y);
}
inline void cdel(int &x,int y){
x=(x-y<0)?(x-y+mod):(x-y);
}
int qpow(int x,int y){
int s=1;
while(y){
if(y&1)s=1ll*s*x%mod;
x=1ll*x*x%mod;
y>>=1;
}
return s;
}
int n,mx,fac[N];
int vis[N],mu[N];
int f[N],g[N];
int ans;
vi pr;
void init(){
fac[0]=1;
for(int i=1;i<=mx;i++)fac[i]=1ll*fac[i-1]*i%mod;
g[mx]=qpow(fac[mx],mod-2);
for(int i=mx-1;~i;--i)g[i]=1ll*g[i+1]*(i+1)%mod;
for(int i=1;i<=mx;i++)g[i]=1ll*g[i]*fac[i-1]%mod;
mu[1]=1;
for(int i=2;i<=mx;i++){
if(!vis[i]){
pr.pb(i);
mu[i]=-1;
}
for(auto j:pr){
if(i*j>mx)break;
vis[i*j]=1;
if(i%j==0)break;
mu[i*j]=-mu[i];
}
}
for(int i=1;i<=mx;i++)
if(mu[i]==-1)
mu[i]=mod-1;
}
signed main(){
#ifdef do_while_true
//		assert(freopen("data.in","r",stdin));
#endif
for(int i=1;i<=n;i++){
}
init();
for(int i=1;i<=mx;i++)f[i]=1ll*f[i]*i%mod;
for(auto x:pr)
for(int i=mx/x;i;i--)