Luzhiled's Library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub ei1333/library

:heavy_check_mark: Divisor (約数列挙) (math/number-theory/divisor.hpp)

与えられた整数の約数を列挙します。

divisor

vector< int64_t > divisor(int64_t n)

n の約数を昇順に返します。

制約

  • $1 \le n$

計算量

  • $O(\sqrt n)$

Verified with

Code

#pragma once

#include <algorithm>
#include <cstdint>
#include <vector>

std::vector<std::int64_t> divisor(std::int64_t n) {
  std::vector<std::int64_t> ret;
  for (std::int64_t i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n) ret.push_back(n / i);
    }
  }
  std::sort(ret.begin(), ret.end());
  return ret;
}
#line 2 "math/number-theory/divisor.hpp"

#include <algorithm>
#include <cstdint>
#include <vector>

std::vector<std::int64_t> divisor(std::int64_t n) {
  std::vector<std::int64_t> ret;
  for (std::int64_t i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n) ret.push_back(n / i);
    }
  }
  std::sort(ret.begin(), ret.end());
  return ret;
}
Back to top page