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

题目

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

这题解法较多,都有值得学习的部分

解法一

\(Ans=\sum_{i=1}^{n}\sum_{j=1}^{m}\frac{ij}{gcd(i,j)}\)

思考把\(\sum_{d|n}\mu(d)=[n=1]\)带进去

\(Ans=\sum_{d=1}^{min(n,m)}\sum_{i=1}^{n}\sum_{j=1}^{m}[gcd(i,j)=d]\frac{ij}{d}\)

\(Ans=\sum_{d=1}^{min(n,m)}d\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}\sum_{j=1}^{\lfloor\frac{m}{d}\rfloor}[gcd(i,j)=1]ij\)

至此,我们把式变成:

\(Ans=\sum_{d=1}^{min(n,m)}d\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}\sum_{j=1}^{\lfloor\frac{m}{d}\rfloor}\sum_{x|gcd(i,j)}\mu(x)ij\)

我们来枚举\(x\)消掉\(\sum_{x|gcd(i,j)}\)

\(Ans=\sum_{d=1}^{min(n,m)}d\sum_{x=1}^{min(\lfloor\frac{n}{d}\rfloor,\lfloor\frac{m}{d}\rfloor)}\mu(x)\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}\sum_{j=1}^{\lfloor\frac{m}{d}\rfloor}ij[x|gcd(i,j)]\)

对于判断\([x|gcd(i,j)]\)也要消掉:

\(Ans=\sum_{d=1}^{min(n,m)}d\sum_{x=1}^{min(\lfloor\frac{n}{d}\rfloor,\lfloor\frac{m}{d}\rfloor)}\mu(x)\sum_{xu=1}^{\lfloor\frac{n}{d}\rfloor}\sum_{xv=1}^{\lfloor\frac{m}{d}\rfloor}x^2uv\)

最后要处理的式子:

\(Ans=\sum_{d=1}^{min(n,m)}d\sum_{x=1}^{min(\lfloor\frac{n}{d}\rfloor,\lfloor\frac{m}{d}\rfloor)}x^2\mu(x)(\sum_{u=1}^{\lfloor\frac{n}{dx}\rfloor}u)(\sum_{v=1}^{\lfloor\frac{m}{dx}\rfloor}v)\)

直接分块,另\((\sum_{u=1}^{\lfloor\frac{n}{dx}\rfloor}u)(\sum_{v=1}^{\lfloor\frac{m}{dx}\rfloor}v)\)相同

解法二

\(\begin{aligned} Ans &= \sum_{d=1}^n\sum_{i=1}^{n}\sum_{j=1}^{m}\frac{ij}{d}[gcd(i,j)=d] \\ &= \sum_{d=1}^nd\sum_{i=1}^{\lfloor \frac{n}{d} \rfloor }\sum_{j=1}^{\lfloor \frac{m}{d} \rfloor }ij[gcd(i,j)==1] \\ & = \sum_{d=1}^nd\sum_{i=1}^{\lfloor \frac{n}{d} \rfloor }\sum_{j=1}^{\lfloor \frac{m}{d} \rfloor }ij\sum_{t|gcd(i,j)}\mu(t) \\ & = \sum_{d=1}^nd\sum_{t=1}^{\lfloor \frac{n}{d} \rfloor}\mu(t)\sum_{i=1}^{\frac{n}{dt}}it\sum_{j=1}^{\frac{m}{dt}}jt \\ & = \sum_{d=1}^nd\sum_{t=1}^{\lfloor \frac{n}{d} \rfloor}t^2\mu(t)\sum_{i=1}^{\frac{n}{dt}}i\sum_{j=1}^{\frac{m}{dt}}j \\ \end{aligned}\)

由于\(\sum_{i=1}^{\frac{n}{dt}}i\sum_{j=1}^{\frac{m}{dt}}j\)可以分块,所以枚举\(dt\)

\(\sum_{T=1}^n \sum_{i=1}^{\frac{n}{T}}i\sum_{j=1}^{\frac{m}{T}} \rfloor)T\sum_{t|T}t\mu(t)\)

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const LL p=20101009;
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 n,m;
LL ans;
int mu[maxn],prime[maxn];
LL sum[maxn];
bool visit[maxn];
inline void F_phi(LL max_n){
	mu[1]=1;
	int tot=0;
	for(int i=2;i<=max_n;++i){
		if(!visit[i]){
		    prime[++tot]=i,
			mu[i]=-1;
		}
		for(int j=1;j<=tot&&i*prime[j]<=max_n;++j){
			visit[i*prime[j]]=true;
			if(i%prime[j]==0)
			    break;
			else
			    mu[i*prime[j]]=-mu[i];
		}
	}
	for(int i=1;i<=max_n;++i)
	    sum[i]=(sum[i-1]+(LL)mu[i]*i%p*i%p)%p;
}
int main(){
	scanf("%d%d",&n,&m);
	int N=min(n,m);
	F_phi(N);
	for(int d=1;d<=N;++d){
		int maxx=n/d,maxy=m/d;
		int x=min(maxx,maxy);
		LL num=0;
		for(int l=1,r;l<=x;l=r+1){
			r=min(maxx/(maxx/l),maxy/(maxy/l));
			num=(num+
			((sum[r]-sum[l-1]+p)%p)*
			((((1ll+maxx/l)%p)*1ll*(maxx/l)/2%p)%p)%p*
			((((1ll+maxy/l)%p)*1ll*(maxy/l)/2%p)%p)%p)%p;
		}
		ans=(ans+(num*1ll*d)%p)%p;
	}
	printf("%lld",ans);
	return 0;
}/*
1000000 1000000
9002207
*/
posted @ 2019-01-05 14:05  y2823774827y  阅读(177)  评论(0编辑  收藏  举报