前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >P1829 [国家集训队]Crash的数字表格 / JZPTAB

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

作者头像
yzxoi
发布2022-09-16 14:29:04
1890
发布2022-09-16 14:29:04
举报
文章被收录于专栏:OI

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^2G(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

代码语言: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%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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-05-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Desciption
  • Solution
  • Code
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档