莫比乌斯反演;
形式一:
F(n)=∑d|nf(d)=>f(n)=∑d|nμ(d)F(nd)
形式二:
F(n)=∑n|df(d)=>f(n)=∑n|dμ(dn)F(d)
题目大意:求在a<=x<=b,c<=y<=d,满足gcd(x,y)是k的(x,y)的对数;
题目解析:满足gcd(x,y)是k的(x,y)的对数也等价于1<=x<=n/k,1<=y<=m/k,(x,y)互质的对数,
令f(i)表示满足gcd(x,y)=i时(x,y)的对数,F(i)表示满足i|gcd(x,y)的(x,y)的对数,
F(i)=∑i|df(d)=>f(i)=∑i|dμ(di)F(d)=∑i|dμ(di)⌊nd⌋⌊md⌋
接着我们利用分块的思想,预处理出莫比乌斯函数的前缀和;
AC代码:
#include
#include
using namespace std;
#define MAXN 50000
int a,b,c,d,k,p[MAXN+10],pcnt,mu[MAXN+10],sum[MAXN+10],ans,n;
bool f[MAXN+10];
void Read(int &x){
char c;
while(c=getchar(),c!=EOF)
if(c>='0'&&c<='9'){
x=c-'0';
while(c=getchar(),c>='0'&&c<='9')
x=x*10+c-'0';
ungetc(c,stdin);
return;
}
}
void prepare(){
int i,j;
mu[1]=sum[1]=1;
for(i=2;i<=MAXN;i++){
if(!f[i])
p[++pcnt]=i,mu[i]=-1;
for(j=1;p[j]*i<=MAXN;j++){
f[p[j]*i]=1;
if(i%p[j]==0){
mu[p[j]*i]=0;
break;
}
mu[p[j]*i]=-mu[i];
}
sum[i]=sum[i-1]+mu[i];
}
}
int cal(int n,int m){
int t=min(m,n),last,ret=0,i;
for(i=1;i<=t;i=last+1){
last=min(n/(n/i),m/(m/i));
ret+=(sum[last]-sum[i-1])*(n/i)*(m/i);
}
return ret;
}
void solve(int a,int b,int c,int d,int k){
a--,c--;
a/=k,b/=k,c/=k,d/=k;
ans=cal(b,d)-cal(a,d)-cal(b,c)+cal(a,c);
}
int main()
{
Read(n);
prepare();
while(n--){
Read(a),Read(b),Read(c),Read(d),Read(k);
solve(a,b,c,d,k);
printf("%d\n",ans);
}
}