/* ***************************************************************************** * %{QMAKE_PROJECT_NAME} * Copyright (c) %YEAR% killerbee * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * ****************************************************************************/ #ifndef VOTE_COUNT_H #define VOTE_COUNT_H #include #include #include #include #include #include using kblib::max; void print(std::vector& vec) { std::clog << "{"; for (auto i : vec) { if (i != max) { std::clog << ' ' << i; } else { std::clog << " _"; } } std::clog << " }\n"; } inline std::uint64_t vote_count(std::int16_t min, std::int16_t total) { std::vector votes(total); std::uint64_t counter{}; for (auto n : kblib::range(+min, total + 1)) { // std::clog << n << '/' << total << ":\n"; std::iota(votes.begin(), votes.begin() + n, 1); std::fill(votes.begin() + n, votes.end(), max); do { print(votes); ++counter; } while (std::next_permutation(votes.begin(), votes.end())); } return counter; } inline std::uint64_t factorial_div(std::int16_t x, std::int16_t d) { if (x < 0) { throw std::domain_error{"negative factorial"}; } std::uint64_t counter{1}; // bool first = true; for (auto i : kblib::range(d + 1, x + 1)) { // if (! std::exchange(first, false)) { // std::cout << " * "; //} // std::cout << i; counter *= i; } // std::cout << '\n'; return counter; } inline std::uint64_t vote_calc(std::int16_t min, std::int16_t total) { std::uint64_t counter{}; for (auto n : kblib::range(0, total - min + 1)) { // const auto t_f = factorial(total); counter += factorial_div(total, n); } return counter; } #endif // VOTE_COUNT_H