Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //1
- #include <iostream>
- #include <vector>
- using namespace std;
- int factorial[13] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600};
- vector<int> getPermutation(int n, long long k) {
- vector<int> permutation(n);
- vector<bool> used(n + 1);
- for (int i = 0; i < n; i++) {
- int usedPermutations = (k - 1) / factorial[n - i - 1] + 1;
- int j = 1, position = 0;
- for (; j < n + 1; j++) {
- if (!used[j])
- position++;
- if (usedPermutations == position)
- break;
- }
- permutation[i] = j;
- used[j] = true;
- k = (k - 1) % factorial[n - 1 - i] + 1;
- }
- return permutation;
- }
- int main() {
- int n, k;
- cin >> n >> k;
- auto answer = getPermutation(n, k);
- for (auto &i : answer)
- cout << i << ' ';
- return 0;
- }
- // 2
- #include <iostream>
- #include <vector>
- using namespace std;
- vector<int> combination;
- void printCombination() {
- for (int i: combination)
- cout << i << ' ';
- cout << '\n';
- }
- void nextCombination(int i, int n, int k) {
- if (k == 0) {
- printCombination();
- return;
- }
- for (; i <= n - k; i++) {
- combination.push_back(i + 1);
- nextCombination(i + 1, n, k - 1);
- combination.pop_back();
- }
- }
- int main() {
- int n, k;
- cin >> n >> k;
- nextCombination(0, n, k);
- return 0;
- }
- //3
- #include <iostream>
- #include <vector>
- using namespace std;
- int placement_amount(int n, int m) {
- int res = 1;
- for (int i = n; i >= n - m + 1; i--)
- res *= i;
- return res;
- }
- void genPlacementByNum(int m, int n, int k, vector<int> &placement) {
- vector<bool> used(n);
- int _n = n - 1, _k = k - 1;
- for (int i = 0; i < k; i++) {
- int block_cnt = m / placement_amount(_n, _k);
- m -= block_cnt * placement_amount(_n, _k);
- int position = 0;
- block_cnt++;
- while (block_cnt) {
- if (!used[position])
- --block_cnt;
- position++;
- }
- used[position - 1] = true;
- placement[i] = position;
- _n--; _k--;
- }
- }
- int main() {
- int n, k, m;
- cin >> n;
- cin >> k;
- cin >> m;
- vector<int> ans(k);
- genPlacementByNum(m - 1, n, k, ans);
- for (auto &i : ans)
- cout << i << ' ';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement