
from itertools import accumulate
from functools import reduce
from operator  import mul

def Partition(n, m, L=[]):
    if m == 1 and n:
        yield [n]+L
    else:
        for i in range(1, n):
            yield from Partition(n-i, m-1, [i]+L)

def MAX_product(N, K):
    N_str = str(N)
    n = len(N_str)
    if n < K - 1:
        print('K too large.')
        return (-1, [])

    R = []
    for p in Partition(n, K+1):
        L = [ 0 ] + list(p)
        pa = list(accumulate(L))
        Numbers = list(map(int, (N_str[a:b] for a, b in zip(pa, pa[1:]))))
        Product = reduce(mul, Numbers, 1)
        R.append((Product, Numbers))

    return sorted(R, key=lambda t: t[0])[-1]

P1, L1 = MAX_product( 746589, 2)
print(P1, '=', ' x '.join(map(str, L1)))

P2, L2 = MAX_product(1111114, 3)
print(P2, '=', ' x '.join(map(str, L2)))
