Description
题目链接:P3327
设 $d(x)$ 表示 $x$ 的约数个数,有 $T$ 组数据,给定 $n,m$ 求
$$\sum\limits_{i=1}^n\sum\limits_{j=1}^md(ij)$$
$1\leq T,n,m\leq 5\times 10^4$
Solution
首先你得知道:
$$d(ij)=\sum\limits_{xi}\sum\limits_{yj}[gcd(x,y)=1]$$
然后就是基本套路时间了:
$$\sum\limits_{i=1}^n\sum\limits_{j=1}^md(ij)$$
$$=\sum\limits_{i=1}^n\sum\limits_{j=1}^m\sum\limits_{xi}\sum\limits_{yj}[gcd(x,y)=1]$$
然后套路地改为枚举 $x,y$:
$$=\sum\limits_{x=1}^n\sum\limits_{y=1}^m[gcd(x,y)=1]\sum\limits_{i=1}^n\sum\limits_{j=1}^m[xi][yj]$$
显而易见:
$$=\sum\limits_{x=1}^n\sum\limits_{y=1}^m[gcd(x,y)=1]\lfloor\frac{n}{x} \rfloor\lfloor \frac{m}{y} \rfloor$$
然后为了美观,把 $x,y$ 换为 $i,j$,由莫比乌斯函数性质可得:
$$=\sum\limits_{i=1}^n\sum\limits_{j=1}^m\sum\limits_{dgcd(i,j)}\mu(d)\times \lfloor \frac{n}{i} \rfloor\times \lfloor \frac{m}{j} \rfloor$$
再根据常见套路,改为枚举 $d$:
$$=\sum\limits_{d=1}^n\mu(d)\sum\limits_{i=1}^{\lfloor \frac{n}{d} \rfloor}\sum\limits_{j=1}^{\lfloor\frac{m}{d} \rfloor}\lfloor \frac{n}{id}\rfloor \lfloor \frac{m}{jd}\rfloor$$
然后根据分配律,可以把后面式子分为两部分计算:
$$=\sum\limits_{d=1}^n\mu(d)(\sum\limits_{i=1}^{\lfloor \frac{n}{d} \rfloor}\lfloor \frac{n}{id}\rfloor)(\sum\limits_{j=1}^{\lfloor\frac{m}{d} \rfloor} \lfloor \frac{m}{jd}\rfloor)$$
所以我们只需要预处理出 $F(x)=\sum\limits_{i=1}^x\lfloor \frac{x}{i} \rfloor$ 即可。
$$=\sum\limits_{d=1}^n\mu(d)\times F(\lfloor\frac{n}{d}\rfloor)\times F(\lfloor \frac{m}{d}\rfloor)$$
时间复杂度:$\mathcal O(T\sqrt N +N\sqrt 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
| #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 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=5e4+10; int T,n,m,p[N],v[N],mu[N],tot; LL F[N]; 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++) mu[i]+=mu[i-1]; for(k=1;k<N;k++) for(i=1;i<=k;i=j+1) j=k/(k/i),F[k]+=1LL*(j-i+1)*(k/i); } I LL S(CI n,CI m){ RI i,j;LL X=0;for(i=1;i<=min(n,m);i=j+1) j=min(n/(n/i),m/(m/i)),X+=1LL*(mu[j]-mu[i-1])*F[n/i]*F[m/i];return X; } int main(){ GM(),read(T);W(T--) read(n,m),writeln(S(n,m));return 0; }
|