P1829 [国家集训队]Crash的数字表格 / JZPTAB

2022-09-16 14:29:04 浏览数 (2)

Desciption

题目链接:P1829

给定 n,m,求

(sumlimits_{i=1}^nsumlimits_{j=1}^mlcm(i,j)) bmod 20101009

1leq n,mleq 10^7

Solution

首先抛开模数,

sumlimits_{i=1}^nsumlimits_{j=1}^mlcm(i,j)

根据最小公倍数定义,

=sumlimits_{i=1}^nsumlimits_{j=1}^mfrac{ij}{gcd(i,j)}

然后常见套路,枚举 gcd

=sumlimits_{i=1}^nsumlimits_{j=1}^msumlimits_{d=1}^nfrac{ij}{d}[gcd(i,j)=d]

改变枚举顺序,把 gcd(i,j)=d 消成 gcd(i,j)=1

=sumlimits_{d=1}^nsumlimits_{i=1}^{lfloorfrac{n}{d} rfloor}sumlimits_{j=1}^{lfloorfrac{m}{d}rfloor}dij[gcd(i,j)=1]

然后根据莫比乌斯函数性质,

=sumlimits_{d=1}^nsumlimits_{i=1}^{lfloorfrac{n}{d} rfloor}sumlimits_{j=1}^{lfloorfrac{m}{d}rfloor}sumlimits_{kgcd(i,j)}mu(k)dij

改变枚举顺序,

=sumlimits_{d=1}^ndsumlimits_{k=1}^{lfloor{frac{n}{d}}rfloor}mu(k)k^2sumlimits_{i=1}^{lfloor frac{n}{dk}rfloor}sumlimits_{j=1}^{lfloor frac{m}{dk}rfloor}ij

然后根据结合律,

=sumlimits_{d=1}^ndsumlimits_{k=1}^{lfloor{frac{n}{d}}rfloor}mu(k)k^2(sumlimits_{i=1}^{lfloor frac{n}{dk}rfloor}i)(sumlimits_{j=1}^{lfloor frac{m}{dk}rfloor}j)

那么令 F(x)=mu(x)x^2G(x)=sumlimits_{i=1}^xi,由于其均是积性函数,故

=sumlimits_{d=1}^ndsumlimits_{k=1}^{lfloor{frac{n}{d}}rfloor}F(k)G(lfloor frac{n}{dk}rfloor)G(lfloor frac{m}{dk}rfloor)

然后 F(x) 直接筛的时候处理即可,G(x) 为等差数列,直接 mathcal O(1) 求出,最后用整除分块优化即可。

时间复杂度:mathcal O(N)sumlimits_{i=1}^N sqrtfrac{N}{i}approx N

Code

代码语言:javascript复制
#include<bits/stdc  .h>
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
#define W while
#define I inline
#define RI register int
#define LL long long
#define int LL
#define Cn const
#define CI Cn int&
#define gc getchar
#define D isdigit(c=gc())
#define pc(c) putchar((c))
#define min(x,y) ((x)<(y)?(x):(y))
#define max(x,y) ((x)>(y)?(x):(y))
using namespace std;
namespace Debug{
    Tp I void _debug(Cn char* f,Ty t){cerr<<f<<'='<<t<<endl;}
    Ts I void _debug(Cn char* f,Ty x,Ar... y){W(*f!=',') cerr<<*f  ;cerr<<'='<<x<<",";_debug(f 1,y...);}
    Tp ostream& operator<<(ostream& os,Cn vector<Ty>& V){os<<"[";for(Cn auto& vv:V) os<<vv<<",";os<<"]";return os;}
    #define gdb(...) _debug(#__VA_ARGS__,__VA_ARGS__)
}using namespace Debug;
namespace FastIO{
    Tp I void read(Ty& x){char c;int f=1;x=0;W(!D) f=c^'-'?1:-1;W(x=(x<<3) (x<<1) (c&15),D);x*=f;}
    Ts I void read(Ty& x,Ar&... y){read(x),read(y...);}
    Tp I void write(Ty x){x<0&&(pc('-'),x=-x,0),x<10?(pc(x '0'),0):(write(x/10),pc(x '0'),0);}
    Tp I void writeln(Cn Ty& x){write(x),pc('n');}
}using namespace FastIO;
Cn int N=1e7 10,P=20101009;
int n,m,p[N],v[N],mu[N],tot,F[N],Inv2;
I void GM(){
    RI i,j,k;for(mu[1]=1,i=2;i<N;i  ) for(!v[i]&&(mu[p[  tot]=i]=-1,0),j=1;j<=tot&&i*p[j]<N;j  )
    if(v[i*p[j]]=1,i%p[j]) mu[i*p[j]]=-mu[i];else break ;
    for(i=1;i<N;i  ) F[i]=(1LL*F[i-1] mu[i]*i%P*i%P)%P;
}
I int S(CI n,CI m){
    #define Sum(x) (1LL*(1 (x))*(x)%P*Inv2%P)
    RI i,j,k,Tn,Tm;LL X=0,Y=0;for(k=1;k<=min(n,m);(Y =X*k%P)%=P,k  ) for(Tn=n/k,Tm=m/k,X=0,i=1;i<=min(Tn,Tm);i=j 1)
    j=min(Tn/(Tn/i),Tm/(Tm/i)),(X =1LL*(F[j]-F[i-1])*Sum(Tn/i)%P*Sum(Tm/i)%P)%=P;return (Y P)%P;
}
signed main(){
    return Inv2=(P 1)/2,GM(),read(n,m),writeln(S(n,m)),0;
}

0 人点赞