# BZOJ 2301 Problem b（莫比乌斯反演+容斥）

﻿﻿

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<ctime>
#define eps 1e-6
#define LL long long
#define pii pair<int, int>
using namespace std;
const int MAXN = 50050;
bool check[MAXN+10];
int prime[MAXN+10];
int mu[MAXN+10];
int Sum[MAXN+10];
void Moblus()
{
memset(check,false,sizeof(check));
mu[1] = 1;
int tot = 0;
for(int i = 2; i <= MAXN; i++)
{
if( !check[i] )
{
prime[tot++] = i;
mu[i] = -1;
}
for(int j = 0; j < tot; j++)
{
if(i * prime[j] > MAXN) break;
check[i * prime[j]] = true;
if( i % prime[j] == 0)
{
mu[i * prime[j]] = 0;
break;
}
else
{
mu[i * prime[j]] = -mu[i];
}
}
}
}
LL solve(int a, int b, int k) {
if(a > b) swap(a, b);
a /= k, b /= k;
LL ans = 0;
int last;
for(int i = 1; i <= a; i = last+1) {
last = min(a/(a/i), b/(b/i));
ans += (LL)(Sum[last]-Sum[i-1])*(a/i)*(b/i);
}
return ans;
}
int main() {
//freopen("input.txt", "r", stdin);
int a, b, c, d, k;
int T; scanf("%d", &T);
Moblus();
for(int i=1;i<MAXN;i++)
Sum[i]=Sum[i-1]+mu[i];
while(T--) {
scanf("%d%d%d%d%d", &a, &b, &c, &d, &k);
LL ans = solve(b, d, k)+solve(a-1, c-1, k)-solve(a-1, d, k)-solve(c-1, b, k);
printf("%lld\n", ans);
}
return 0;
}



©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客