传送门:Primes in GCD Table

题意:给定两个数SPOJ PGCD(莫比乌斯反演)-LMLPHPSPOJ PGCD(莫比乌斯反演)-LMLPHP,其中SPOJ PGCD(莫比乌斯反演)-LMLPHPSPOJ PGCD(莫比乌斯反演)-LMLPHP,求SPOJ PGCD(莫比乌斯反演)-LMLPHP为质数的SPOJ PGCD(莫比乌斯反演)-LMLPHP有多少对?其中SPOJ PGCD(莫比乌斯反演)-LMLPHPSPOJ PGCD(莫比乌斯反演)-LMLPHP的范围是SPOJ PGCD(莫比乌斯反演)-LMLPHP

分析:这题不能枚举质数来进行莫比乌斯反演,得预处理出∑υ(n/p)(n%p==0).

#pragma comment(linker,"/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <limits.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 100000000
#define inf 0x3f3f3f3f
#define eps 1e-6
#define N 10000000
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PII pair<int,int>
using namespace std;
inline int read()
{
char ch=getchar();int x=,f=;
while(ch>''||ch<''){if(ch=='-')f=-;ch=getchar();}
while(ch<=''&&ch>=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
bool vis[N+];
int mu[N+],prime[N+],sum[N+],num[N+];
void Mobius()
{
memset(vis,false,sizeof(vis));
mu[]=;
int tot=;
for(int i=;i<=N;i++)
{
if(!vis[i])
{
prime[tot++]=i;
mu[i]=-;
}
for(int j=;j<tot;j++)
{
if(i*prime[j]>N)break;
vis[i*prime[j]]=true;
if(i%prime[j]==)
{
mu[i*prime[j]]=;
break;
}
else
{
mu[i*prime[j]]=-mu[i];
}
}
}
for(int i=;i<tot;i++)
for(int j=prime[i];j<=N;j+=prime[i])
num[j]+=mu[j/prime[i]];//预处理出对于所有质数p,sigma(f(p))对应的F(i)的系数,用num[i]表示
for(int i=;i<=N;i++)sum[i]=sum[i-]+num[i];
}
LL solve(int n,int m)
{
LL res=;
if(n>m)swap(n,m);
for(int i=,last=;i<=n;i=last+)
{
last=min(n/(n/i),m/(m/i));
res+=(LL)(sum[last]-sum[i-])*(n/i)*(m/i);
}
return res;
} int main()
{
int T,n,m;
Mobius();
T=read();
while(T--)
{
n=read();m=read();
LL ans=solve(n,m);
printf("%lld\n",ans);
}
}
05-08 15:26