Given a range represented by two positive integers l and r and two integers d and m. Find the count of numbers lying in the range which is divisible by m and have digit d at even positions of the number. (i.e. digit d should not occur on odd position). Note: Both numbers l and r have same number of digits.
Input : l = 10, r = 99, d = 8, m = 2
Output : 8
Explanation :Valid numbers are 18, 28, 38, 48, 58, 68, 78 and 98.
88 is not a valid number since 8 is also present at odd position.
Input : l = 1000, r = 9999, d = 7, m = 19
Output : 6
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 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.
If the current position is an even position, we simply place digit d and recursively solve for the next positions. But if the current position is an odd position we can place any digit except d and solve for the next positions.
Below is the implementation of the above approach.
Time Complexity : O(18 * (m – 1) * 2), if we are dealing with the numbers upto 1018
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Count of numbers in a range that does not contain the digit M and which is divisible by M.
- Count of numbers in range which are divisible by M and have digit D at odd places
- Count numbers divisible by K in a range with Fibonacci digit sum for Q queries
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Count of Numbers in Range where first digit is equal to last digit of the number
- Queries to count integers in a range [L, R] such that their digit sum is prime and divisible by K
- Count of Numbers in a Range where digit d occurs exactly K times
- Count numbers in a range that are divisible by all array elements
- Find all numbers between range L to R such that sum of digit and sum of square of digit is prime
- Numbers with a Fibonacci difference between Sum of digits at even and odd positions in a given range
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- N digit numbers divisible by 5 formed from the M digits
- Sum of all N-digit palindromic numbers which doesn't contains 0 and are divisible by 9
- Find two numbers made up of a given digit such that their difference is divisible by N
- Sum of all N digit palindromic numbers divisible by 9 formed using digits 1 to 9
- Count of integers from the range [0, N] whose digit sum is a multiple of K
- Numbers that are not divisible by any number in the range [2, 10]
- Queries for count of even digit sum elements in given range using MO's Algorithm
- Queries for the count of even digit sum elements in the given range using Segment Tree.
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.