import math, copy, time


n = 10

pomocnePole = [0]*int((n*(n+1))/2)

for i in range(1,11):
    pomocnePole[int((i*(i+1))/2 - 1)] = i + 1


def generate(n):
    r = []
    k = int(math.factorial(n+1) / 2)
    for i in range(k):
        z = [0]*n
        z[n-1] = 1
        cis = i
        for j in range(n-1):
            z[n-2-j] = cis % (3+j)
            cis = cis // (3+j)
        r.append(z)
    return r

def generate2m(n):
    t = []
    r = generate2(n+1)
    for i in r:
        suma = sum(i)
        k = pomocnePole[suma-1]
        if k != 0:
            if k % 2 == 1:
                suma = 0
                for j in range(n):
                    suma += (j+1) * i[j]
                if (suma % 2 == 1):
                    continue
            lave = 0
            prave = 0
            for j in range(n-1):
                if i[j] >= k or i[j] < k - (n+j+2)/2:
                    lave = -1
                    break
                if j+1 <= n / 2:
                    lave += i[j]
                if j+1 >= n / 2:
                    prave += i[j]
            if lave < k-2 or prave < k-2:
                continue
            t.append(i)
    return t
    

def generate2(n):
    r = []
    if n == 2:
        return [[1]]
    k = generate2(n-1)
    for j in k:
        for i in range(n):
            r.append([i] + j)
    return r

def test(p):
##    end = len(p) - 1
##    cisla = [0, end]
##    pole = copy.copy(p)
##    if pole[end] != 1:
##        return [0, 0]
##    pole[end] = 0
##    for i in range(end):
##        if pole[end-1-i] != 0:
##            k
##
##
##    return [1, cisla]
    end = len(p) - 1
    cisla = [0, end + 1]
    pole = copy.copy(p)
    if pole[end] != 1:
        return [0, 0]
    pole[end] = 0
    n = test2(pole, cisla, end-1)
    return n
    if n[0] == 0:
        return 0
    return set(n[1])

def test2(pole, cisla, end):
    if end < 0:
        return [1, cisla]
    if pole[end] == 0:
        return test2(pole, cisla, end-1)
    if pole[end] == 2:
        if check(cisla, end+1, pole):
            pridaj(cisla, end+1, pole)
            if pole[end] == 0:
                return test2(pole, cisla, end-1)
            if check(cisla, cisla[1] - end-1, pole):
                pridaj(cisla, cisla[1] - end-1, pole)
                return test2(pole, cisla, end-1)
        return [0,0]
    if pole[end] == 1:
        if check(cisla, end+1, pole):
            pole2 = copy.copy(pole)
            cisla2 = copy.copy(cisla)
            pridaj(cisla2, end+1, pole2)
            k = test2(pole2, cisla2, end-1)
            if (k[0] == 1):
                return k
        if check(cisla, cisla[1] - end-1, pole):
            pridaj(cisla, cisla[1] - end-1, pole)
            return test2(pole, cisla, end-1)
    return [0,0]
        

def check(cisla, i, pole):
    pole2 = [0]*len(pole)
    for j in cisla:
        if j == i:
            return False
        z = max(i-j, j-i)
        pole2[z-1] += 1
        if pole2[z-1] > pole[z-1]:
            return False
    return True

def pridaj(cisla, i, pole):
    for j in cisla:
        z = max(i-j, j-i)
        pole[z-1] -= 1
    cisla.append(i)
t = time.time()
print(t)
m2 = generate2m(n)
print(len(m2))
print(time.time() - t)
with open('output.txt', 'w') as o:
    with open('output2.txt', 'w') as o2:
        for i in m2:
            k = test(i)
            if k[0] == 1:
                o.write(str(i) + "\n")
            else:
                o2.write(str(i) + "\n")

