Emacs, 2626, 25-ish keystrokes
ARETBRETF3UPUPC-SPACEC-EM-WDOWNDOWNC-Y UPC-AC-SPACEC-EM-WDOWNC-EC-YRETF4C-U#(n-2)F4
command(s) explanation buffer reads (| = cursor aka point)
-----------------------------------------------------------------------------------------------
A<RET> B<RET> input starting points "a\nb\n|"
<F3> start new macro "a\nb\n|"
<UP><UP> move point two lines up "|a\nb\n"
C-<SPACE> C-E M-W copy line at point "a|\nb\n"
<DOWN><DOWN> move point two lines down "a\nb\n|"
C-Y yank (paste) "a\nb\na|"
<UP> move point one line up "a\nb|\na"
C-A C-<SPACE> C-E M-W copy line at point "a\nb|\na"
<DOWN> move point one line down "a\nb|\na|"
C-E C-Y <RET> yank (paste) and add new line "a\nb|\nab\n|"
<F4> stop macro recording "a\nb|\nab\n|"
C-U #(n-3) <F4> apply macro n-3 times "a\nb|\nab\nbab\nabbab\n|"