-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem0215.h
More file actions
70 lines (64 loc) · 1.87 KB
/
Problem0215.h
File metadata and controls
70 lines (64 loc) · 1.87 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//
// Created by Fengwei Zhang on 1/17/22.
//
#ifndef ACWINGSOLUTION_PROBLEM0215_H
#define ACWINGSOLUTION_PROBLEM0215_H
#include <iostream>
#include <cstring>
using namespace std;
class Problem0215 {
// 利用整数除法的性质:https://www.acwing.com/solution/content/17858/
private:
void GetMobius(const int n, int mobius[]) {
int primes[n + 1];
int p_cnt = 0;
bool filtered[n + 1];
memset(filtered, 0, sizeof filtered);
mobius[1] = 1;
for (int f = 2; f <= n; ++f) {
if (!filtered[f]) {
primes[p_cnt] = f;
mobius[f] = -1;
++p_cnt;
}
for (int i = 0; i < p_cnt && primes[i] <= n / f; ++i) {
auto t = primes[i] * f;
filtered[t] = true;
if (f % primes[i]) {
mobius[t] = mobius[f] * -1;
} else {
mobius[t] = 0;
break;
}
}
}
}
int main() {
const int N = 50000;
int mobius[N + 1];
memset(mobius, 0, sizeof mobius);
GetMobius(N, mobius);
int prefix[N + 1];
memset(prefix, 0, sizeof prefix);
for (int i = 1; i <= N; ++i) {
prefix[i] = prefix[i - 1] + mobius[i];
}
int t;
scanf("%d", &t);
while (t--) {
int a, b, d;
scanf("%d%d%d", &a, &b, &d);
a /= d;
b /= d;
auto n = min(a, b);
long long res = (long long) a * b;
for (int l = 2, r; l <= n; l = r + 1) {
r = min(n, min(a / (a / l), b / (b / l)));
res += (prefix[r] - prefix[l - 1]) * ((long long) (a / l) * (b / l));
}
printf("%lld\n", res);
}
return 0;
}
};
#endif //ACWINGSOLUTION_PROBLEM0215_H