# 题目

$a\leq x\leq b,c\leq y\leq d$$gcd(x,y)=k$的个数

# 分析

$TotalAns=Ans(b,d)-Ans(a-1,d)-Ans(b,c-1)+Ans(a-1,c-1)$

$f(k)=\sum_{i=1}^{n}\sum_{j=1}^{m} [gcd(i,j)=k]$
$F(d)=\sum_{k|d}f(k)=\lfloor\frac{n}{d}\rfloor\lfloor\frac{m}{d}\rfloor$

$f(k)=\sum_{k|d}\mu(\lfloor\frac{d}{k}\rfloor)\times F(d)$
$Ans=\sum_{k|d}\mu(\lfloor\frac{d}{k}\rfloor)\times F(d)$

$Ans=\sum_{i=1}^{min\{n,m\}}\mu(i)\times\lfloor\frac{n}{i*d}\rfloor\times\lfloor\frac{m}{i*d}\rfloor$

# 代码

#include <cstdio>
#define rr register
#define min(a,b) ((a)<(b))?(a):(b)
#define N 50000
using namespace std;
typedef unsigned uit;
uit cnt,mu[N+1],v[N+1],prime[N+1];
inline signed iut(){
rr uit ans=0; rr char c=getchar();
while (c<48||c>57) c=getchar();
while (c>47&&c<58) ans=(ans<<3)+(ans<<1)+c-48,c=getchar();
return ans;
}
inline void iiiii(){//线性筛莫比乌斯函数
mu[1]=1;
for (rr uit i=2;i<=N;++i){
if (!v[i]) v[i]=i,prime[++cnt]=i,mu[i]=-1;
for (rr uit j=1;j<=cnt&&prime[j]*i<=N;++j){
v[i*prime[j]]=prime[j];
if (i%prime[j]==0) break;
mu[i*prime[j]]=-mu[i];
}
}
for (rr uit i=2;i<=N;++i) mu[i]+=mu[i-1];
}
inline void print(uit ans){
if (ans>9) print(ans/10);
putchar(ans%10+48);
}
inline signed answ(uit n,uit m,uit k){
rr uit ans=0,t=min(n,m);
for (rr uit l=1,r;l<=t;l=r+1){
r=min(n/(n/l),m/(m/l));//确定区间
ans+=(n/l/k)*(m/l/k)*(mu[r]-mu[l-1]);//计算答案
}
return ans;
}
signed main(){
iiiii();
for (rr uit t=iut();t;--t){
rr uit a=iut(),b=iut(),c=iut(),d=iut(),k=iut();
rr uit ans=answ(b,d,k)+answ(a-1,c-1,k)-answ(b,c-1,k)-answ(a-1,d,k);
if (ans) print(ans); else putchar(48); putchar(10);
}
return 0;
}


©️2019 CSDN 皮肤主题: Age of Ai 设计师: meimeiellie