fork download
  1. #include <iostream>
  2. #include <numeric>
  3. #include <vector>
  4.  
  5. int pCount(int n, int m, int myMax) {
  6.  
  7. if (myMax * m < n) return 0;
  8. if (myMax * m == n) return 1;
  9.  
  10. if (m < 2) return m;
  11. if (n < m) return 0;
  12. if (n <= m + 1) return 1;
  13.  
  14. int niter = n / m;
  15. int count = 0;
  16.  
  17. for (; niter--; n -= m, --myMax) {
  18. count += pCount(n - 1, m - 1, myMax);
  19. }
  20.  
  21. return count;
  22. }
  23.  
  24. std::vector<int> unRank(int n, int m, int myMax, int nth) {
  25.  
  26. std::vector<int> z(m, 0);
  27. int count = 0;
  28. int j = 0;
  29.  
  30. for (int i = 0; i < z.size(); ++i) {
  31. int temp = pCount(n - 1, m - 1, myMax);
  32.  
  33. for (int r = n - m, k = myMax - 1;
  34. (count + temp) < nth && r > 0 && k; r -= m, --k) {
  35.  
  36. count += temp;
  37. n = r;
  38. myMax = k;
  39. ++j;
  40. temp = pCount(n - 1, m - 1, myMax);
  41. }
  42.  
  43. --m;
  44. --n;
  45. z[i] = j;
  46. }
  47.  
  48. return z;
  49. }
  50.  
  51. int main() {
  52. int max = 10;
  53. int min = 3;
  54. int N = 7;
  55. int sum = 42;
  56.  
  57. int m = N;
  58. int n = sum - m * (min - 1);
  59. int myMax = max - min + 1;
  60. int totalNum = pCount(n, m, myMax);
  61. int index = 100;
  62.  
  63. std::vector<int> z = unRank(n, m, myMax, index);
  64.  
  65. std::vector<int> v(max - min + 1);
  66. std::iota(v.begin(), v.end(), min);
  67.  
  68. for (int i = 0; i < m; ++i)
  69. std::cout << v[z[i]] << ", ";
  70.  
  71. std::cout << std::endl;
  72. return 0;
  73. }
Success #stdin #stdout 0s 4540KB
stdin
Standard input is empty
stdout
4, 4, 4, 5, 6, 9, 10,