def func
def init
if $level == 1
$stack[$level] = 0
else
$stack[$level] = $stack[$level-1]-1
end
end
def succ()
if $stack[$level] < $n - $s
$stack[$level] += 1
return true
else
$s = $s - $stack[$level-1]
return false
end
end
def valid
if $stack[$level] <= $n - $s
$s = $s + $stack[$level]
return true
end
return false
end
def sol
return $s == $n
end
def printf
for i in 1..$level
print $stack[i], ' '
end
$s = $s - $stack[$level]
print "\n"
end
def solve
$s = 0
$level = 1
init()
while $level > 0
su = true
v = false
while su == true && v == false
su = succ()
if su == true
v = valid()
end
if su == true
if sol() == true
printf()
else
$level = $level + 1
init()
end
else
$level = $level - 1
end
end
end
end
$n = 5
$stack = [0] * ($n+1)
solve
end
func
ZGVmIGZ1bmMKCiAgICBkZWYgaW5pdAogICAgICBpZiAkbGV2ZWwgPT0gMQogICAgICAgICRzdGFja1skbGV2ZWxdID0gMAogICAgICBlbHNlCiAgICAgICAgJHN0YWNrWyRsZXZlbF0gPSAkc3RhY2tbJGxldmVsLTFdLTEKICAgICAgZW5kCiAgICBlbmQKCiAgICBkZWYgc3VjYygpCiAgICAgICAgaWYgJHN0YWNrWyRsZXZlbF0gPCAkbiAtICRzCiAgICAgICAgICAgJHN0YWNrWyRsZXZlbF0gKz0gMQogICAgICAgICAgIHJldHVybiB0cnVlCiAgICAgICAgZWxzZQogICAgICAgICAgICRzID0gJHMgLSAkc3RhY2tbJGxldmVsLTFdCiAgICAgICAgICAgcmV0dXJuIGZhbHNlCiAgICAgICAgZW5kCiAgICBlbmQKCiAgICBkZWYgdmFsaWQKICAgICAgICBpZiAkc3RhY2tbJGxldmVsXSA8PSAkbiAtICRzCiAgICAgICAgICAgJHMgPSAkcyArICRzdGFja1skbGV2ZWxdCiAgICAgICAgICAgcmV0dXJuIHRydWUKICAgICAgICBlbmQKICAgICAgICByZXR1cm4gZmFsc2UKICAgIGVuZAoKICAgIGRlZiBzb2wKICAgICAgICByZXR1cm4gJHMgPT0gJG4KICAgIGVuZAoKICAgIGRlZiBwcmludGYKICAgICAgZm9yIGkgaW4gMS4uJGxldmVsCiAgICAgICAgcHJpbnQgJHN0YWNrW2ldLCAnICcKICAgICAgZW5kCiAgICAgICRzID0gJHMgLSAkc3RhY2tbJGxldmVsXQogICAgICBwcmludCAiXG4iCiAgICBlbmQKCiAgICBkZWYgc29sdmUKICAgICAgJHMgPSAwCiAgICAgICRsZXZlbCA9IDEKICAgICAgaW5pdCgpCgogICAgICB3aGlsZSAkbGV2ZWwgPiAwCiAgICAgICAgICAgIHN1ID0gdHJ1ZQogICAgICAgICAgICB2ID0gZmFsc2UKICAgICAgICAgICAgd2hpbGUgc3UgPT0gdHJ1ZSAmJiB2ID09IGZhbHNlCiAgICAgICAgICAgICAgICAgIHN1ID0gc3VjYygpCiAgICAgICAgICAgICAgICAgIGlmIHN1ID09IHRydWUKICAgICAgICAgICAgICAgICAgICB2ID0gdmFsaWQoKQogICAgICAgICAgICAgICAgICBlbmQKICAgICAgICAgICAgICAgICAgaWYgc3UgPT0gdHJ1ZQogICAgICAgICAgICAgICAgICAgIGlmIHNvbCgpID09IHRydWUKICAgICAgICAgICAgICAgICAgICAgICBwcmludGYoKQogICAgICAgICAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgICAgICAgICRsZXZlbCA9ICRsZXZlbCArIDEKICAgICAgICAgICAgICAgICAgICAgIGluaXQoKQogICAgICAgICAgICAgICAgICAgIGVuZAogICAgICAgICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICAgICAgJGxldmVsID0gJGxldmVsIC0gMQogICAgICAgICAgICAgICAgICBlbmQKICAgICAgICAgICAgZW5kCiAgICAgIGVuZAogICAgZW5kCgogICAgJG4gPSA1CiAgICAkc3RhY2sgPSBbMF0gKiAoJG4rMSkKICAgIHNvbHZlCmVuZAoKZnVuYwo=