Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- const short int N = 102;
- using namespace std;
- int n, number;
- queue <int> our;
- set <int> fact;
- int ans[20];
- void prepation()
- {
- cin >> n >> number;
- for (int i = 1; i <= n; ++i) {
- fact.insert(i);
- }
- }
- int factorial(int n)
- {
- if (n == 0) {
- return 1;
- }
- return factorial(n - 1) * n;
- }
- int searching(int x)
- {
- int where = 0;
- for (auto el: fact) {
- if (where == x) {
- x = el;
- fact.erase(el);
- return x;
- }
- ++where;
- }
- }
- bool in(int x, int s, int f)
- {
- return x >= s && x <= f;
- }
- main()
- {
- prepation();
- pair <int, int> range;
- range = {1, factorial(n)};
- int position = 0;
- for (int i = n - 1; i >= 1; --i) {
- //cout << range.first << " " << range.second << endl;
- int scope = factorial(i);
- int begining = range.first;
- int ending = begining + scope - 1;
- /*for (auto el: fact) {
- cout << el << " ";
- }
- cout << endl;*/
- for (int j = 0; j <= i; ++j) {
- if (in(number, begining, ending)) {
- ans[position] = searching(j);
- break;
- }
- ending += scope;
- begining += scope;
- }
- ++position;
- range = {begining, ending};
- }
- for (int i = 0; i < n - 1; ++i) {
- cout << ans[i] << " ";
- }
- cout << *fact.begin();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement