Toys (Codeforces 44I)
Tags: Others, combinatorics

Little Masha loves arranging her toys into piles on the floor. And she also hates it when somebody touches her toys. One day Masha arranged all her n toys into several piles and then her elder brother Sasha came and gathered all the piles into one. Having seen it, Masha got very upset and started crying. Sasha still can't calm Masha down and mom is going to come home soon and punish Sasha for having made Masha crying. That's why he decides to restore the piles' arrangement. However, he doesn't remember at all the way the toys used to lie. Of course, Masha remembers it, but she can't talk yet and can only help Sasha by shouting happily when he arranges the toys in the way they used to lie. That means that Sasha will have to arrange the toys in every possible way until Masha recognizes the needed arrangement. The relative position of the piles and toys in every pile is irrelevant, that's why the two ways of arranging the toys are considered different if can be found two such toys that when arranged in the first way lie in one and the same pile and do not if arranged in the second way. Sasha is looking for the fastest way of trying all the ways because mom will come soon. With every action Sasha can take a toy from any pile and move it to any other pile (as a result a new pile may appear or the old one may disappear). Sasha wants to find the sequence of actions as a result of which all the pile arrangement variants will be tried exactly one time each. Help Sasha. As we remember, initially all the toys are located in one pile. 

Input: The first line contains an integer n (1 ≤ n ≤ 10) — the number of toys.

Output: In the first line print the number of different variants of arrangement of toys into piles. Then print all the ways of arranging toys into piles in the order in which Sasha should try them (i.e. every next way must result from the previous one through the operation described in the statement). Every way should be printed in the following format. In every pile the toys should be arranged in ascending order of the numbers. Then the piles should be sorted in ascending order of the numbers of the first toys there. Output every way on a single line. Cf. the example to specify the output data format. If the solution is not unique, output any of them.


Sample Inputs:
3

Sample Outputs:
5
{1,2,3}
{1,2},{3}
{1},{2,3}
{1},{2},{3}
{1,3},{2}

Solution (Python):
import sys
readline = sys.stdin.readline

def read() -> int:
    return int(readline().strip())

def solve(n: int) -> list:
    if n == 1:
        return [[(1,)]]
    part1 = solve(n - 1)
    result = []
    invert = True
    for comb in part1:
        tmp = [comb + [(n,)]]
        for i in range(len(comb) - 1, -1, -1):
            comb2 = comb[:]
            comb2[i] += (n,)
            tmp.append(comb2)
        if invert:
            tmp.reverse()
        result.extend(tmp)
        invert = not invert
    return result

def write_result(result: list):
    print(len(result))
    for line in result:
        tmp = []
        for item in line:
            tmp.append('{' + ','.join((str(n) for n in item)) + '}')
        print(','.join(tmp))
N = read()
result = solve(N)
write_result(result)

Solution (Python):
def combinacoes(x):
    if x == 1:
        return [[0]]
    else:
        aux = combinacoes(x - 1)
    pilha = []
    par = 0
    for element in aux:
        lmt = max(element) + 1
        if par == 0:
            for j in range(lmt + 1):
                pilha.append(element + [j])
        else:
            for j in range(lmt + 1)[::-1]:
                pilha.append(element + [j])
        par = par ^ 1
    return pilha
n = int(input())
possibilidades = combinacoes(n)
print(len(possibilidades))
for item in possibilidades:
    arranjos = ''
    limit = max(item) + 1
    for group in range(limit):
        arranjos += '{'
        toys = ''
        for i in range(n):
            if item[i] == group:
                toys += '{0},'.format(i + 1)
        arranjos += toys[:-1]
        arranjos += '},'
    print(arranjos[:-1])

Solution (Python):
def combinacoes(x):
    if x == 1:
        return [[0]]
    else:
        aux = combinacoes(x - 1)
    pilha = []
    par = 0
    for element in aux:
        lmt = max(element) + 1
        if par == 0:
            for j in range(lmt + 1):
                pilha.append(element + [j])
        else:
            for j in range(lmt + 1)[::-1]:
                pilha.append(element + [j])
        par = par ^ 1
    return pilha
n = int(input())
possibilidades = combinacoes(n)
print(len(possibilidades))
for item in possibilidades:
    arranjos = ''
    limit = max(item) + 1
    for group in range(limit):
        arranjos += '{'
        toys = ''
        for i in range(n):
            if item[i] == group:
                toys += '{0},'.format(i + 1)
        arranjos += toys[:-1]
        arranjos += '},'
    print(arranjos[:-1])