码迷,mamicode.com
首页 > 其他好文 > 详细

【BZOJ 2301】 [HAOI2011]Problem b

时间:2015-02-18 17:42:52      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:bzoj   oi   莫比乌斯函数   

2301: [HAOI2011]Problem b

Time Limit: 50 Sec  Memory Limit: 256 MB
Submit: 1501  Solved: 628
[Submit][Status]

Description

对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数。



Input

第一行一个整数n,接下来n行每行五个整数,分别表示a、b、c、d、k

 

Output

共n行,每行一个整数表示满足要求的数对(x,y)的个数

 

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


莫比乌斯函数。


【BZOJ 1101】基本一样,只是比他多了个下限,只要把多余部分减去,多减掉的加上即可(类似容斥原理)


#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define LL long long
using namespace std;
int check[50005],mu[50005],p[50005],sum[50005];
void Getmobius()
{
	memset(check,0,sizeof(check));
	mu[1]=1;
	int tot=0;
	for (int i=2;i<=50000;i++)
	{
		if (!check[i])
		{
			p[++tot]=i;
			mu[i]=-1;
		}
		for (int j=1;j<=tot;j++)
		{
			if (i*p[j]>50000) break;
			check[i*p[j]]=1;
			if (i%p[j]==0)
			{
				mu[i*p[j]]=0;
				break;
			}
			else mu[i*p[j]]=-mu[i];
		}
	}
	sum[0]=0;
	for (int i=1;i<=50000;i++)
		sum[i]=sum[i-1]+mu[i];
}
LL Calc(int a,int b)
{	
	LL ans=0LL;
	int pos;
	for (int d=1;d<=min(a,b);d=pos+1)
	{
		pos=min(a/(a/d),b/(b/d));
		ans+=(LL)(sum[pos]-sum[d-1])*(a/d)*(b/d);
	}
	return ans;
}
int main()
{
        Getmobius();
	int T;
	scanf("%d",&T);
	while (T--)
	{
		int a,b,c,d,k;
		scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
		a=(a-1)/k,b/=k,c=(c-1)/k,d/=k;
		LL ans=0LL;
		ans=Calc(b,d)-Calc(a,d)-Calc(c,b)+Calc(a,c);
		printf("%lld\n",ans);
	}
	return 0;
}
技术分享

【BZOJ 2301】 [HAOI2011]Problem b

标签:bzoj   oi   莫比乌斯函数   

原文地址:http://blog.csdn.net/regina8023/article/details/43877233

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!