Given a range [L, R] and two positive integers N and M. The task is to count the numbers in the range containing only non-zero digits whose sum of digits is equal to N and the number is divisible by M.
Input: L = 1, R = 100, N = 8, M = 2
Only 8, 26, 44 and 62 are valid numbers
Input: L = 1, R = 200, N = 4, M = 11
Only 22 and 121 are valid numbers
Prerequisites : Digit DP
Approach: Firstly, if we are able to count the required numbers up to R i.e. in the range [0, R], we can easily reach our answer in the range [L, R] by solving for from zero to R and then subtracting the answer we get after solving for from zero to L – 1. Now, we need to define the DP states.
- Since we can consider our number as a sequence of digits, one state is the position at which we are currently in. This position can have values from 0 to 18 if we are dealing with the numbers up to 1018. In each recursive call, we try to build the sequence from left to right by placing a digit from 0 to 9.
- Second state is the sum of the digits we have placed so far.
- Third state is the remainder which defines the modulus of the number we have made so far modulo M.
- Another state is the boolean variable tight which tells the number we are trying to build has already become smaller than R so that in the upcoming recursive calls we can place any digit from 0 to 9. If the number has not become smaller, the maximum limit of digit we can place is digit at the current position in R.
For the number to have only non-zero digits, we maintain a variable nonz whose value if 1 tells the first digit in the number we have placed is a non-zero digit and thus, now we can’t place any zero digit in upcoming calls. Otherwise, we can place a zero digit as a leading zero so as to make number of digits in current number smaller than number of digits in upper limit.
Below is the implementation of the above approach:
Short Python Implementation :
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Count numbers from range whose prime factors are only 2 and 3
- Count numbers from range whose prime factors are only 2 and 3 using Arrays | Set 2
- Count of numbers having only 1 set bit in the range [0, n]
- Count of numbers having only one unset bit in a range [L,R]
- Find the count of numbers that can be formed using digits 3, 4 only and having length at max N.
- Count of Numbers in a Range divisible by m and having digit d in even positions
- Count the numbers with N digits and whose suffix is divisible by K
- Count of numbers from range [L, R] whose sum of digits is Y
- Count all prime numbers in a given range whose sum of digits is also prime
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Smallest number greater than or equal to X whose sum of digits is divisible by Y
- Kth number exceeding N whose sum of its digits divisible by M
- Count of n digit numbers whose sum of digits equals to given sum
- Find the maximum range [L,R] whose sum is divisible by M
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.