Given three positive integers N, A and B. The task is to count the numbers of length N containing only digits A and B and whose sum of digits also contains the digits A and B only. Print the answer modulo 109 + 7.
Input: N = 3, A = 1, B = 3
Possible numbers of length 3 are 113, 131, 111, 333, 311, 331 and so on…
But only 111 is a valid number since its sum of digits is 3 (contains digits A and B only)
Input: N = 10, A = 2, B = 3
Approach: The idea is to express the sum of digits of the number as a linear equation in two variables i.e.
S = X * A + Y * B where A and B are the given digits and X and Y are the frequencies of these digits respectively.
Since, the sum of (X + Y) should be equal to N (length of the number) according to the given condition, we can replace Y with (N – X) and the equation reduces to S = X * A + (N – X) * B. Thus, X = (S – N * B) / (A – B).
Now, we can iterate over all possible values of S where minimum value of S is an N-digit number where all digits are 1 and maximum value of S is an N-digit number where all digits are 9 and check if the current value contains only digits A and B. Find the values of X and Y using the above formula for valid current S. Since, we can also permute the digits count of numbers will be (N! / X! Y!) for current value S. Add this result to the final answer.
Note: Use Fermat Little Theorem to compute n! % p.
Below is the implementation of the above approach:
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Find a Symmetric matrix of order N that contain integers from 0 to N-1 and main diagonal should contain only 0's
- Count of integers of length N and value less than K such that they contain digits only from the given set
- Find the count of numbers that can be formed using digits 3, 4 only and having length at max N.
- Find two numbers whose sum is N and does not contain any digit as K
- Length of longest subarray having only K distinct Prime Numbers
- Find whether only two parallel lines contain all coordinates points or not
- Count number of binary strings of length N having only 0's and 1's
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Count of n digit numbers whose sum of digits equals to given sum
- Print all n-digit numbers whose sum of digits equals to given sum
- Print numbers having first and last bits as the only set bits
- 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]
- Check if a String contains Anagrams of length K which does not contain the character X
- Print numbers with digits 0 and 1 only such that their sum is N
- Sum of the digits of square of the given number which has only 1's as its 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.