fork download
  1. f = ->(r, m, n) {
  2. fact = ->n {(1..n).inject(:*) || 1}
  3. a = r.map {|d| [d] * m}.flatten
  4. x = fact.(a.size) / (fact.(m) ** r.size)
  5. return a.reverse if x < n
  6. a.size.times.each_with_object([]) {|_, acc|
  7. y = x.to_f / a.size
  8. acc << a.delete_at((n - 1) % x / y)
  9. x = (a.count(acc.last) + 1) * y
  10. n -= a.count(&acc.last.method(:>)) * y
  11. }
  12. }
  13. p f.(1..9, 1, 1)
  14. p f.(1..9, 1, 2)
  15. p f.(1..9, 1, 3)
  16. p f.(1..9, 1, 123456)
  17. p f.(1..9, 1, 234567)
  18. p f.(1..9, 1, 362880)
  19. p f.(1..4, 3, 1)
  20. p f.(1..4, 3, 2)
  21. p f.(1..4, 3, 3)
  22. p f.(1..4, 3, 123456)
  23. p f.(1..4, 3, 234567)
  24. p f.(1..4, 3, 369600)
  25.  
Success #stdin #stdout 0.01s 8008KB
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]