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
#pragma once


#include <cassert><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <vector><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <ranges><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <algorithm><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.


#include "numeric/internal/factorize.hpp"
#include "numeric/modular/modint_interface.hpp"


namespace uni {

namespace internal {


//Thanks to: https://github.com/NyaanNyaan/library/blob/master/prime/fast-factorize.hpp
template<modint_family Small, modint_family Large, class R>
void divisors(const u64 n, R *const res) noexcept(NO_EXCEPT) {
    if(n == 0) return;

    std::vector<u64> facts; factorize<Small, Large>(n, &facts);
    std::ranges::sort(facts);

    std::vector<std::pair<u64, int>> v;
    for(auto &p : facts) {
        if(v.empty() || v.back().first != p) {
            v.emplace_back(p, 1);
        } else {
            v.back().second++;
        }
    }

    using value_type = std::ranges::range_value_t<R>;

    const auto size = std::ranges::ssize(v);
    auto f = [&](auto rc, int i, value_type x) noexcept(NO_EXCEPT) -> void {
        if(i == size) {
            res->push_back(x);
            return;
        }
        for(int j = v[i].second; ; --j) {
            rc(rc, i + 1, x);
            if(j == 0) break;
            x *= static_cast<value_type>(v[i].first);
        }
    };
    f(f, 0, 1);
}


} // namespace internal

} // namespace uni