# 【BZOJ2301】[HAOI2011]Problem b

//by sdfzchy
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int inf=(1<<30),N=100010;
int n,m;
inline int in()
{
char ch=getchar();
int f=1,tmp=0;
while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') {tmp=(tmp<<1)+(tmp<<3)+(ch-'0');ch=getchar();}
return tmp*f;
}
int pri[N],pcnt,miu[N+10],sum[N+10];
bool ok[N+10];

void init()
{
miu[1]=1;
for(int i=2;i<=N;i++)
{
if(!ok[i]) pri[++pcnt]=i,miu[i]=-1;
for(int j=1;j<=pcnt&&(LL)i*pri[j]<=N;j++)
{
ok[i*pri[j]]=1;
if(i%pri[j]==0) {miu[i*pri[j]]=0;break;}
miu[i*pri[j]]=-miu[i];
}
}
for(int i=1;i<=N;i++) sum[i]=sum[i-1]+miu[i];
}

LL cal(int x,int y)
{
if(x>y) swap(x,y);
LL ans=0;
for(int i=1,p;i<=x;i=p+1)
{
p=min(x/(x/i),y/(y/i));
ans+=((LL )sum[p]-sum[i-1])*(x/i)*(y/i);
}
return ans;
}
int a,b,c,d,k;
int main()
{
init();
int T=in();
while(T--)
{
a=in(),c=in(),b=in(),d=in(),k=in();
printf("%lld\n",cal((a-1)/k,(b-1)/k)+cal(c/k,d/k)-cal((a-1)/k,d/k)-cal(c/k,(b-1)/k));
}
return 0;
}


©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客