# bzoj 2301

$\sum_{d=1}^{[n/k]}\mu(d)[n/kd][m/kd]$（注意这里的上界应该是n/k,m/k中较小者）

#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#define ll long long
using namespace std;
int T;
int pri[50005];
bool used[50005];
int miu[50005];
int smiu[50005];
int cnt=0;
int a,b,c,d,k;
void init()
{
miu[1]=1;
for(int i=2;i<=50000;i++)
{
if(!used[i])
{
pri[++cnt]=i;
miu[i]=-1;
}
for(int j=1;j<=cnt&&i*pri[j]<=50000;j++)
{
used[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<=50000;i++)
{
smiu[i]=smiu[i-1]+miu[i];
}
}
ll solve(ll x,ll y)
{
ll ans=0;
if(x>y)
{
swap(x,y);
}
x/=k,y/=k;
int last=0;
for(int i=1;i<=x;i=last+1)
{
last=min(x/(x/i),y/(y/i));
ans+=(smiu[last]-smiu[i-1])*(x/i)*(y/i);
}
return ans;
}
int main()
{
scanf("%d",&T);
init();
while(T--)
{
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
printf("%lld\n",solve(b,d)-solve(a-1,d)-solve(b,c-1)+solve(a-1,c-1));
}
return 0;
}

posted @ 2019-04-08 16:29  lleozhang  Views(...)  Comments(...Edit  收藏
levels of contents