Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

quotients #320

Merged
merged 1 commit into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
}
Loading