BZOJ 2820: YY的GCD | 数论
题目:
题解:
#include<cstdio> #include<algorithm> #define N 10000005 typedef long long ll; using namespace std; int T,n,m,cnt; bool mark[N]; int pri[N],mu[N]; ll f[N]; void getphi() { mu[1]=1; for (int i=2;i<N;i++) { if (!mark[i]) pri[++cnt]=i,mu[i]=-1; for (int j=1;j<=cnt && pri[j]*i<N;j++) { mark[i*pri[j]]=1; if (i%pri[j]==0) {mu[i*pri[j]]=0;break;} else mu[i*pri[j]]=-mu[i]; } } for (int i=1;i<=cnt;i++) { int p=pri[i]; for (int j=1;j*p<N;j++) f[j*p]+=mu[j]; } for (int i=1;i<N;i++) f[i]+=f[i-1]; } int main() { getphi(); scanf("%d",&T); while (T--) { ll ans=0; scanf("%d%d",&n,&m); if (n>m) swap(n,m); for (int i=1,j;i<=n;i=j+1) { j=min(n/(n/i),m/(m/i)); ans+=(f[j]-f[i-1])*(n/i)*(m/i); } printf("%lld\n",ans); } return 0; }