#Python program for A368557 - John Rascoe 12/29/23
from sympy.utilities.iterables import ordered_partitions
from itertools import permutations, product
from collections import Counter

def A368557(n):
    if n == 0: return(1)
    C,p1 = [],[]
    p = list(ordered_partitions(n))
    #Sieves out partitions that have too many of the same part
    for x in range(len(p)):
        tot = Counter()
        for y in range(len(p[x])):
                tot.update({p[x][y]})
        if tot.most_common()[0][1] <= (len(p[x]) + 1)//2:
            p1.append(p[x])
    #finds all permutations of these partitions
    for i in range(len(p1)):
        pset = set(p1[i])
        if len(pset) == len(p1[i]):
            c = list(permutations(p1[i]))
        else:
            #Here product is used instead of permutations for speed, but some duplicates are created
            c = list(product(list(pset), repeat=len(p1[i])))
        for j in range(len(c)):
            s = 0
            if sum(c[j]) == n:
                #checks if adjacent differences are valid
                for k in range(len(c[j])-1):
                    if abs(c[j][k]-c[j][k+1]) in c[j]:
                        s += 1
                    else:
                        break
            if len(c[j])-1 == s:
                C.append(c[j])
    return(len(set(C)))

for i in range(17):
    print(i,A368557(i))