fork download
  1. function nthPartition(m, n)
  2. implicit none
  3. character * 32 :: nthPartition
  4. integer :: m, n, i, j, L, v(32)
  5. integer, allocatable :: A(:, :)
  6.  
  7. allocate(A(m, m))
  8. A = 0
  9. A(1, 1:min(m, 5)) = 1
  10. do i = 2, m
  11. do j = 1, 5
  12. A(i, j + 1:) = A(i, j + 1:) + A(i - 1, :m - j)
  13. end do
  14. end do
  15.  
  16. do L = 1, m
  17. if (n <= A(L, m)) exit
  18. if (L < m) then
  19. n = n - A(L, m)
  20. else
  21. nthPartition = '0'
  22. deallocate(A)
  23. return
  24. end if
  25. end do
  26.  
  27. do i = 1, L
  28. do j = 1, 5
  29. if (i < L) then
  30. if (n <= A(L - i, m - j)) exit
  31. n = n - A(L - i, m - j)
  32. else
  33. if (j == m) exit
  34. end if
  35. end do
  36.  
  37. v(i) = j
  38. m = m - j
  39. end do
  40.  
  41. write(nthPartition, '(32(i0))') v(1:L)
  42. deallocate(A)
  43. end function
  44.  
  45. program main
  46. implicit none
  47. integer :: Q(2, 16) = reshape((/ &
  48. (/2, 1/), (/2, 2/), (/2, 3/), &
  49. (/20, 1/), (/20, 2/), (/20, 3/), (/20, 400096/), (/20, 400097/), &
  50. (/32, 1/), (/32, 2/), (/32, 3/), (/32, 1000/), (/32, 1000000/), &
  51. (/32, 1000000000/), (/32, 1333610936/), (/32, 1333610937/) &
  52. /), (/2, 16/))
  53.  
  54. character * 32 :: nthPartition
  55. integer :: i, m, n
  56.  
  57. do i = 1, size(Q, 2)
  58. m = Q(1, i); n = Q(2, i)
  59. print '("(", (i0), ", ", (i0), ") → ", (a))', m, n, nthPartition(m, n)
  60. end do
  61. end program
Success #stdin #stdout 0s 5320KB
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