C++ Library for Competitive Programming
View the Project on GitHub emthrm/cp-library
/* * @title 数学/商の列挙 * * verification-helper: PROBLEM https://judge.yosupo.jp/problem/enumerate_quotients */ #include <iostream> #include <tuple> #include <ranges> #include "emthrm/math/enumerate_quotients.hpp" int main() { long long n; std::cin >> n; // GCC 12 adopted P2415. const std::vector<std::tuple<long long, long long, long long>> quotients = emthrm::enumerate_quotients(n); const auto a = quotients | std::views::elements<2> | std::views::reverse; // const auto a = emthrm::enumerate_quotients(n) // | std::views::elements<2> // | std::views::reverse; const int k = a.size(); std::cout << k << '\n'; for (int i = 0; i < k; ++i) { std::cout << a[i] << " \n"[i + 1 == k]; } return 0; }
#line 1 "test/math/enumerate_quotients.test.cpp" /* * @title 数学/商の列挙 * * verification-helper: PROBLEM https://judge.yosupo.jp/problem/enumerate_quotients */ #include <iostream> #include <tuple> #include <ranges> #line 1 "include/emthrm/math/enumerate_quotients.hpp" #line 5 "include/emthrm/math/enumerate_quotients.hpp" #include <vector> namespace emthrm { template <typename T> std::vector<std::tuple<T, T, T>> enumerate_quotients(const T n) { std::vector<std::tuple<T, T, T>> quotients; for (T l = 1; l <= n;) { const T quotient = n / l, r = n / quotient + 1; quotients.emplace_back(l, r, quotient); l = r; } return quotients; } } // namespace emthrm #line 12 "test/math/enumerate_quotients.test.cpp" int main() { long long n; std::cin >> n; // GCC 12 adopted P2415. const std::vector<std::tuple<long long, long long, long long>> quotients = emthrm::enumerate_quotients(n); const auto a = quotients | std::views::elements<2> | std::views::reverse; // const auto a = emthrm::enumerate_quotients(n) // | std::views::elements<2> // | std::views::reverse; const int k = a.size(); std::cout << k << '\n'; for (int i = 0; i < k; ++i) { std::cout << a[i] << " \n"[i + 1 == k]; } return 0; }