luogu2522 [HAOI2011]Problem b

参考

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long ll;
int n, mu[50005], pri[50005], cnt;
bool isp[50005];
int a, b, c, d, k;
ll q(int x, int y){
	int lst;
	ll ans=0;
	if(x>y)	swap(x, y);
	for(int i=1; i<=x; i=lst+1){
		lst = min(x/(x/i), y/(y/i));
		ans += (mu[lst] - mu[i-1]) * (ll)(x/i) * (ll)(y/i);
	}
	return ans;
}
void solv(){
	a--; c--;
	a /= k; b /= k; c /= k; d /= k;
	printf("%lld\n", q(b, d)-q(a,d)-q(b,c)+q(a,c));
}
void shai(){
	memset(isp, true, sizeof(isp));
	isp[0] = isp[1] = false;
	mu[1] = 1;
	for(int i=2; i<=50000; i++){
		if(isp[i])	pri[++cnt] = i, mu[i] = -1;
		for(int j=1; j<=cnt; j++){
			if(i*pri[j]>50000)	break;
			isp[i*pri[j]] = false;
			if(i%pri[j]==0){
				mu[i*pri[j]] = 0;
				break;
			}
			mu[i*pri[j]] = -mu[i];
		}	
	}
	for(int i=2; i<=50000; i++)	mu[i] += mu[i-1];
}
int main(){
	cin>>n;
	shai();
	while(n--){
		scanf("%d %d %d %d %d", &a, &b, &c, &d, &k);
		solv();
	}
	return 0;
}
posted @ 2018-01-25 20:23  poorpool  阅读(122)  评论(0编辑  收藏  举报