v 0. Pasted by slipstak2 as cpp at 2011-02-11 18:52:00 MSK and set expiration to never.

Paste will expire never.

  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. int fact[13] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600};
  7.  
  8. int notUsed(vector<bool> &used, int blockNum) {
  9.  
  10.     int j, pos = 0;
  11.     for (j = 1; j < used.size(); j++) {
  12.         if (!used[j]) pos++;
  13.         if (blockNum == pos)
  14.             break;
  15.     }
  16.     return j;
  17. }
  18. vector<int> permutation_by_num(int n, int num) {
  19.  
  20.     vector<int> res(n);
  21.     vector<bool> used(n+1,false);
  22.  
  23.     for (int i = 0; i < n; i++) {
  24.         int blockNum = (num - 1) / fact[n - i - 1] + 1;
  25.         int j = notUsed(used, blockNum);
  26.         res[i] = j;
  27.         used[j] = true;
  28.         num = (num - 1) % fact[n - i - 1] + 1;
  29.     }
  30.     return res;
  31. }
  32. void output(vector<int> &perm) {
  33.     for (int i=0;i<perm.size();i++)
  34.         cout<<perm[i]<<' ';
  35. }
  36. int main() {
  37.     freopen("input.txt","r",stdin);
  38.     freopen("output.txt","w",stdout);
  39.  
  40.     int n,num;
  41.     cin>>n>>num;
  42.     vector<int> perm = permutation_by_num(n,num);
  43.     output(perm);
  44.     return 0;
  45. }


Editing is locked.