from functools import cache
fac = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]
pfds = lambda xs: sum((i + 1) * fac[x] for i, x in enumerate(xs[::-1]))
@cache
def gen(s, k):
if s < k:
return []
if k == 1:
return [[s]] if s < 10 else []
ans = []
for d in range(1, 10):
for xs in gen(s - d, k - 1):
if xs[0] >= d:
ans.append([d] + xs[:])
return ans
def find(n, k):
s, l = sum(int(c) for c in str(n)), len(str(n))
X = pfds([int(c) for c in str(n)])
for nf in gen(s, l + 1):
if pfds(nf) < X // k:
return int(''.join(map(str, nf)))
return -1
print(find(20001100, 120))
To embed this program on your website, copy the following code and paste it into your website's HTML: