# Bzoj2301 [haoi2011]problem b

Posted by yjjr's blog on December 13, 2017

# 题目

Description

Input

Output

Sample Input 2

2 5 1 5 1

1 5 1 5 2

Sample Output

14

3

HINT

100%的数据满足：1≤n≤50000，1≤a≤b≤50000，1≤c≤d≤50000，1≤k≤50000

# 分析

http://blog.csdn.net/qwerty1125/article/details/78788467

# code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define mem(x,num) memset(x,num,sizeof x)
using namespace std;
inline ll read()
{
ll f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int maxn=50006;
int cnt=0,mu[maxn],sum[maxn],prime[maxn];
bool is_prime[maxn];
int cal(int n,int m)
{
if(n>m)swap(n,m);
int ans=0,pos;
for(int i=1;i<=n;i=pos+1){
pos=min(n/(n/i),m/(m/i));
ans+=(sum[pos]-sum[i-1])*(n/i)*(m/i);
}
return ans;
}

int main()
{
mu[1]=1;
rep(i,2,maxn-6){
if(!is_prime[i])prime[++cnt]=i,mu[i]=-1;
rep(j,1,cnt){
if(i*prime[j]>maxn-6)break;
is_prime[prime[j]*i]=1;
if(i%prime[j]==0){mu[i*prime[j]]=0;break;}
else mu[i*prime[j]]=-mu[i];
}
}
rep(i,1,maxn-6)sum[i]=sum[i-1]+mu[i];
int Que=read();
while(Que--){
int a=read(),b=read(),c=read(),d=read(),k=read();
a--,c--;
a/=k,b/=k,c/=k,d/=k;
printf("%d\n",cal(a,c)+cal(b,d)-cal(a,d)-cal(b,c));
}
return 0;
}