Skip to content

Commit 3b84543

Browse files
authored
quotients (#320)
1 parent 4b09c97 commit 3b84543

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

utilities/quotients.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
#include <algorithm>
3+
#include <vector>
4+
5+
// Generate all quotients of n
6+
// return: n/1, n/2, ..., n
7+
// Complexity: O(sqrt(n))
8+
template <class T = long long> std::vector<T> get_quotients(T n) {
9+
std::vector<T> res;
10+
for (T x = 1;; ++x) {
11+
if (x * x >= n) {
12+
const int sz = res.size();
13+
if (x * x == n) res.push_back(x);
14+
res.reserve(res.size() + sz);
15+
for (int i = sz - 1; i >= 0; --i) {
16+
T tmp = n / res.at(i);
17+
if (tmp < x) continue;
18+
if (tmp == x and tmp * tmp == n) continue;
19+
res.push_back(tmp);
20+
}
21+
return res;
22+
} else {
23+
res.push_back(x);
24+
}
25+
}
26+
}

utilities/quotients.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
title: Quotients of integer (商列挙)
3+
documentation_of: ./quotients.hpp
4+
---
5+
6+
正の整数 $n$ に対して $\lfloor n / k \rfloor$ ( $k$ は整数)の形で表される整数を昇順に列挙する.計算量は $O(\sqrt{n})$.
7+
8+
## 使用方法
9+
10+
```cpp
11+
long long n = 10;
12+
vector<long long> v = get_quotients(n); // 1, 2, 3, 5, 10
13+
```
14+
15+
## 問題例
16+
17+
- [Library Checker: Enumerate Quotients](https://judge.yosupo.jp/problem/enumerate_quotients)

utilities/test/quotients.test.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#define PROBLEM "https://judge.yosupo.jp/problem/enumerate_quotients"
2+
#include "../quotients.hpp"
3+
4+
#include <iostream>
5+
using namespace std;
6+
7+
int main() {
8+
cin.tie(nullptr);
9+
ios::sync_with_stdio(false);
10+
11+
long long N;
12+
cin >> N;
13+
14+
auto ret = get_quotients(N);
15+
cout << ret.size() << '\n';
16+
for (auto x : ret) cout << x << ' ';
17+
cout << '\n';
18+
}

0 commit comments

Comments
 (0)