fork download
  1. def f(m, n)
  2. cc = []
  3. count_permutations = ->s, w {
  4. if s < 1 || w < 1 || s < w || w * 5 < s
  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 * 10 + s if w == 1
  19. (1..5).inject(b) {|b, d|
  20. c = count_permutations.(s - d, w - 1)
  21. return aux.(acc * 10 + d, b, s - d, w - 1) if n <= b + c
  22. b + c
  23. } && nil
  24. }
  25. 0 < m && 0 < n && (1..m).inject(0) {|b, w|
  26. c = count_permutations.(m, w)
  27. return aux.(0, b, m, w) if n <= b + c
  28. b + c
  29. } && 0 || 0
  30. end
  31. g = ->m, n {puts "(#{m},#{n}) → #{f(m, n)}"}
  32. =begin
  33. g.(-1,1)
  34. g.(1,-1)
  35. g.(-1,-1)
  36. g.(2,1)
  37. g.(2,2)
  38. g.(2,3)
  39. g.(20,1)
  40. g.(20,2)
  41. g.(20,3)
  42. g.(20,400096)
  43. g.(20,400097)
  44. g.(32,1)
  45. g.(32,2)
  46. g.(32,3)
  47. g.(32,1000)
  48. g.(32,1000000)
  49. g.(32,1000000000)
  50. g.(32,1333610936)
  51. g.(32,1333610937)
  52. =end
  53. g.(2000, 1 << 1024)
  54.  
Success #stdin #stdout 1.1s 53312KB
stdin
Standard input is empty
stdout
(2000,179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216) → 133455533351454245243544545545533544533353455351551354434255345155535155553543255433542555343535545355541554555135355524435524344435532524545543525545533451134455552254455333442334354534523153425445155555435153554255552353344553554445354545514355554555554435333353123555345255242334345515554533345534425455553521553555455455545441241132525455435442255542322515454522545115515532453523344525423555455451555554153554555544451254441554323215525455545135444445535554555545425522544345243455524545444534534551543444144