using System;
using System.Linq;
using System.Collections.Generic;
public class Test
{
static int[] d;
public static void Main()
{
int m = 5;
var query = foo(new int[]{ }, m).Select(item => string.Join("+", item)).Distinct();
foreach (var item in query)
Console.WriteLine(item);
Console.WriteLine(query.Count());
}
static IEnumerable<IEnumerable<int>> foo(IEnumerable<int> seed, int m)
{
if (seed.Sum() == m)
{
yield return seed;
}
else
{
if (seed.Count() == 0)
{
for (int i = 1; i <= m; i++)
{
var items = foo(seed.Concat(new int[] { i }), m);
foreach (var item in items) yield return item;
}
}
else
{
if (seed.Sum() < m)
{
for (int i = seed.Last(); i <= m; i++)
{
var items = foo(seed.Concat(new int[] { i }), m);
foreach (var item in items) yield return item;
}
}
}
}
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uTGlucTsKdXNpbmcgU3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWM7CgpwdWJsaWMgY2xhc3MgVGVzdAp7CiAgICBzdGF0aWMgaW50W10gZDsKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBNYWluKCkKICAgIHsKICAgIAlpbnQgbSA9IDU7CiAgICAgICAgdmFyIHF1ZXJ5ID0gZm9vKG5ldyBpbnRbXXsgfSwgbSkuU2VsZWN0KGl0ZW0gPT4gc3RyaW5nLkpvaW4oIisiLCBpdGVtKSkuRGlzdGluY3QoKTsKICAgICAgICBmb3JlYWNoICh2YXIgaXRlbSBpbiBxdWVyeSkKICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoaXRlbSk7CiAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUocXVlcnkuQ291bnQoKSk7CiAgICB9CiAgICBzdGF0aWMgSUVudW1lcmFibGU8SUVudW1lcmFibGU8aW50Pj4gZm9vKElFbnVtZXJhYmxlPGludD4gc2VlZCwgaW50IG0pCiAgICB7CiAgICAgICAgaWYgKHNlZWQuU3VtKCkgPT0gbSkKICAgICAgICB7CiAgICAgICAgICAgIHlpZWxkIHJldHVybiBzZWVkOwogICAgICAgIH0KICAgICAgICBlbHNlCiAgICAgICAgewogICAgICAgIAlpZiAoc2VlZC5Db3VudCgpID09IDApCiAgICAgICAgCXsKICAgICAgICAJCWZvciAoaW50IGkgPSAxOyBpIDw9IG07IGkrKykKICAgICAgICAJCXsKCQkJCQl2YXIgaXRlbXMgPSBmb28oc2VlZC5Db25jYXQobmV3IGludFtdIHsgaSB9KSwgbSk7CiAgICAgICAgICAgICAgICAgICAgZm9yZWFjaCAodmFyIGl0ZW0gaW4gaXRlbXMpIHlpZWxkIHJldHVybiBpdGVtOwogICAgICAgIAkJfQogICAgICAgIAl9CiAgICAgICAgCWVsc2UKICAgICAgICAJewoJICAgICAgICAgICAgaWYgKHNlZWQuU3VtKCkgPCBtKQoJICAgICAgICAgICAgewoJICAgICAgICAgICAgICAgIGZvciAoaW50IGkgPSBzZWVkLkxhc3QoKTsgaSA8PSBtOyBpKyspCgkgICAgICAgICAgICAgICAgewoJICAgICAgICAgICAgICAgIAl2YXIgaXRlbXMgPSBmb28oc2VlZC5Db25jYXQobmV3IGludFtdIHsgaSB9KSwgbSk7CgkgICAgICAgICAgICAgICAgICAgIGZvcmVhY2ggKHZhciBpdGVtIGluIGl0ZW1zKSB5aWVsZCByZXR1cm4gaXRlbTsKCSAgICAgICAgICAgICAgICB9CgkgICAgICAgICAgICB9CiAgICAgICAgCX0KICAgICAgICB9CiAgICB9Cgp9