fork download
  1. mthPartition <- function(n, m)
  2. {
  3. A <- matrix(0, n, n)
  4. A[1, 1:min(n, 5)] <- 1
  5. if (n > 1) for (i in 2:n) for (j in 1:n) A[i, j] <- sum(A[i - 1, pmax(j - 1:5, 0)])
  6.  
  7. for (L in 1:n) {
  8. if (m <= A[L, n]) break
  9. ifelse(L < n, m <- m - A[L, n], return(0))
  10. }
  11.  
  12. a <- integer(L)
  13. for (i in 1:L) {
  14. for (j in 1:5) {
  15. if (i < L) {
  16. if (m <= A[L - i, n - j]) break
  17. m <- m - A[L - i, n - j]
  18. } else {
  19. if (j == n) break
  20. }
  21. }
  22.  
  23. a[i] <- j
  24. n <- n - j
  25. }
  26.  
  27. paste(a, collapse = "")
  28. }
  29.  
  30. Q <- list(
  31. c(2, 1), c(2, 2), c(2, 3),
  32. c(20, 1), c(20, 2), c(20, 3), c(20, 400096), c(20, 400097),
  33. c(32, 1), c(32, 2), c(32, 3), c(32, 1000), c(32, 1000000), c(32, 1000000000), c(32, 1333610936), c(32, 1333610937)
  34. )
  35.  
  36. for (q in Q) cat(sprintf("(%d, %d) → %s\n", q[1], q[2], mthPartition(q[1], q[2])))
Success #stdin #stdout 0.44s 41272KB
stdin
Standard input is empty
stdout
(2, 1) → 2
(2, 2) → 11
(2, 3) → 0
(20, 1) → 5555
(20, 2) → 14555
(20, 3) → 15455
(20, 400096) → 11111111111111111111
(20, 400097) → 0
(32, 1) → 2555555
(32, 2) → 3455555
(32, 3) → 3545555
(32, 1000) → 34355354
(32, 1000000) → 11532334334
(32, 1000000000) → 2141111311212411131
(32, 1333610936) → 11111111111111111111111111111111
(32, 1333610937) → 0