Johnny has created a table which encodes the results of some operation -- a function of two arguments. But instead of a boring multiplication table of the sort you learn by heart at prep-school, he has created a GCD (greatest common divisor) table! So he now has a table (of height a and width b), indexed from (1,1) to (a,b), and with the value of field (i,j) equal to gcd(i,j). He wants to know how many times he has used prime numbers when writing the table.
Input
First, t ≤ 10, the number of test cases. Each test case consists of two integers, 1 ≤ a,b < 107.
Output
For each test case write one number - the number of prime numbers Johnny wrote in that test case.
Example
Input: 2 10 10 100 100 Output: 30 2791
莫比乌斯的理解见 传送门
题意:
要求求出gcd(x , y)为质数的个数,x ,y的区间见题
题解:
令 f(p)=gcd(x,y)为p (p为质数)
F(pk)=gcd(x,y)>=p (k为质数)
公式借鉴 传送门
直接这样做会TLE,那么考虑优化,令t=pk
我们便可以将 a[t]=∑p|tμ(tp) 预处理出来,再对其求前缀和,然后对其分块求解
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define MAXN 10000000
#define LL long long
bool check[MAXN+10];
int primer[MAXN+10];
int mu[MAXN+10];
int sum[MAXN+10];
void Moblus()
{
memset(check,0,sizeof(check));
mu[1]=1;
int tot=0;
for(int i=2;i<=MAXN;i++){
if(!check[i]){
primer[tot++]=i;
mu[i]=-1;
}
for(int j=0;j<tot&&i*primer[j]<=MAXN;j++){
check[i*primer[j]]=true;
if(i%primer[j]==0){
mu[i*primer[j]]=0;
break;
}
mu[i*primer[j]]=-mu[i];
}
}
///
sum[0]=0;
for(int i=0;i<tot;i++)
for(int j=primer[i];j<MAXN;j+=primer[i])
sum[j]+=mu[j/primer[i]];
for(int i=1;i<MAXN;i++)
sum[i]+=sum[i-1];
}
LL solve(int n,int m)
{
LL ans=0;
if(n>m) swap(n,m);
for(int i=1,last;i<=n;i=last+1){
last=min(n/(n/i),m/(m/i));
ans+=(LL)(sum[last]-sum[i-1])*(n/i)*(m/i);
}
return ans;
}
int main()
{
int T;
Moblus();
int a,b;
freopen("in.txt","r",stdin);
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&a,&b);
LL ans=solve(a,b);
printf("%lld\n",ans);
}
return 0;
}