Advertisement
Guest User

Untitled

a guest
Jun 13th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. const short int N = 102;
  3.  
  4. using namespace std;
  5.  
  6. int n, number;
  7. queue <int> our;
  8. set <int> fact;
  9. int ans[20];
  10.  
  11. void prepation()
  12. {
  13. cin >> n >> number;
  14. for (int i = 1; i <= n; ++i) {
  15. fact.insert(i);
  16. }
  17. }
  18.  
  19. int factorial(int n)
  20. {
  21. if (n == 0) {
  22. return 1;
  23. }
  24. return factorial(n - 1) * n;
  25. }
  26.  
  27. int searching(int x)
  28. {
  29. int where = 0;
  30. for (auto el: fact) {
  31. if (where == x) {
  32. x = el;
  33. fact.erase(el);
  34. return x;
  35. }
  36. ++where;
  37. }
  38. }
  39.  
  40. bool in(int x, int s, int f)
  41. {
  42. return x >= s && x <= f;
  43. }
  44.  
  45. main()
  46. {
  47. prepation();
  48. pair <int, int> range;
  49. range = {1, factorial(n)};
  50. int position = 0;
  51. for (int i = n - 1; i >= 1; --i) {
  52. //cout << range.first << " " << range.second << endl;
  53.  
  54. int scope = factorial(i);
  55. int begining = range.first;
  56. int ending = begining + scope - 1;
  57. /*for (auto el: fact) {
  58. cout << el << " ";
  59. }
  60. cout << endl;*/
  61. for (int j = 0; j <= i; ++j) {
  62. if (in(number, begining, ending)) {
  63. ans[position] = searching(j);
  64. break;
  65. }
  66. ending += scope;
  67. begining += scope;
  68.  
  69. }
  70. ++position;
  71. range = {begining, ending};
  72. }
  73. for (int i = 0; i < n - 1; ++i) {
  74. cout << ans[i] << " ";
  75. }
  76. cout << *fact.begin();
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement