-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a32e95c
commit a5ca555
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#pragma once | ||
#include <algorithm> | ||
#include <vector> | ||
|
||
// Generate all quotients of n | ||
// return: n/1, n/2, ..., n | ||
// Complexity: O(sqrt(n)) | ||
template <class T = long long> std::vector<T> get_quotients(T n) { | ||
std::vector<T> 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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- | ||
title: 商列挙 | ||
documentation_of: ./quotients.hpp | ||
--- | ||
|
||
正の整数 $n$ に対して $\lfloor n / k \rfloor$ ( $k$ は整数)の形で表される整数を昇順に列挙する.計算量は $O(\sqrt{n})$. | ||
|
||
## 使用方法 | ||
|
||
```cpp | ||
long long n = 10; | ||
vector<long long> v = get_quotients(n); // 1, 2, 3, 5, 10 | ||
``` | ||
|
||
## 問題例 | ||
|
||
- [Library Checker: Enumerate Quotients](https://judge.yosupo.jp/problem/enumerate_quotients) |