fork download
  1. def f(m, n)
  2. cc = []
  3. count_permutations = ->s, w {
  4. if s < 1 || w < 1
  5. 0
  6. elsif s <= 5 && w == 1
  7. 1
  8. elsif cc[s] && cc[s][w]
  9. cc[s][w]
  10. else
  11. (cc[s] ||= [])[w] = (1..5).inject(0) {|c, d|
  12. c + count_permutations.(s - d, w - 1)
  13. }
  14. end
  15. }
  16. aux = ->acc, b, s, w {
  17. return acc if s < 1 || w < 1
  18. return acc << s if w == 1
  19. (1..5).inject(b) {|b, d|
  20. c = count_permutations.(s - d, w - 1)
  21. return aux.(acc << d, b, s - d, w - 1) if n <= b + c
  22. b + c
  23. } && nil
  24. }
  25. (1..m).inject(0) {|b, w|
  26. c = count_permutations.(m, w)
  27. return aux.([], b, m, w).join.to_i if n <= b + c
  28. b + c
  29. } && 0
  30. end
  31. g = ->m, n {puts "(#{m},#{n}) → #{f(m, n)}"}
  32. g.(2000, 1 << 1024)
Success #stdin #stdout 2.03s 47248KB
stdin
Standard input is empty
stdout
(2000,179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216) → 133455533351454245243544545545533544533353455351551354434255345155535155553543255433542555343535545355541554555135355524435524344435532524545543525545533451134455552254455333442334354534523153425445155555435153554255552353344553554445354545514355554555554435333353123555345255242334345515554533345534425455553521553555455455545441241132525455435442255542322515454522545115515532453523344525423555455451555554153554555544451254441554323215525455545135444445535554555545425522544345243455524545444534534551543444144