fork download
  1. import java.util.*;
  2. import java.util.function.*;
  3. class Ideone {
  4. private static int factorial(int n) {
  5. int acc = 1;
  6. for (int i = 1; i <= n; i++) acc *= i;
  7. return acc;
  8. }
  9. private static <T> List<T> reversed(List<T> list) {
  10. Collections.reverse(list);
  11. return list;
  12. }
  13. private static <T> int count(Predicate<T> pred, List<T> list) {
  14. int count = 0;
  15. for (var x : list) if (pred.test(x)) count++;
  16. return count;
  17. }
  18. private static List<Integer> f(int r, int m, int n) {
  19. List<Integer> list = new ArrayList<>();
  20. for (int i = 1; i <= r; i++)
  21. for (int j = 0; j < m; j++)
  22. list.add(i);
  23. int x = factorial(list.size()) / (int)Math.pow(factorial(m), r);
  24. if (x < n) return reversed(list);
  25. List<Integer> acc = new ArrayList<>();
  26. for (int i = 0, n_times = list.size(); i < n_times; i++) {
  27. double y = (double)x / list.size();
  28. acc.add(list.remove((int)((n - 1) % x / y)));
  29. x = (int)((count(z -> z == acc.get(acc.size() - 1), list) + 1) * y);
  30. n -= (int)(count(z -> z < acc.get(acc.size() - 1), list) * y);
  31. }
  32. return acc;
  33. }
  34. public static void main(String[] args) {
  35. System.out.println(f(9, 1, 1));
  36. System.out.println(f(9, 1, 2));
  37. System.out.println(f(9, 1, 3));
  38. System.out.println(f(9, 1, 123456));
  39. System.out.println(f(9, 1, 234567));
  40. System.out.println(f(9, 1, 362880));
  41. System.out.println(f(4, 3, 1));
  42. System.out.println(f(4, 3, 2));
  43. System.out.println(f(4, 3, 3));
  44. System.out.println(f(4, 3, 123456));
  45. System.out.println(f(4, 3, 234567));
  46. System.out.println(f(4, 3, 369600));
  47. }
  48. }
  49.  
Success #stdin #stdout 0.08s 53452KB
stdin
Standard input is empty
stdout
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 9, 8]
[1, 2, 3, 4, 5, 6, 8, 7, 9]
[4, 1, 6, 5, 8, 9, 7, 3, 2]
[6, 8, 4, 7, 5, 3, 2, 1, 9]
[9, 8, 7, 6, 5, 4, 3, 2, 1]
[1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]
[1, 1, 1, 2, 2, 2, 3, 3, 4, 3, 4, 4]
[1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 3, 4]
[2, 2, 2, 3, 3, 1, 4, 3, 4, 1, 1, 4]
[3, 2, 4, 4, 2, 4, 3, 3, 1, 1, 1, 2]
[4, 4, 4, 3, 3, 3, 2, 2, 2, 1, 1, 1]