解析：

\begin{aligned} &\sum_{i=1}^{a-1}\sum_{j=1}^{c-1}[gcd(i,j)=k]+\sum_{i=1}^b\sum_{j=1}^d[gcd(i,j)=k]\\ -&\sum_{i=1}^{a-1}\sum_{j=1}^d[gcd(i,j)=k]-\sum_{i=1}^b\sum_{j=1}^{c-1}[gcd(i,j)=k] \end{aligned}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define re register
#define gc get_char
#define pc putchar
#define cs const

namespace IO{
namespace IOONLY{
cs int Rlen=1<<18|1;
char buf[Rlen],*p1,*p2;
}
inline char get_char(){
using namespace IOONLY;
}

inline int getint(){
re int num;
re char c;
while(!isdigit(c=gc()));num=c^48;
while(isdigit(c=gc()))num=(num+(num<<2)<<1)+(c^48);
return num;
}
inline void outint(ll a){
static char ch[23];
if(a==0)pc('0');
while(a)ch[++ch[0]]=a-a/10*10,a/=10;
while(ch[0])pc(ch[ch[0]--]^48);
}
}
using namespace IO;

cs int P=50004;

int prime[P],pcnt,mu[P];
bool mark[P];

inline void linear_sieves(int len=P-4){
mu[1]=1;
for(int re i=2;i<=len;++i){
if(!mark[i])prime[++pcnt]=i,mu[i]=-1;
for(int re j=1;i*prime[j]<=len;++j){
mark[i*prime[j]]=true;
if(i%prime[j]==0)break;
mu[i*prime[j]]=-mu[i];
}
mu[i]+=mu[i-1];
}
}

inline ll calc(int n,int m){
ll ans=0;
if(n>m)swap(n,m);
for(int re i=1,j;i<=n;i=j+1){
j=min(n/(n/i),m/(m/i));
ans+=(ll)(mu[j]-mu[i-1])*(n/i)*(m/i);
}
return ans;
}

signed main(){
linear_sieves();
for(int re T=getint();T--;){
int a=getint(),b=getint(),c=getint(),d=getint(),k=getint();
a=(a-1)/k;b/=k;c=(c-1)/k;d/=k;
outint(calc(b,d)+calc(a,c)-calc(a,d)-calc(b,c));pc('\n');
}
return 0;
}


©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客