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

Desciption

题目链接:P1829

给定 $n,m$,求

$$(\sum\limits_{i=1}^n\sum\limits_{j=1}^mlcm(i,j)) \bmod 20101009$$

$1\leq n,m\leq 10^7$

Solution

首先抛开模数,

$$\sum\limits_{i=1}^n\sum\limits_{j=1}^mlcm(i,j)$$

根据最小公倍数定义,

$$=\sum\limits_{i=1}^n\sum\limits_{j=1}^m\frac{ij}{gcd(i,j)}$$

然后常见套路,枚举 $gcd$,

$$=\sum\limits_{i=1}^n\sum\limits_{j=1}^m\sum\limits_{d=1}^n\frac{ij}{d}[gcd(i,j)=d]$$

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

$$=\sum\limits_{d=1}^n\sum\limits_{i=1}^{\lfloor\frac{n}{d} \rfloor}\sum\limits_{j=1}^{\lfloor\frac{m}{d}\rfloor}dij[gcd(i,j)=1]$$

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

$$=\sum\limits_{d=1}^n\sum\limits_{i=1}^{\lfloor\frac{n}{d} \rfloor}\sum\limits_{j=1}^{\lfloor\frac{m}{d}\rfloor}\sum\limits_{kgcd(i,j)}\mu(k)dij$$

改变枚举顺序,

$$=\sum\limits_{d=1}^nd\sum\limits_{k=1}^{\lfloor{\frac{n}{d}}\rfloor}\mu(k)k^2\sum\limits_{i=1}^{\lfloor \frac{n}{dk}\rfloor}\sum\limits_{j=1}^{\lfloor \frac{m}{dk}\rfloor}ij$$

然后根据结合律,

$$=\sum\limits_{d=1}^nd\sum\limits_{k=1}^{\lfloor{\frac{n}{d}}\rfloor}\mu(k)k^2(\sum\limits_{i=1}^{\lfloor \frac{n}{dk}\rfloor}i)(\sum\limits_{j=1}^{\lfloor \frac{m}{dk}\rfloor}j)$$

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

$$=\sum\limits_{d=1}^nd\sum\limits_{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)$($\sum\limits_{i=1}^N \sqrt\frac{N}{i}\approx N$)

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#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%10+'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;
}