from collections import Counter

M = 10**9 + 7
dct = {k: 0 for k in range(10)}
dct[0] = 1
ans = [(0, 1)]
for _ in range(10**5):
    x = max(k for k in dct if dct[k] > 0)
    new_dct = dict(dct)
    for k, v in dct.items():
        if v > 0:
            if k == x:
                new_dct[k] -= v
                new_dct[1] += v
                new_dct[0] += v
            else:
                new_dct[k] -= v
                new_dct[k + 10 - x] += v
    for k in new_dct:
        new_dct[k] 
    dct = new_dct

    l = sum(dct.values())
    ans.append((ans[-1][0] + 10 - x, l))
    
def find(k, K):
    i = 0
    while ans[i + 1][0] <= K + k:
        i += 1
    return ans[i][1]

def digitwise_addition(N, K):
    cnt = Counter(map(int, str(N)))
    res = 0
    for key in cnt:
        v = find(key, K)
        res = (res + find(key, K) * cnt[key]) % M
    return res

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: