# 【HAOI2011】【BZOJ2301】Problem b（莫比乌斯反演，容斥原理）

## Solution

1. 这里要求在给定区间记数，用类似二维前缀和的东西容斥一下就好了
2. 这里的数对是有序的，有了这个条件应该更加好做了吧。

## Code

/**************************
* Au: Hany01
* Date: Jan 18th, 2018
* Prob: bzoj2301 & haoi2011
* Email: hany01@foxmail.com
**************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define fir first
#define sec second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define debug(...) fprintf(stderr, __VA_ARGS__)

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read()
{
register int _, __; register char c_;
for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}

inline void File()
{
#ifdef hany01
freopen("bzoj2301.in", "r", stdin);
freopen("bzoj2301.out", "w", stdout);
#endif
}

const int maxn = 100005;

int T, a, b, c, d, k, mu[maxn], sum[maxn], pr[maxn >> 1], np[maxn], cnt;

inline void Get_mu()
{
mu[1] = 1;
For(i, 2, maxn - 5) {
if (!np[i]) pr[++ cnt] = i, mu[i] = -1;
for (register int j = 1; j <= cnt && pr[j] * i <= maxn - 5; ++ j) {
np[i * pr[j]] = 1;
if (!(i % pr[j])) { mu[pr[j] * i] = 0; break; }
mu[pr[j] * i] = -mu[i];
}
}
For(i, 1, maxn - 5) sum[i] = sum[i - 1] + mu[i];
}

inline LL Calc(int n, int m)
{
if (n > m) swap(n, m);
if (n < 1) return 0;
register LL Ans = 0;
for (register int i = 1, j; i <= n; i = j + 1)
j = min(n / (n / i), m / (m / i)),
Ans += (n / i) * 1ll * (m / i) * 1ll * (sum[j] - sum[i - 1]);
return Ans;
}

int main()
{
File();
Get_mu();
T = read();
while (T --) {
a = read() - 1, b = read(), c = read() - 1, d = read(), k = read();
if (!k) { puts("0"); continue; }
a /= k, b /= k, c /= k, d /= k;
printf("%lld\n", Calc(b, d) - Calc(a, d) - Calc(b, c) + Calc(a, c));
}
return 0;
}
//星垂平野阔，月涌大江流。
//    -- 杜甫《旅夜书怀》

©️2019 CSDN 皮肤主题: 技术工厂 设计师: CSDN官方博客