# bzoj 2301: [HAOI2011]Problem b

Description

Input

Output

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define RG register
#define il inline
#define iter iterator
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
const int N=50005;
int num=0,prime[N],phi[N],sum[N];bool vis[N];
void prework(){
int to;phi[1]=1;
for(int i=2;i<N;i++){
if(!vis[i]){
prime[++num]=i;
phi[i]=-1;
}
for(int j=1;j<=num && prime[j]*i<N;j++){
to=prime[j]*i;vis[to]=true;
if(i%prime[j])phi[to]=-phi[i];
else{
phi[to]=0;
break;
}
}
}
for(int i=1;i<N;i++)sum[i]=sum[i-1]+phi[i];
}
ll solve(int a,int b,int k){
a/=k;b/=k;if(a>b)swap(a,b);
ll ret=0,nxt;
for(int i=1;i<=a;i=nxt+1){
nxt=Min(a/(a/i),b/(b/i));
ret+=(ll)(a/i)*(b/i)*(sum[nxt]-sum[i-1]);
}
return ret;
}
void work()
{
int a,b,c,d,k;
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
ll ans=solve(b,d,k)-solve(a-1,d,k)-solve(b,c-1,k)+solve(a-1,c-1,k);
printf("%lld\n",ans);
}

int main()
{
int T;cin>>T;
prework();
while(T--)work();
return 0;
}


posted @ 2017-09-02 01:18  PIPIBoss  阅读(...)  评论(...编辑  收藏