fork download
  1. #include <bits/stdc++.h>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. unsigned long long factorial(unsigned long long n) {
  7. unsigned long long result = 1;
  8. for(unsigned long long i=1; i<=n;i++) {
  9. result *= i;
  10. }
  11. return result;
  12. }
  13.  
  14. // Function to display the array
  15. void display(vector<int> vec, int n)
  16. {
  17. for (int i = 0; i < n-1; i++) {
  18. cout << vec[i] << ' ';
  19. }
  20. cout << vec[n-1] << '\n';
  21. }
  22.  
  23. // Function to find the permutations
  24. void findPermutations(vector<int> vec, unsigned long long n, unsigned long long pom, unsigned long long how_many)
  25. {
  26. unsigned long long fac, fac2, shift;
  27. fac = factorial(n-1);
  28. fac2 = factorial(n);
  29. if(pom > fac2) {
  30. cout << 0; return;
  31. }
  32. shift = pom / fac;
  33. pom -= shift*fac;
  34. if(shift > 0){
  35. vec.insert(vec.begin(), vec[shift]);
  36. vec.erase(vec.begin() + vec[shift+1]);
  37. }
  38.  
  39. while(pom--) {
  40. next_permutation(vec.begin(), vec.end());
  41. }
  42.  
  43. while(how_many--) {
  44. display(vec, n);
  45. next_permutation(vec.begin(), vec.end());
  46. }
  47. }
  48.  
  49. // Driver code
  50. int main()
  51. {
  52. ios_base::sync_with_stdio(false);
  53. cin.tie(NULL);
  54. unsigned long long t, a, b, c;
  55.  
  56. for (int i = 15; i <= 30; ++i)
  57. cout << i << " = " << factorial(i) << endl;
  58.  
  59. cin >> t;
  60. while(t--) {
  61. vector<int> vec;
  62. cin >> a >> b >> c;
  63. for(int i=1; i<=a; i++) {
  64. vec.push_back(i);
  65. }
  66.  
  67. findPermutations(vec, vec.size(), b, c);
  68.  
  69. ///
  70. cout << " <--- Ewidentny błąd! \n";
  71. ///
  72.  
  73. cout << '\n';
  74. }
  75. return 0;
  76. }
Success #stdin #stdout 0s 5592KB
stdin
1
23 17196083355034583040 10
stdout
15 = 1307674368000
16 = 20922789888000
17 = 355687428096000
18 = 6402373705728000
19 = 121645100408832000
20 = 2432902008176640000
21 = 14197454024290336768
22 = 17196083355034583040
23 = 8128291617894825984
24 = 10611558092380307456
25 = 7034535277573963776
26 = 16877220553537093632
27 = 12963097176472289280
28 = 12478583540742619136
29 = 11390785281054474240
30 = 9682165104862298112
0  <--- Ewidentny błąd!