aboutsummaryrefslogtreecommitdiff
path: root/07_mathematics/counting_divisors_1713.cpp
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2026-07-06 19:08:08 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2026-07-06 19:08:08 +0200
commit96254947699986c59f0dc63d69fd4b76bd3ed43e (patch)
tree6c4dca945d7f7427c48be234d827fe4d33be02c5 /07_mathematics/counting_divisors_1713.cpp
downloadcses-96254947699986c59f0dc63d69fd4b76bd3ed43e.tar.gz
cses-96254947699986c59f0dc63d69fd4b76bd3ed43e.zip
Initial commit
Diffstat (limited to '')
-rw-r--r--07_mathematics/counting_divisors_1713.cpp34
1 files changed, 34 insertions, 0 deletions
diff --git a/07_mathematics/counting_divisors_1713.cpp b/07_mathematics/counting_divisors_1713.cpp
new file mode 100644
index 0000000..fe30ab4
--- /dev/null
+++ b/07_mathematics/counting_divisors_1713.cpp
@@ -0,0 +1,34 @@
1#include <array>
2#include <iostream>
3
4constexpr size_t max = 1000001;
5std::array<size_t, max> spf; // Smallest prime factor of i
6
7size_t ndiv(size_t x) {
8 size_t n{1}, d{0}, e{0};
9 for (size_t i = x; i > 1; i /= spf[i]) {
10 if (spf[i] != d) {
11 n *= e+1;
12 d = spf[i];
13 e = 1;
14 } else e++;
15 }
16 return n * (e+1);
17}
18
19int main() {
20 for (size_t i = 2; i < max; i++) {
21 if (spf[i] != 0) continue;
22 spf[i] = i;
23 for (size_t j = 2; i*j < max; j++)
24 if (spf[i*j] == 0)
25 spf[i*j] = i;
26 }
27
28 size_t n, x;
29 std::cin >> n;
30 for (size_t i = 0; i < n; i++) {
31 std::cin >> x;
32 std::cout << ndiv(x) << "\n";
33 }
34}

Generated with cgit - Back to sebastiano.tronto.net