Count of integers from the range [0, N] whose digit sum is a multiple of K

Given two integers N and K, the task is to calculate the number of integers in the range [0, N] whose digit sum is a multiple of K. The answer could be large, so print the answer modulo 109 +7.
Examples:Â Â
Input: N = 10, K = 5Â
Output: 2Â
0 and 5 are the only possible integers.Input: N = 30, K = 4Â
Output: 7Â Â
Naive Approach: For small value of N, loop through the range [0, N] and check if the sum of the digits of the numbers are multiples of K or not.
Â
Efficient Approach: The idea is to use digit dp to solve this problem. Subproblems iterating through all index values from the left or most significant digit(MSD) in the given integer will be solved and for each index, store the number of ways such that (sum of digits upto current index) mod K to be zero. The dp states will be:Â
dp[idx][sum][tight]Â
idx = position, it tells about the index value from left in the given integerÂ
sum = sum of digits mod k, This parameter will store the (sum of digits mod k) in the generated integer from most significant digit(MSD) to pÂ
tight = flag if the current value is crossing the range (1, n) or notÂ
For unrestricted range tight = 0Â
For restricted range tight = 1Â
Let’s say we are at the MSD having index idx. So initially the sum will be 0. At every position, set a limit that is always in the range [0, 9].Â
Therefore, fill the digit at index by the digits in its range from 0 to limit and fetch the answer from the next state having index = idx + 1 and new_tight for next state is calculated separately. The dp state definition will be:Â
dp[idx][sum][tight] += dp[idx + 1][(sum + d) % k][new_tight]Â
for d in [0, limit]Â Â
Below is the implementation of the above approach:Â
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
#define MAX 100005#define MOD 1000000007Â
// To store the states of the dpint dp[MAX][101][2];Â
// Function to return the count of numbers// from the range [0, n] whose digit sum// is a multiple of k using bottom-up dpint countNum(int idx, int sum, int tight,             vector<int> num, int len, int k){    if (len == idx) {        if (sum == 0)            return 1;        else            return 0;    }    if (dp[idx][sum][tight] != -1)        return dp[idx][sum][tight];    int res = 0, limit;Â
    // The digit in this index can    // only be from [0, num[idx]]    if (tight == 0) {        limit = num[idx];    }Â
    // The digit in this index can    // be anything from [0, 9]    else {        limit = 9;    }    for (int i = 0; i <= limit; i++) {Â
        // new_tight is the flag value        // for the next position        int new_tight = tight;        if (tight == 0 && i < limit)            new_tight = 1;        res += countNum(idx + 1,                        (sum + i) % k, new_tight,                        num, len, k);        res %= MOD;    }Â
    // res can't be negative    if (res < 0)        res += MOD;    return dp[idx][sum][tight] = res;}Â
// Function to process the string to// a vector of digits from MSD to LSDvector<int> process(string s){Â Â Â Â vector<int> num;Â Â Â Â for (int i = 0; i < s.length(); i++) {Â Â Â Â Â Â Â Â num.push_back(s[i] - '0');Â Â Â Â }Â Â Â Â return num;}Â
// Driver codeint main(){Â
    // For large input number n    string n = "98765432109876543210";Â
    // Total number of digits in n    int len = n.length();Â
    int k = 58;Â
    // Clean dp table    memset(dp, -1, sizeof(dp));Â
    // Process the string to a vector    // of digits from MSD to LSD    vector<int> num = process(n);Â
    cout << countNum(0, 0, 0, num, len, k);Â
    return 0;} |
Java
// Java implementation of the approachimport java.util.*;Â
class GFG{Â
static final int MAX = 100005;static final int MOD = 1000000007;Â
// To store the states of the dpstatic int [][][]dp = new int[MAX][101][2];Â
// Function to return the count of numbers// from the range [0, n] whose digit sum// is a multiple of k using bottom-up dpstatic int countNum(int idx, int sum, int tight,            Vector<Integer> num, int len, int k){    if (len == idx)     {        if (sum == 0)            return 1;        else            return 0;    }    if (dp[idx][sum][tight] != -1)        return dp[idx][sum][tight];    int res = 0, limit;Â
    // The digit in this index can    // only be from [0, num[idx]]    if (tight == 0)     {        limit = num.get(idx);    }Â
    // The digit in this index can    // be anything from [0, 9]    else    {        limit = 9;    }    for (int i = 0; i <= limit; i++)    {Â
        // new_tight is the flag value        // for the next position        int new_tight = tight;        if (tight == 0 && i < limit)            new_tight = 1;        res += countNum(idx + 1,                        (sum + i) % k, new_tight,                        num, len, k);        res %= MOD;    }Â
    // res can't be negative    if (res < 0)        res += MOD;    return dp[idx][sum][tight] = res;}Â
// Function to process the String to// a vector of digits from MSD to LSDstatic Vector<Integer> process(String s){Â Â Â Â Vector<Integer> num = new Vector<Integer>();Â Â Â Â for (int i = 0; i < s.length(); i++)Â Â Â Â {Â Â Â Â Â Â Â Â num.add(s.charAt(i) - '0');Â Â Â Â }Â Â Â Â return num;}Â
// Driver codepublic static void main(String[] args){Â
    // For large input number n    String n = "98765432109876543210";Â
    // Total number of digits in n    int len = n.length();Â
    int k = 58;Â
    // Clean dp table    for(int i = 0; i < MAX; i++)    {        for(int j = 0; j < 101; j++)        {            for(int l = 0; l < 2; l++)            dp[i][j][l] = -1;        }    }Â
    // Process the String to a vector    // of digits from MSD to LSD    Vector<Integer> num = process(n);Â
    System.out.print(countNum(0, 0, 0, num, len, k));Â
}}Â
// This code is contributed by 29AjayKumar |
Python 3
# Python 3 implementation of the approachMAX = 10005MOD = 1000000007Â
# Function to return the count of numbers# from the range [0, n] whose digit sum# is a multiple of k using bottom-up dpdef countNum(idx, sum, tight, num, len1, k):Â Â Â Â if (len1 == idx):Â Â Â Â Â Â Â Â if (sum == 0):Â Â Â Â Â Â Â Â Â Â Â Â return 1Â Â Â Â Â Â Â Â else:Â Â Â Â Â Â Â Â Â Â Â Â return 0Â Â Â Â if (dp[idx][sum][tight] != -1):Â Â Â Â Â Â Â Â return dp[idx][sum][tight]Â Â Â Â res = 0Â
    # The digit in this index can    # only be from [0, num[idx]]    if (tight == 0):        limit = num[idx]Â
    # The digit in this index can    # be anything from [0, 9]    else:        limit = 9    for i in range(limit + 1):                 # new_tight is the flag value        # for the next position        new_tight = tight        if (tight == 0 and i < limit):            new_tight = 1        res += countNum(idx + 1,(sum + i) % k,                       new_tight, num, len1, k)        res %= MODÂ
    # res can't be negative    if (res < 0):        res += MOD    dp[idx][sum][tight] = res    return dp[idx][sum][tight]Â
# Function to process the string to# a vector of digits from MSD to LSDdef process(s):Â Â Â Â num = []Â Â Â Â for i in range(len(s)):Â Â Â Â Â Â Â Â num.append(ord(s[i]) - ord('0'))Â Â Â Â return numÂ
# Driver codeif __name__ == '__main__':         # For large input number n    n = "98765432109876543210"Â
    # Total number of digits in n    len1 = len(n)Â
    k = 58         # To store the states of the dp    dp = [[[-1 for i in range(2)]               for j in range(101)]                for k in range(MAX)]Â
    # Process the string to a vector    # of digits from MSD to LSD    num = process(n)Â
    print(countNum(0, 0, 0, num, len1, k))Â
# This code is contributed by Surendra_Gangwar |
C#
// C# implementation of the approachusing System;using System.Collections.Generic;Â
class GFG{static readonly int MAX = 10005;static readonly int MOD = 1000000007;Â
// To store the states of the dpstatic int [,,]dp = new int[MAX, 101, 2];Â
// Function to return the count of numbers// from the range [0, n] whose digit sum// is a multiple of k using bottom-up dpstatic int countNum(int idx, int sum, int tight,              List<int> num, int len, int k){    if (len == idx)     {        if (sum == 0)            return 1;        else            return 0;    }         if (dp[idx, sum, tight] != -1)        return dp[idx, sum, tight];    int res = 0, limit;Â
    // The digit in this index can    // only be from [0, num[idx]]    if (tight == 0)     {        limit = num[idx];    }Â
    // The digit in this index can    // be anything from [0, 9]    else    {        limit = 9;    }    for (int i = 0; i <= limit; i++)    {Â
        // new_tight is the flag value        // for the next position        int new_tight = tight;        if (tight == 0 && i < limit)            new_tight = 1;        res += countNum(idx + 1,                       (sum + i) % k, new_tight,                        num, len, k);        res %= MOD;    }Â
    // res can't be negative    if (res < 0)        res += MOD;    return dp[idx, sum, tight] = res;}Â
// Function to process the String to// a vector of digits from MSD to LSDstatic List<int> process(String s){Â Â Â Â List<int> num = new List<int>();Â Â Â Â for (int i = 0; i < s.Length; i++)Â Â Â Â {Â Â Â Â Â Â Â Â num.Add(s[i] - '0');Â Â Â Â }Â Â Â Â return num;}Â
// Driver codepublic static void Main(String[] args){Â
    // For large input number n    String n = "98765432109876543210";Â
    // Total number of digits in n    int len = n.Length;Â
    int k = 58;Â
    // Clean dp table    for(int i = 0; i < MAX; i++)    {        for(int j = 0; j < 101; j++)        {            for(int l = 0; l < 2; l++)            dp[i, j, l] = -1;        }    }Â
    // Process the String to a vector    // of digits from MSD to LSD    List<int> num = process(n);Â
    Console.Write(countNum(0, 0, 0, num, len, k));}}Â
// This code is contributed by Rajput-Ji |
Javascript
<script>Â
// Javascript implementation of the approachÂ
var MAX = 100005;var MOD = 1000000007;Â
// To store the states of the dpvar dp = Array.from(Array(MAX), () => Array(101));for(var i =0; i<MAX; i++)Â Â Â Â Â Â Â Â for(var j =0; j<101; j++)Â Â Â Â Â Â Â Â Â Â Â Â dp[i][j] = new Array(2).fill(-1);Â
// Function to return the count of numbers// from the range [0, n] whose digit sum// is a multiple of k using bottom-up dpfunction countNum(idx, sum, tight, num, len, k){    if (len == idx) {        if (sum == 0)            return 1;        else            return 0;    }    if (dp[idx][sum][tight] != -1)        return dp[idx][sum][tight];    var res = 0, limit;Â
    // The digit in this index can    // only be from [0, num[idx]]    if (tight == 0) {        limit = num[idx];    }Â
    // The digit in this index can    // be anything from [0, 9]    else {        limit = 9;    }    for (var i = 0; i <= limit; i++) {Â
        // new_tight is the flag value        // for the next position        var new_tight = tight;        if (tight == 0 && i < limit)            new_tight = 1;        res += countNum(idx + 1,                        (sum + i) % k, new_tight,                        num, len, k);        res %= MOD;    }Â
    // res can't be negative    if (res < 0)        res += MOD;    return dp[idx][sum][tight] = res;}Â
// Function to process the string to// a vector of digits from MSD to LSDfunction process(s){Â Â Â Â var num = [];Â Â Â Â for (var i = 0; i < s.length; i++) {Â Â Â Â Â Â Â Â num.push(s[i].charCodeAt(0) - '0'.charCodeAt(0));Â Â Â Â }Â Â Â Â return num;}Â
// Driver code// For large input number nvar n = "98765432109876543210";// Total number of digits in nvar len = n.length;var k = 58;// Process the string to a vector// of digits from MSD to LSDvar num = process(n);document.write( countNum(0, 0, 0, num, len, k));Â
</script> |
635270835
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



