dc, 72 bytes
[3Q]sq2?dsa^1+2/dsusk[Ola^/O%plk1-dsk1>q]sp1[d5r^dOla^<psz1+d4/lu>t]dstx
0-based indexing.
This uses exact integer arithmetic -- no logarithm approximations. It will work up to the memory capacity of the computer.
The dc code can be turned into a Bash solution:
Bash + GNU utilities, 96 77 75 bytes
u=$[(2**$1+1)/2]
dc -e "[O$1^/O%p]sp1[d5r^dO$1^<psz1+d4/$u>t]dstx"|head -$u