http://blog.csdn.net/weixinding/article/details/7621103
http://blog.csdn.net/z690933166/article/category/1349672
莫比乌斯反演+容斥原理,好神奇!
#include<map>
#include<stack>
#include<queue>
#include<ctime>
#include<cmath>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<utility>
#include<iostream>
#include<algorithm>
const int SIZE = 1e7+5;
int mu[SIZE], g[SIZE], prime[SIZE>>4], tot;
bool check[SIZE]; int sum[SIZE];
void PreWork()
{
sum[1] = g[1] = 0, mu[1] = 1;
for(int i = 2; i < SIZE; i++)
{
if(!check[i]) prime[++tot] = i, mu[i] = -1, g[i] = 1;
sum[i] = sum[i-1] + g[i];
for(int j = 1, t; j <= tot && (t = prime[j]*i) < SIZE; j++)
{
check[t] = true;
if(i%prime[j]) mu[t] = -mu[i], g[t] = mu[i] - g[i];
else {mu[t] = 0, g[t] = mu[i]; break;}
}
}
}
long long Count(int p,int q)
{
long long ret = 0;
if(p > q) std::swap(p,q);
for(int i = 1, next = 0; i <= p; i = next+1)
{
next = std::min(p/(p/i), q/(q/i));
ret += (long long)(sum[next]-sum[i-1])*(p/i)*(q/i);
}
return ret;
}
int main()
{
int T;
#ifndef ONLINE_JUDGE
freopen("bzoj2820.in","r",stdin);
freopen("bzoj2820.out","w",stdout);
#endif
scanf("%d",&T);
PreWork();
while(T--)
{
int m, n;long long ans;
scanf("%d%d",&m,&n);
printf("%lld\n",Count(m,n));
}
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
return 0;
#endif
}
版权声明:本文为博主原创文章,未经博主允许不得转载。