# permutations => partitions
def perm():
n = 3
sol = [0] * (n+1)
def ok(k):
for i in range(1, k):
if sol[i] == sol[k]:
return 0
return 1
def ok(k):
for i in range(1, k):
if sol[i] == sol[k]:
return 0;
return 1
def display():
for i in range(1, n+1):
print(sol[i], end = " ")
print()
def bkt(k):
if k == n + 1:
display()
else:
for i in range(1,n+1):
sol[k] = i;
if ok(k):
bkt(k+1)
bkt(1)
perm()
print("-"*5)
def main():
n = 3
sol = [0] * (n+1)
def ok(k):
for i in range(1, k):
if sol[i] == sol[k]:
return 0
return 1
def getMax(k):
max = sol[1]
for i in range(2, k):
if sol[i] > max:
max = sol[i]
return max
def display():
for i in range(1, n+1):
print(sol[i], end = " ")
print()
def bkt(k):
if k == n + 1:
display()
else:
for i in range(1,getMax(k)+2):
sol[k] = i;
bkt(k+1)
bkt(1)
main()
IyBwZXJtdXRhdGlvbnMgPT4gcGFydGl0aW9ucwoKZGVmIHBlcm0oKToKICAgIG4gPSAzCiAgICBzb2wgPSBbMF0gKiAobisxKQogICAgZGVmIG9rKGspOgogICAgICAgIGZvciBpIGluIHJhbmdlKDEsIGspOgogICAgICAgICAgICBpZiBzb2xbaV0gPT0gc29sW2tdOgogICAgICAgICAgICAgICAgcmV0dXJuIDAKICAgICAgICByZXR1cm4gMQogICAgZGVmIG9rKGspOgogICAgICAgIGZvciBpIGluIHJhbmdlKDEsIGspOgogICAgICAgICAgICBpZiBzb2xbaV0gPT0gc29sW2tdOgogICAgICAgICAgICAgICAgcmV0dXJuIDA7CiAgICAgICAgcmV0dXJuIDEKICAgIGRlZiBkaXNwbGF5KCk6CiAgICAgICAgZm9yIGkgaW4gcmFuZ2UoMSwgbisxKToKICAgICAgICAgICAgcHJpbnQoc29sW2ldLCBlbmQgPSAiICIpCiAgICAgICAgcHJpbnQoKQogICAgZGVmIGJrdChrKToKICAgICAgICBpZiBrID09IG4gKyAxOgogICAgICAgICAgICBkaXNwbGF5KCkKICAgICAgICBlbHNlOgogICAgICAgICAgICBmb3IgaSBpbiByYW5nZSgxLG4rMSk6CiAgICAgICAgICAgICAgICBzb2xba10gPSBpOwogICAgICAgICAgICAgICAgaWYgb2soayk6CiAgICAgICAgICAgICAgICAgICBia3QoaysxKQogICAgYmt0KDEpCnBlcm0oKQoKcHJpbnQoIi0iKjUpCgpkZWYgbWFpbigpOgogICAgbiA9IDMKICAgIHNvbCA9IFswXSAqIChuKzEpCiAgICBkZWYgb2soayk6CiAgICAgICAgZm9yIGkgaW4gcmFuZ2UoMSwgayk6CiAgICAgICAgICAgIGlmIHNvbFtpXSA9PSBzb2xba106CiAgICAgICAgICAgICAgICByZXR1cm4gMAogICAgICAgIHJldHVybiAxCiAgICBkZWYgZ2V0TWF4KGspOgogICAgICAgIG1heCA9IHNvbFsxXQogICAgICAgIGZvciBpIGluIHJhbmdlKDIsIGspOgogICAgICAgICAgICBpZiBzb2xbaV0gPiBtYXg6CiAgICAgICAgICAgICAgICBtYXggPSBzb2xbaV0KICAgICAgICByZXR1cm4gbWF4CiAgICBkZWYgZGlzcGxheSgpOgogICAgICAgIGZvciBpIGluIHJhbmdlKDEsIG4rMSk6CiAgICAgICAgICAgIHByaW50KHNvbFtpXSwgZW5kID0gIiAiKQogICAgICAgIHByaW50KCkKICAgIGRlZiBia3Qoayk6CiAgICAgICAgaWYgayA9PSBuICsgMToKICAgICAgICAgICAgZGlzcGxheSgpCiAgICAgICAgZWxzZToKICAgICAgICAgICAgZm9yIGkgaW4gcmFuZ2UoMSxnZXRNYXgoaykrMik6CiAgICAgICAgICAgICAgICBzb2xba10gPSBpOwogICAgICAgICAgICAgICAgYmt0KGsrMSkKICAgIGJrdCgxKQptYWluKCkK