1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| #include <cstdio> #include <algorithm> using namespace std; typedef long long LL; const int N = 1e6 + 10; bool vis[N]; int cnt, primes[N], mu[N]; LL s[N]; void init() { mu[1] = 1; for (int i = 2; i < N; i++) { if (!vis[i]) { primes[++cnt] = i; mu[i] = -1; } for (int j = 1; j <= cnt && i * primes[j] < N; j++) { vis[primes[j] * i] = true; if (i % primes[j] == 0) { mu[primes[j] * i] = 0; break; } mu[primes[j] * i] = -mu[i]; } } for (int i = 1; i < N; i++) s[i] = s[i - 1] + mu[i]; } LL f(int n, int m, int k) { n /= k, m /= k; LL res = 0; for (int l = 1, r; l <= min(n, m); l = r + 1) { r = min(n / (n / l), m / (m / l)); res += (s[r] - s[l - 1]) * (LL)(n / l) * (LL)(m / l); } return res; } int main() { init(); int T; scanf("%d", &T); while (T--) { int a, b, c, d, k; scanf("%d%d%d%d%d", &a, &b, &c, &d, &k); printf("%lld\n", f(b, d, k) - f(a - 1, d, k) - f(b, c - 1, k) + f(a - 1, c - 1, k)); } return 0; }
|