Desciption
题目链接:P1829
给定 n,m,求
1leq n,mleq 10^7
Solution
首先抛开模数,
根据最小公倍数定义,
然后常见套路,枚举 gcd,
改变枚举顺序,把 gcd(i,j)=d 消成 gcd(i,j)=1,
然后根据莫比乌斯函数性质,
改变枚举顺序,
然后根据结合律,
那么令 F(x)=mu(x)x^2,G(x)=sumlimits_{i=1}^xi,由于其均是积性函数,故
然后 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;
}