# L-24 MCS 507 Mon 17 Oct 2011 : enumpowersiter.py

# We enumerate exponents of all monomials in n variables of degree d
# with an iterative version of a recursive enumerator using a stack.

# Because we not always pop the tuple where accu[0] is the highest
# number of all accumulators in the stack, the order is not lexicographic.

d = input('give degree : ')
n = input('give number of variables : ')
L = [0 for i in xrange(n)]
S = [(0,L)]
while S != []:
   (k,accu) = S.pop(0)
   if sum(accu) == d:
      print accu
   elif k < len(accu):
      for i in xrange(d,-1,-1):
         new_accu = [x for x in accu]
         new_accu[k] = i
         S.append((k+1,new_accu))
