# See https://oeis.org/A177789
use v5.18.1; use warnings;
$|= 1;  # auto-flush

# 16 bits =>   791 elements
# 20 bits =>  4404 elements
# 22 bits => 12449 elements
use constant BITS => 16;

sub A177789 {
    my ($i, @t)= (shift, [[1,0]]);
    state @r; @r and return $r[$i][0][1];  # cached

    LOOP_1:
    while (@t) {
        my $t0= $t[0];  # get first task
        my ($p2,$p,$n2,$n)= map @$_,(@$t0[0], @$t0[-1]);

        if (($p2 > $n2) or (($p2 == $n2) and ($p > $n))) {  # stopper
            push @r, [[$p2,$p],[$n2,$n]];  # record finished task
            shift @t;  # remove finished task
            next LOOP_1;
        }
        $p2 <= (1 << BITS) or last LOOP_1;

        LOOP_2:
        while ($t0) {  # task
            if ($n2 & 1) {  # task failed, needs a split
                shift @t;   # remove failed task
                if ($n & 1) {  # n2 odd, n odd
                    push @t, [[$p2*2,$p],[$n2*2,$n]], [[$p2*2,$p+$p2],[$n2,($n+$n2)/2]];
                }
                else {         # n2 odd, n even
                    push @t, [[$p2*2,$p],[$n2,$n/2]], [[$p2*2,$p+$p2],[$n2*2,$n+$n2]];
                }
                last LOOP_2;
            }
            else {  # extend task
                if ($n & 1) {  # n2 even, n odd
                    ($n2,$n)= (3*$n2/2, (3*$n+1)/2);
                    push @$t0, [$n2,$n];
                }
                else {         # n2 even, n even
                    until (!$n2 or ($n2 & 1) or ($n & 1)) {$n2/=2; $n/=2}
                    push @$t0, [$n2,$n];
                }
            }
        }
    }
    @r= sort {$a->[0][0] <=> $b->[0][0] or $a->[0][1] <=> $b->[0][1]} @r;
    return $r[$i][0][1];
}

say join ",", map A177789($_), 0..750;

exit;
__END__