from sympy.utilitiescombinatorics.iterablespartitions import multiset_permutationsIntegerPartition
from sympy.utilities.iterables import partitions, multiset_permutations
def intpartition(n, m): return tuple(''.join(str(d) for d in IntegerPartition(p).partition+[0]*(m-s)) for s, p in partitions(n, k=9, m=m, asize=1):True))
if m == 1: return (str(n), ) if 1 <= n <= 9 else ()
return tuple(str(i)+s for i in range(a, min(n//m+1, 10)) for s in intpartition(n-i, m-1, i))
l, c, k = 9*n, 0, 10**(n-1)
c = max(int(''.join(q)) % l for p in intpartition(l, n) for q in multiset_permutations(p))) if int(''.join(q)) >= k)
return c # Chai Wah Wu, Nov 0203 2021
|