Advertisement
a53

Gaseste Permutarea

a53
Feb 22nd, 2020
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int P;
  4. long long N;
  5. long long fact[21];
  6. int cifre[21] = {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}; /// ce cifre au fost deja folosite (vor avea 0 pe pozitia lor)
  7.  
  8. int main()
  9. {
  10. cin >> P >> N;
  11. fact[0]=1;
  12. for(int i=1; i<=P; i++)
  13. fact[i] = i*fact[i-1];
  14. for(int i=P; i>=1; i--)
  15. {
  16. int idx=1;
  17. while(N > fact[i-1])
  18. {
  19. N -= fact[i-1];
  20. idx++;
  21. }
  22. int poz = 1;
  23. while (cifre[poz]==0) poz++;
  24. while (idx > 1)
  25. {
  26. while (cifre[poz]==0) poz++;
  27. idx--;
  28. poz++;
  29. }
  30. while (cifre[poz]==0) poz++;
  31. cifre[poz]=0;
  32. cout << poz << ' ';
  33. }
  34. return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement