bzoj2301[HAOI2011]Problem b
[传送门]([HAOI2011]Problem b)
这个题比起[POI2007]ZAP-Queries 固定了左端点,我们可以把它画成一个二维的平面
那么$$ans=solve((1,b),(1,d))-solve((1,b),(1,c-1))-solve((1,a-1),(1,d))+solve((1,a-1),(1,c-1))$$
solve部分的推导可以参考[POI2007]ZAP-Queries
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
void read(int &x) {
char ch; bool ok;
for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
#define rg register
const int maxn=5e4;
int n,a,b,c,d,k,mu[maxn+1],tot,pri[maxn+1];bool vis[maxn+1];
void prepare()
{
mu[1]=1;
for(rg int i=2;i<=maxn;i++)
{
if(!vis[i])pri[++tot]=i,mu[i]=-1;
for(rg int j=1;j<=tot&&pri[j]*i<=maxn;j++)
{
vis[pri[j]*i]=1;
if(i%pri[j])mu[pri[j]*i]=-mu[i];
else {mu[pri[j]*i]=0;break;}
}
}
for(rg int i=1;i<=maxn;i++)mu[i]+=mu[i-1];
}
int solve(int n,int m)
{
long long ans=0;
if(n>m)swap(n,m);
n=n/k,m=m/k;
for(rg int i=1,j;i<=n;i=j+1)
{
j=min(n/(n/i),m/(m/i));
ans+=1ll*(mu[j]-mu[i-1])*(n/i)*(m/i);
}
return ans;
}
int main()
{
read(n),prepare();
while(n--)
{
read(a),read(b),read(c),read(d),read(k);
printf("%d\n",solve(b,d)-solve(c-1,b)-solve(a-1,d)+solve(a-1,c-1));
}
}