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.
Input: N = 10, K = 5
0 and 5 are the only possible integers.
Input: N = 30, K = 4
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 value from 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:
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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Count of Numbers in a Range where digit d occurs exactly K times
- Count of numbers in range which are divisible by M and have digit D at odd places
- Count of Numbers in a Range divisible by m and having digit d in even positions
- Number of integers in a range [L, R] which are divisible by exactly K of it's digits
- Ways to form an array having integers in given range such that total sum is divisible by 2
- Count of integers of length N and value less than K such that they contain digits only from the given set
- Count numbers from 1 to n that have 4 as a digit
- Count of subsets of integers from 1 to N having no adjacent elements
- Count possible decodings of a given Digit Sequence | Set 2
- Count numbers less than N containing digits from the given set : Digit DP
- Count Possible Decodings of a given Digit Sequence
- Count numbers (smaller than or equal to N) with given digit sum
- Count of n digit numbers whose sum of digits equals to given sum
- Count digit groupings of a number with given constraints
- Count numbers with exactly K non-zero digits and distinct odd digit sum
- Count of N-digit numbers in base K with no two consecutive zeroes
- Count of integers obtained by replacing ? in the given string that give remainder 5 when divided by 13
- Count of N digit Numbers having no pair of equal consecutive Digits
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.