Skip to content

Commit

Permalink
quotients
Browse files Browse the repository at this point in the history
  • Loading branch information
hitonanode committed Dec 26, 2023
1 parent a32e95c commit 07e9654
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
26 changes: 26 additions & 0 deletions utilities/quotients.hpp
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);
}
}
}
17 changes: 17 additions & 0 deletions utilities/quotients.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
title: Quotients of integer (商列挙)
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)
18 changes: 18 additions & 0 deletions utilities/test/quotients.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#define PROBLEM "https://judge.yosupo.jp/problem/enumerate_quotients"
#include "../quotients.hpp"

#include <iostream>
using namespace std;

int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);

long long N;
cin >> N;

auto ret = get_quotients(N);
cout << ret.size() << '\n';
for (auto x : ret) cout << x << ' ';
cout << '\n';
}

0 comments on commit 07e9654

Please sign in to comment.