【JZOJ6150】爱乐之城
【JZOJ6150】爱乐之城
by AmanoKumiko
Description
求
\[f(n)=Σ_{i=1}^nΣ_{j=1}^nμ(ij)
\]
\[g(n)=Σ_{i=1}^niΣ_{j=1}^nj[i⊥j]
\]
\[F(S)=Σ_{T∈S}f(gcd(a∈T))Π_{a∈T}g(a)
\]
强制在线,模数998244353
Input
第一行三个整数n,m,op,n表示音符个数,m,op的含义见题意
Output
若op=0,输出一个整数\(F(a_{1-n})\)
若op=1,输出i行,第i行一个整数\(F(a_{1-i})\)
Sample Input
3 5 0
1 2 3
Sample Output
231
Data Constraint
n<=400000,m<=400000
Solution
\[f(n)=Σ_{i=1}^nΣ_{j=1}^nμ(ij)
\]
\[f(n)=Σ_{i=1}^nΣ_{j=1}^nμ(i)μ(j)[i⊥j]
\]
\[f(n)=Σ_{i=1}^nΣ_{j=1}^nμ(i)μ(j)Σ_{d|gcd(i,j)}μ(d)
\]
\[f(n)=Σ_{d=1}^nμ(i)Σ_{i=1}^{\lfloor{\frac{n}{d}}\rfloor}Σ_{j=1}^{\lfloor{\frac{n}{d}}\rfloor}μ(id)μ(jd)
\]
\[f(n)=Σ_{d=1}^nμ(i)(Σ_{i=1}^{\lfloor{\frac{n}{d}}\rfloor}μ(id))^2
\]
故增量处理
\[g(n)=Σ_{i=1}^niΣ_{j=1}^nj[i⊥j]
\]
\[g(n)=2(Σ_{i=1}^niΣ_{j=1}^ij[i⊥j]-Σ_{i=1}^n[i⊥i]i^2)+Σ_{i=1}^n[i⊥i]i^2
\]
\[g(n)=2(Σ_{i=1}^niΣ_{j=1}^ij[i⊥j]-1)+1
\]
\[g(n)=2(Σ_{i=1}^niΣ_{d|i}μ(d)Σ_{j=1}^{\frac{i}{d}}jd-1)+1
\]
\[g(n)=2(Σ_{i=1}^n\frac{i}{2}Σ_{d|i}μ(d)(1+\frac{i}{d})\frac{i}{d}d-1)+1
\]
\[g(n)=2(Σ_{i=1}^n\frac{i}{2}Σ_{d|i}μ(d)(i+\frac{i^2}{d})-1)+1
\]
\[g(n)=2(Σ_{i=1}^n\frac{i^2}{2}Σ_{d|i}μ(d)(1+\frac{i}{d})-1)+1
\]
\[g(n)=2(Σ_{i=1}^n\frac{i^2}{2}([i=1]+φ(i))-1)+1
\]
\[g(n)=Σ_{i=1}^ni^2([i=1]+φ(i))-1
\]
\[g(n)=Σ_{i=1}^nφ(i)i^2
\]
考虑构造\(f(n)=Σ_{d|n}h(d)\)
\[h(n)=Σ_{d|n}f(d)μ(\frac{n}{d})
\]
故原式
\[F(S)=Σ_{T∈S}(Σ_{d|gcd(a∈T)}h(d))Π_{a∈T}g(a)
\]
\[F(S)=Σ_{d=1}^mh(d)(Π_{d|a_i}(g(a_i)+1)-1)
\]
每个数选与不选,故为\(g(a_i)+1\),减掉空集故最后\(-1\)
同样增量处理
Code
#pragma GCC optimize("Ofast")
#pragma GCC target("sse3","sse2","sse")
#pragma GCC diagnostic error "-std=c++14"
#pragma GCC diagnostic error "-fwhole-program"
#pragma GCC diagnostic error "-fcse-skip-blocks"
#pragma GCC diagnostic error "-funsafe-loop-optimizations"
#pragma GCC optimize("fast-math","unroll-loops","no-stack-protector","inline")
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
#define fs(i,a) for(int i=1;i<=t[a];i++)
#define LL long long
#define mo 998244353
#define N 400100
#define M 400010
namespace IO{
const int sz=1<<22;
char a[sz+5],b[sz+5],*p1=a,*p2=a,*t=b,p[105];
inline char gc(){
return p1==p2?(p2=(p1=a)+fread(a,1,sz,stdin),p1==p2?EOF:*p1++):*p1++;
}
template<class T>void read(T& x){
x=0;char c=gc();
for(;c<'0'||c>'9';c=gc());
for(;c>='0'&&c<='9';c=gc())x=x*10+(c-'0');
}
inline void flush(){fwrite(b,1,t-b,stdout),t=b;}
inline void pc(char x){*t++=x;if(t-b==sz)flush();}
template<class T>void write(T x,char c='\n'){
if(x==0)pc('0');int t=0;
for(;x;x/=10)p[++t]=x%10+'0';
for(;t;--t)pc(p[t]);pc(c);
}
struct F{~F(){flush();}}f;
}
using IO::read;
using IO::write;
int *d[N],t[N],e[N*30],*tmp=e;
int n,m,op,miu[N],prime[N],phi[N],tot,vis[N],Min[N],sigma[N];
LL a[N],f[N],g[N],h[N],ans,pl[N];
inline LL sqr(LL x){return x*x%mo;}
inline LL mod(LL x){return x>=mo?x-mo:x;}
int main(){
freopen("lalaland.in","r",stdin);
freopen("lalaland.out","w",stdout);
read(n);read(m);read(op);
vis[1]=miu[1]=phi[1]=g[1]=1;
fo(i,2,M){
if(!vis[i])prime[++tot]=i,miu[i]=-1,phi[i]=i-1,sigma[i]=2,Min[i]=1;
for(int j=1;j<=tot&&i*prime[j]<M;j++){
vis[i*prime[j]]=1;
if(!(i%prime[j])){
miu[i*prime[j]]=0;
phi[i*prime[j]]=phi[i]*prime[j];
sigma[i*prime[j]]=sigma[i]/(Min[i]+1)*(Min[i]+2);
Min[i*prime[j]]=Min[i]+1;
break;
}
else{
miu[i*prime[j]]=-miu[i];
phi[i*prime[j]]=phi[i]*(prime[j]-1);
sigma[i*prime[j]]=sigma[i]*2;
Min[i*prime[j]]=1;
}
}
miu[i]=miu[i]<0?miu[i]+mo:miu[i];
g[i]=mod(g[i-1]+(LL)i*i%mo*phi[i]%mo);
}
fo(i,1,M)d[i]=tmp,tmp+=sigma[i];
fo(i,1,M){int x=M/i;fo(j,1,x)d[i*j][++t[i*j]]=i;}
fo(i,1,M){
f[i]=f[i-1];
fs(j,i){
f[i]=f[i]-miu[d[i][j]]*sqr(pl[d[i][j]])%mo;
f[i]=f[i]<0?f[i]+mo:f[i];
pl[d[i][j]]=mod(pl[d[i][j]]+miu[i]);
f[i]=mod(f[i]+miu[d[i][j]]*sqr(pl[d[i][j]])%mo);
h[i]=mod(h[i]+f[d[i][j]]*miu[i/d[i][j]]%mo);
}
}
fo(i,1,m)pl[i]=1;
fo(i,1,n){
read(a[i]);
if(op==1)a[i]=((LL)19891989*ans+a[i])%m+1;
fs(j,a[i]){
ans=ans-h[d[a[i]][j]]*(pl[d[a[i]][j]]-1)%mo;
ans=ans<0?ans+mo:ans;
(pl[d[a[i]][j]]*=g[a[i]]+1)%=mo;
ans=mod(ans+h[d[a[i]][j]]*(pl[d[a[i]][j]]-1)%mo);
}
if(op==1||op==0&&i==n)write(ans);
}
return 0;
}