serna37's Library

Logo

C++ アルゴリズムとデータ構造のライブラリ

View the Project on GitHub serna37/library-cpp

:heavy_check_mark: 約数列挙
(library/number/divisors.hpp)

約数列挙

できること

計算量

$O(\sqrt N)$

使い方

vector<int> divs = divisors(N);

Verified with

Code

#pragma once
vector<int> divisors(int N) {
    vector<int> res;
    for (int i = 1; i * i <= N; ++i) {
        if (N % i != 0) continue;
        res.push_back(i);
        if (N / i != i) res.push_back(N / i);
    }
    return res;
}
#line 2 "library/number/divisors.hpp"
vector<int> divisors(int N) {
    vector<int> res;
    for (int i = 1; i * i <= N; ++i) {
        if (N % i != 0) continue;
        res.push_back(i);
        if (N / i != i) res.push_back(N / i);
    }
    return res;
}
Back to top page