<<Combinatorica`

Get[NotebookDirectory[]<>"mathmzv2.m"];

aToN[a_List] := Join[{0}, #, {1}]&@Flatten[Join[{1}, Table[0, {#-1}]]&/@a]

nToA[a_List] := If[#[[1]] ≠ 2, {{#[[1]]-2}, Rest[#]}, Rest[#]]&@Differences[Flatten[Position[Join[{1}, a], 1]]]

zAtoiN[a_List] := (-1)^Length[a] ii[aToN[a]];

d[ii[a_List],n_Integer]/;n≥3:=Sum[il[(a[[Range[i,i+n+1]]])]*im[(a[[Join[Range[i],Range[i+n+1,Length[a]]]]])],{i,1,Length[a]-n-1}];

d[sum_Plus,n_Integer]/;n≥3:=Expand[d[#1,n]&/@sum];

d[c[i_Integer]*ii[a_List],n_Integer]/;n≥3:=c[i] d[ii[a],n];
d[l_*c[i_Integer]*ii[a_List],n_Integer]/;NumberQ[l]&&n≥3:=l c[i] d[ii[a],n];
d[l_*ii[a_List],n_Integer]/;NumberQ[l]&&n≥3:=l d[ii[a],n];

r1x[a_List, func_] := Block[{}, 
(
If[Length[a] == 2, Return[func[{0,1}]]];
If[a[[1]] == a[[-1]], Return[0]];
If[Length[Union[a[[2;;-2]]]] == 1, Return[0]];
Return[func[a]]
)];

r2x[a_List, func_] := Block[{pos,k, nk,comp,sum},(
If[Length[a] == 2, Return[func@@{0,1}]];

If[a[[1]] ≠ 0 || a[[-1]] ≠ 1, Return[func[a]]];

(* returns (k, n1, ... nr) *)
pos = Differences[Join[{2},Flatten[Position[a,1]]]];
k = pos[[1]];
nk = Rest[pos];

comp = Compositions[k, Length[nk]];

sum = (-1)^k *Sum[(Times@@(Binomial[nk[[#]] + comp[[i,#]]-1, comp[[i,#]]]&/@Range[Length[nk]]))*(func[(aToN[(nk+comp[[i]])])]), {i,1,Length[comp]}];
Return[(sum)]
)];

r3x[a_List, func_] := Block[{}, (
If[Length[a] == 2, Return[func@@{0,1}]];

If[a[[1]] > a[[-1]], Return[(-1)^Length[a]*(func[Reverse[a]])]];
Return[func[a]]
)];

r4x[a_List, func_] := Block[{}, (
If[Length[a] == 2, Return[func[{0,1}]]];

If[a[[1]] == 0 && a[[-1]] == 1 && a[[2]] == 1 && a[[-2]] == 1, Return[(-1)^Length[a] *(func[Reverse[1-a]])]];
Return[func[a]];
)];

candualx[a_List, func_] := Block[{}, (
If[Length[a] == 2, Return[func[{0,1}]]];

If[a[[1]] == 0 && a[[-1]] == 1 && a[[2]] == 1 && a[[-2]] == 0
&& FromDigits[a, 2] > FromDigits[Reverse[1-a], 2], Return[(-1)^(Length[a])func[Reverse[1-a]]]];

Return[func[a]];
)];

tozx[a_List, func_] := Block[{}, (
(-1)^(Count[a, 1]-1)*func[nToA[a]]
)];

r1[expr_] := (expr)/.{il[a_List]:>r1x[a, il],im[a_List]:>r1x[a, im]}//Expand
r2[expr_] := (expr)/.{il[a_List]:>r2x[a, il],im[a_List]:>r2x[a, im]}//Expand
r3[expr_] := (expr)/.{il[a_List]:>r3x[a, il],im[a_List]:>r3x[a, im]}//Expand
r4[expr_] := (expr)/.{il[a_List]:>r4x[a, il],im[a_List]:>r4x[a, im]}//Expand

candual[expr_] := (expr)/.{il[a_List]:>candualx[a, il],im[a_List]:>candualx[a, im]}//Expand

toz[expr_] := (expr)/.{il[a_List]:>tozx[a, zl],im[a_List]:>tozx[a, zm]}//Expand

(* ------------ *)

two[x_] := Table[2, {x}];

z1333[l:{a_,b_,c_,d_,e_}] := zA[Flatten@Riffle[two/@l,{1,3,3,3}]]
z3313[l:{a_,b_,c_,d_,e_}] := zA[Flatten@Riffle[two/@l,{3,3,1,3}]]
z31312[l:{a_,b_,c_,d_,e_}] := zA[Flatten@Riffle[two/@l,{3,1,3,{1,2}}]]
z131212[l:{a_,b_,c_,d_,e_}] := zA[Flatten@Riffle[two/@l,{1,3,{1,2},{1,2}}]]
z121213[l:{a_,b_,c_,d_,e_}] := zA[Flatten@Riffle[two/@l,{{1,2},{1,2},1,3}]]

z1333cyc[{a_,b_,c_,d_,e_}] := z1333[{a,b,c,d,e}] + z3313[{c,d,e,a,b}] - z31312[{d,e,a,b,c}] + z131212[{e,a,b,c,d}]  + z121213[{b,c,d,e,a}];

(z1333cyc[{0,0,0,0,5}]/(Pi^(10 + 2*5)/(10 + 2*5+1)!))/.zA->z

(z1333cyc[{0,0,0,0,6}]/(Pi^(10 + 2*6)/(10 + 2*6+1)!))/.zA->z

(* --------- *)

n := 10;

out = z1333cyc[{0,0,0,0,n}] + z1333cyc[{0,0,0,n,0}] + Sum[z1333cyc[{0,0,i,0,n-i}], {i, 1, n-2}] + z1333cyc[{0,1,0,n-1,0}]/.zA->zAtoiN;

(d[out,#]//r1//r3//r2//r4//r2//candual)&/@Range[3,10+2*n,2]