P2257 YY的GCD

\(ans=\sum\limits_{i=1}^n\sum\limits_{i=1}^m[gcd(i,j)=prime]\)

设函数\(f(d)=\sum\limits_{i=1}^n\sum\limits_{i=1}^m[gcd(i,j)=d]\)

设函数$F(d)=\sum\limits_{i=1}n\sum\limits_{i=1}m[d|gcd(i,j)]=\left\lfloor \frac{n}{d} \right\rfloor \left\lfloor \frac{m}{d} \right\rfloor $

莫比乌斯反演:

\(F(d)=\sum\limits_{d|k}f(k)\)

\(\therefore f(d)=\sum\limits_{d|k}\mu(\left\lfloor \frac{k}{n} \right\rfloor)F(d)\)

推式子了

\(ans=\sum\limits_{p\in prime}\sum\limits_{i=1}^n\sum\limits_{j=1}^m[gcd(i,j)=p]\)

\(f(d)=\sum\limits_{i=1}^n\sum\limits_{i=1}^m[gcd(i,j)=d]\)带进去

\(ans=\sum\limits_{p\in prime}f(p)\)

\(f(d)=\sum\limits_{d|k}\mu(\left\lfloor \frac{k}{n} \right\rfloor)F(d)\)带进去

\(ans=\sum\limits_{p\in prime}\sum\limits_{p|k}\mu(\left\lfloor \frac{k}{n} \right\rfloor)F(p)\)

不觉得\(p|k\)看着不舒服吗,也不好处理,我们换一种方式枚举\(\left\lfloor \frac{k}{n} \right\rfloor\)

\(ans=\sum\limits_{p \in prime} \sum\limits_{d=1}^{ min( \dfrac{n}{p},\frac{m}{p} ) } \mu(d) F(dp)\)

\(~~~~~=\sum\limits_{p \in prime} \sum\limits_{d=1}^{ min( \frac{n}{p},\frac{m}{p} ) } \mu(d) \left\lfloor \frac{n}{dp} \right\rfloor \left\lfloor \frac{m}{dp} \right\rfloor\)

这样做也不好分块,前面的\(p\)有点难处理,我们应该把素数想办法和莫比乌斯函数联系起来,然后丢到欧拉筛处理

\(T\)\(dp\)放到前面去

\(ans=\sum\limits_{T=1}^{min(n,m)} \sum\limits_{t|T,t \in prime} \mu(\left\lfloor \frac{T}{t} \right\rfloor) \left\lfloor \frac{n}{T} \right\rfloor \left\lfloor \frac{m}{T} \right\rfloor\)

还是不好分块吧,\(\mu\)单独就好处理了,于是我们变成这样

$ans=\sum\limits_{T=1}^{min(n,m)} \left\lfloor \frac{n}{T} \right\rfloor \left\lfloor \frac{m}{T} \right\rfloor \sum\limits_{t|T,t \in prime} \mu(\left\lfloor \frac{T}{t} \right\rfloor) $

后面的\(\sum\limits_{t|T,t \in prime} \mu(\left\lfloor \frac{T}{t} \right\rfloor)\)预处理出来,然后对前面的\(\left\lfloor \frac{n}{T} \right\rfloor \left\lfloor \frac{m}{T} \right\rfloor\)进行分块就好了

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=1e7+10;
inline int Read(){
	LL x=0,f=1; char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-') f=-1; c=getchar();
	}
	while(c>='0'&&c<='9'){
		x=(x<<3)+(x<<1)+c-'0',c=getchar();
	}
	return x*f;
}
int T;
int mu[maxn],prime[maxn];
LL g[maxn],sum[maxn];
bool visit[maxn];
inline void F_phi(LL n){
	mu[1]=1;
	int tot=0;
	for(int i=2;i<=n;++i){
		if(!visit[i]){
			prime[++tot]=i;
			mu[i]=-1;
		}
		for(int j=1;j<=tot&&i*prime[j]<=n;++j){
			visit[i*prime[j]]=true;
			if(i%prime[j]==0)
				break;
			else
			    mu[i*prime[j]]=-mu[i];
		}
	}
	for(int j=1;j<=tot;++j)
	    for(LL i=1;i*prime[j]<=n;++i)
	        g[i*prime[j]]+=(LL)mu[i];
	for(int i=1;i<=n;++i)
	    sum[i]=sum[i-1]+g[i];
}
int main(){
	T=Read();
	F_phi(maxn);
	while(T--){
		int n=Read(),m=Read();
		if(n>m)
		    swap(n,m);
		LL ans(0);
		for(int l=1,r;l<=n;l=r+1){
			r=min(n/(n/l),m/(m/l));
			ans+=(LL)(n/l)*(m/l)*(sum[r]-sum[l-1]);
		}
		printf("%lld\n",ans);
	}
}
posted @ 2019-01-08 09:41  y2823774827y  阅读(130)  评论(0编辑  收藏  举报