From a5ca555f2decd8b1a587f65d7f3ecd6fe43ab349 Mon Sep 17 00:00:00 2001 From: hitonanode <32937551+hitonanode@users.noreply.github.com> Date: Tue, 26 Dec 2023 21:19:38 +0900 Subject: [PATCH] quotients --- utilities/quotients.hpp | 26 ++++++++++++++++++++++++++ utilities/quotients.md | 17 +++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 utilities/quotients.hpp create mode 100644 utilities/quotients.md diff --git a/utilities/quotients.hpp b/utilities/quotients.hpp new file mode 100644 index 00000000..79af1519 --- /dev/null +++ b/utilities/quotients.hpp @@ -0,0 +1,26 @@ +#pragma once +#include +#include + +// Generate all quotients of n +// return: n/1, n/2, ..., n +// Complexity: O(sqrt(n)) +template std::vector get_quotients(T n) { + std::vector res; + for (T x = 1;; ++x) { + if (x * x >= n) { + const int sz = res.size(); + if (x * x == n) res.push_back(x); + res.reserve(res.size() + sz); + for (int i = sz - 1; i >= 0; --i) { + T tmp = n / res.at(i); + if (tmp < x) continue; + if (tmp == x and tmp * tmp == n) continue; + res.push_back(tmp); + } + return res; + } else { + res.push_back(x); + } + } +} diff --git a/utilities/quotients.md b/utilities/quotients.md new file mode 100644 index 00000000..172be661 --- /dev/null +++ b/utilities/quotients.md @@ -0,0 +1,17 @@ +--- +title: 商列挙 +documentation_of: ./quotients.hpp +--- + +正の整数 $n$ に対して $\lfloor n / k \rfloor$ ( $k$ は整数)の形で表される整数を昇順に列挙する.計算量は $O(\sqrt{n})$. + +## 使用方法 + +```cpp +long long n = 10; +vector v = get_quotients(n); // 1, 2, 3, 5, 10 +``` + +## 問題例 + +- [Library Checker: Enumerate Quotients](https://judge.yosupo.jp/problem/enumerate_quotients)