Alex_tz307

Kth Permutation

Sep 14th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.47 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. long long nr[20], folosit[20];
  6.  
  7. int main() {
  8.     long long b, n, k, i;
  9.     cin >> n >> k;
  10.     k--;
  11.     for (b = 1; b <= n; b++) {
  12.         nr[b - 1] = k % b;
  13.         k /= b;
  14.     }
  15.     for (b = n - 1; b >= 0; b--) {
  16.         i = -1;
  17.         while (nr[b]-- >= 0)
  18.             do
  19.                 i++;
  20.             while (folosit[i]);
  21.         folosit[i] = 1;    
  22.         cout << i + 1 << ' ';
  23.     }
  24.     return 0;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment