OFFSET
0,3
LINKS
Alois P. Heinz, Table of n, a(n) for n = 0..150
EXAMPLE
a(4) = 21 because there are 4! = 24 permutations of {1,2,3,4} and only 3 of them do not avoid the consecutive step pattern up, down, down given by the binary expansion of 4 = 100_2: (1,4,3,2), (2,4,3,1), (3,4,2,1).
MAPLE
a:= proc(n) option remember; local b, m, r, h;
if n<2 then return 1 fi;
m:= iquo(n, 2, 'r'); h:= 2^ilog2(n);
b:= proc(u, o, t) option remember; `if`(u+o=0, 1,
`if`(t=m and r=0, 0, add(b(u-j, o+j-1, irem(2*t, h)), j=1..u))+
`if`(t=m and r=1, 0, add(b(u+j-1, o-j, irem(2*t+1, h)), j=1..o)))
end; forget(b);
b(n, 0, 0)
end:
seq(a(n), n=0..30);
MATHEMATICA
a[n_] := a[n] = Module[{b, m, r, h},
If[n < 2, Return[1]]; {m, r} = QuotientRemainder[n, 2];
h = 2^(Length@IntegerDigits[n, 2] - 1);
b[u_, o_, t_] := b[u, o, t] = If[u + o == 0, 1,
If[t == m && r == 0, 0,
Sum[b[u - j, o + j - 1, Mod[2t, h]], {j, 1, u}]] +
If[t == m && r == 1, 0,
Sum[b[u + j - 1, o - j, Mod[2t+1, h]], {j, 1, o}]]];
b[n, 0, 0]];
a /@ Range[0, 30] (* Jean-François Alcover, Mar 23 2021, after Alois P. Heinz *)
CROSSREFS
KEYWORD
nonn
AUTHOR
Alois P. Heinz, May 22 2014
STATUS
approved