1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#include<iostream> #include<cstdio> using namespace std; long long read() { long long x=0,f=1; char c=getchar(); while(!isdigit(c)){if(c=='-') f=-1;c=getchar();} while(isdigit(c)){x=x*10+c-'0';c=getchar();} return x*f; } const int N=50000+1000; const int M=50000; int prime[N],p_cnt,mu[N]; bool noPrime[M]; void GetPrime(int n) { noPrime[1]=true,mu[1]=1; for(int i=2;i<=n;i++) { if(noPrime[i]==false) prime[++p_cnt]=i,mu[i]=-1; for(int j=1;j<=p_cnt and i*prime[j]<=n;j++) { noPrime[i*prime[j]]=true; if(i%prime[j]==0) { mu[i*prime[j]]=0; break; } mu[i*prime[j]]=mu[i]*mu[prime[j]]; } } } long long pre_mu[N]; long long GetAns(long long n,long long m,int K) { long long t_ans=0; if(n>m) swap(n,m); n/=K,m/=K; for(int l=1,r;l<=n;l=r+1) { r=min(n/(n/l),m/(m/l)); t_ans+=(pre_mu[r]-pre_mu[l-1])*(n/l)*(m/l); } return t_ans; } int main() { GetPrime(M); for(int i=1;i<=M;i++) pre_mu[i]=pre_mu[i-1]+mu[i];
int T=read(); for(;T>0;T--) { long long a=read(),b=read(),c=read(),d=read(),K=read(); long long ans=GetAns(b,d,K)-GetAns(a-1,d,K)-GetAns(b,c-1,K)+GetAns(a-1,c-1,K); printf("%lld\n",ans); }
return 0; }
|