#A(La) returns the number of words containing La[i] copies of i which avoid 1234 A:=proc(La) local n: n:=nops(La): RETURN(Ap(n,n,n,La)): end: #Ap(c1,c2,c3,La) returns the number of words containing La[i] copies of i such that one could prepend c1, a length-2 increasing sequence ending in c2, or a length-3 increasing sequence ending in c3 without creating a 1234 pattern. #reduLa(L,i) takes a list and an index and returns the list with the element at that index reduced by 1 reduLa:=proc(L,i) local n: n:=nops(L): if i > n then RETURN("index out of bounds"): fi: if i = 1 then RETURN([L[1]-1,op(2..n,L)]): fi: if i = n then RETURN([op(1..n-1,L),L[n]-1]): fi: RETURN([op(1..i-1,L),L[i]-1,op(i+1..n,L)]): end: Ap:=proc(c1,c2,c3,La) local n,i: option remember: n:=nops(La): #print(c1,c2,c3,La): for i from 1 to n do if La[i] <0 then RETURN(0): fi: od: if {op(La)}={0} then RETURN(1): fi: RETURN(add(Ap(i,c2,c3,reduLa(La,i)),i=1..c1)+add(Ap(c1,i,c3,reduLa(La,i)),i=(c1+1)..c2)+add(Ap(c1,c2,i,reduLa(La,i)),i=(c2+1)..c3)): end: #all_words(La) inputs a vector of letter counts and outputs all the words whose letters follow those counts all_words:=proc(La) local n,i, output,small: if {op(La)}={0} then RETURN({[]}): fi: n:= nops(La): output:={}: for i from 1 to n do if La[i] <> 0 then for small in all_words(reduLa(La,i)) do output:=output union {[i,op(small)]}: od: fi: od: output: end: test_contains:=proc(pi,p) local k,n,i,count,S,s: k:=nops(p): n:=nops(pi): #count:=0: for i from k to n do S:=IV(i,k): for s in S do if p=redu(pi[s]) then RETURN(true): print(yes): fi: od: od: false: end: with(combinat): #inputs a list of numbers, and outputs the reduction as a permutation of nops(L) #redu([e,pi,gamma,phi, h])=[ 4, 5 , 2 , 3 ,1] redu:=proc(L) local L1,T,k,i: if not isPerm(L) then RETURN(FAIL) fi: L1:=sort(sort(L,'output'='permutation'),'output'='permutation'): end: #IV(n,k): the set of increasing vectors of length k in {1, ...,n} ending in n IV:=proc(n,k) local S,i,s: S:=choose([seq(i,i=1..n-1)],k-1): {seq([op(s), n], s in S)}: end: #isPerm(L): determines if a list L is a permutation isPerm := proc(L) local n,i: n:=nops(L): if nops({op(L)})=nops(L) then RETURN(true): fi: false: end: Ap2:=proc(c1,c2,c3p,La) local n,i,c3: option remember: n:=nops(La): c3 := min(c3p,n): if c3 < n and {op((c3+1)..-1,La)} <> {0} then RETURN(0): fi: if n > c2 +1 then RETURN(Ap2(c1,c2,c3,[op(1..c2,La),add(La[i],i=(c2+1)..n)])): fi: #print(c1,c2,c3,La): for i from 1 to n do #print(La[i]): if La[i] <0 then RETURN(0): fi: od: if {op(La)}={0} then RETURN(1): fi: RETURN(add(Ap2(i,c2,c3,reduLa(La,i)),i=1..c1)+add(Ap2(c1,i,c3,reduLa(La,i)),i=(c1+1)..c2)+add(Ap2(c1,c2,i,reduLa(La,i)),i=(c2+1)..c3)): end: A2:=proc(La) local n: n:=nops(La): RETURN(Ap2(n,n,n,La)): end: