diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2026-07-06 19:08:08 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2026-07-06 19:08:08 +0200 |
| commit | 96254947699986c59f0dc63d69fd4b76bd3ed43e (patch) | |
| tree | 6c4dca945d7f7427c48be234d827fe4d33be02c5 /07_mathematics/counting_divisors_1713.cpp | |
| download | cses-96254947699986c59f0dc63d69fd4b76bd3ed43e.tar.gz cses-96254947699986c59f0dc63d69fd4b76bd3ed43e.zip | |
Initial commit
Diffstat (limited to '')
| -rw-r--r-- | 07_mathematics/counting_divisors_1713.cpp | 34 |
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 | |||
| 4 | constexpr size_t max = 1000001; | ||
| 5 | std::array<size_t, max> spf; // Smallest prime factor of i | ||
| 6 | |||
| 7 | size_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 | |||
| 19 | int 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 | } | ||
