把博客园图标替换成自己的图标
把博客园图标替换成自己的图标end

luogu P3327 [SDOI2015]约数个数和

题面传送门
考虑经典结论:\(d(i\times j)=\sum\limits_{a|i}{\sum\limits_{b|j}{[(a,b)==1]}}\)
代到原式里面去就是\(\sum\limits_{i=1}^{n}{\sum\limits_{j=1}^{m}{\sum\limits_{a|i}{\sum\limits_{b|j}{[(a,b)==1]}}}}\)
提前枚举\(a\)\(b\)就有\(\sum\limits_{a=1}^{n}{\sum\limits_{b=1}^{m}{\lfloor\frac{n}{a}\rfloor\lfloor\frac{m}{b}\rfloor[(a,b)==1]}}\)
然后莫反就有\(\sum\limits_{a=1}^{n}{\sum\limits_{b=1}^{m}{\lfloor\frac{n}{a}\rfloor\lfloor\frac{m}{b}\rfloor\sum\limits_{d|(a,b)}}{\mu(d)}}\)
提前枚举\(d\)就有\(\sum\limits_{d=1}^{\min(n,m)}{\mu(d)\sum\limits_{a=1}^{\lfloor\frac{n}{d}\rfloor}{\sum\limits_{b=1}^{\lfloor\frac{m}{d}\rfloor}{\lfloor\frac{n}{ad}\rfloor\lfloor\frac{m}{bd}\rfloor}}}\)
各回各家各找各妈就有\(\sum\limits_{d=1}^{\min(n,m)}{\mu(d)\sum\limits_{a=1}^{\lfloor\frac{n}{d}\rfloor}{\lfloor\frac{n}{ad}\rfloor}\sum\limits_{b=1}^{\lfloor\frac{m}{d}\rfloor}{\lfloor\frac{m}{bd}\rfloor}}\)
\(f(x)=\sum\limits_{i=1}^{x}{\lfloor\frac{x}{i}\rfloor}\)这个东西可以整除分块预处理。那么我们就有
\(\sum\limits_{d=1}^{\min(n,m)}{\mu(d)f(\lfloor\frac{n}{d}\rfloor)f(\lfloor\frac{m}{d})}\)
显然这个东西可以整除分块做。所以复杂度就是\(O((n+q)\sqrt n)\)
code:

#include<cstdio>
#define min(a,b) ((a)<(b)?(a):(b))
using namespace std;
int T,pr[50039],flag[50039],ph,mu[50039],w=5e4,n,m,k,x,y,z;
long long f[50039],ans;
inline void swap(int &x,int &y){x^=y^=x^=y;}
int main(){
	freopen("1.in","r",stdin);
	register int i,j,h;mu[1]=1;
	for(i=2;i<=w;i++){
		if(!flag[i]) pr[++ph]=i,mu[i]=-1;
		for(j=1;j<=ph&&i*pr[j]<=w;j++){
			flag[i*pr[j]]=1;if(i%pr[j]==0) break;
			mu[i*pr[j]]=-mu[i];
		}
		mu[i]+=mu[i-1];
	}
	for(i=1;i<=w;i++){
		for(j=1;j<=i;j=h+1)h=i/(i/j),f[i]+=(h-j+1)*(i/j);
	}
	scanf("%d",&T);
	while(T--){
		scanf("%d%d",&x,&y);if(x>y) swap(x,y);ans=0;
		for(i=1;i<=x;i=j+1)j=min(x/(x/i),y/(y/i)),ans+=(mu[j]-mu[i-1])*f[x/i]*f[y/i];
		printf("%lld\n",ans);
	}
}
posted @ 2021-02-24 15:27  275307894a  阅读(47)  评论(0编辑  收藏  举报
浏览器标题切换
浏览器标题切换end