bzoj2820: YY的GCD

传送门:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2820

思路:





#include<cstdio>
#include<cstring>
#include<algorithm>
const int N=10000010;
using namespace std;
typedef long long ll;
int mu[N+10],g[N+10],pri[N+10],tot,cas,qn[10010],qm[10010],maxn=0;ll n,m,ans,sum[N+10];bool isp[N+10];

void prework(){
	memset(isp,1,sizeof(isp));
	mu[1]=1,g[1]=0,isp[1]=0;
	for (int i=2;i<=maxn;i++){
		if (isp[i]) mu[i]=-1,g[i]=1,pri[++tot]=i;
		for (int j=1;j<=tot&&pri[j]*i<=maxn;j++){
			isp[i*pri[j]]=0;
			if (!(i%pri[j])){mu[i*pri[j]]=0,g[i*pri[j]]=mu[i];break;}
			mu[i*pri[j]]=-mu[i],g[i*pri[j]]=mu[i]-g[i];
		}
	}
	for (int i=1;i<=N;i++) sum[i]=sum[i-1]+g[i];
	//for (int i=1;i<=50;i++) printf("%d %d\n",i,mu[i]);
}

int main(){
	scanf("%d",&cas);
	for (int i=1;i<=cas;i++)
		scanf("%d%d",&qn[i],&qm[i]),maxn=max(maxn,qn[i]),maxn=max(maxn,qm[i]);
	prework();
	for (int i=1;i<=cas;i++){
		n=qn[i],m=qm[i],ans=0;
		if (n>m) swap(n,m);
		for (int j=1,ed;j<=n;j=ed+1){
			ed=min(n/(n/j),m/(m/j));
			ans+=1ll*(n/j)*(m/j)*(sum[ed]-sum[j-1]);
		}
		printf("%lld\n",ans);
	}
	return 0;
}


posted @ 2015-10-13 11:25  orzpps  阅读(110)  评论(0编辑  收藏  举报