Given three integers X, Y and Z, the task is to find the sum of all the numbers formed having 4 atmost X times, 5 atmost Y times and 6 atmost Z times, under mod 10^9+7.
Input: X = 1, Y = 1, Z = 1 Output: 3675 Explanation: 4 + 5 + 6 + 45 + 54 + 56 + 65 + 46 + 64 + 456 + 465 + 546 + 564 + 645 + 654 = 3675 Input: X = 4, Y = 5, Z = 6 Output: 129422134
- As this problem has the property of sub-problems overlapping and optimal sub-structure, hence dynamic programming can be used to solve it.
- The numbers having exact i 4s, j 5s and k 6s for all i < x, j < y, j < z are required to get the required sum.
- Therefore the dp array exactnum[i][j][k] will store the exact count of numbers having exact i 4s, j 5s and k 6s.
- If exactnum[i – 1][j][k], exactnum[i][j – 1][k] and exactnum[i][j][k – 1] are already known, then it can be observed that the sum of these is the required answer, except in the case when exactnum[i – 1][j][k], exactnum[i][j – 1][k] or exactnum[i][j][k – 1] doesn’t exist. In that case, just skip it.
- exactsum[i][j][k] stores the sum of exact number having i 4's, j 5's and k 6's in the same way as
exactsum[i][j][k] = 10 * (exactsum[i - 1][j][k] + exactsum[i][j - 1][k] + exactsum[i][j][k - 1]) + 4 * exactnum[i - 1][j][k] + 5 * exactnum[i][j - 1][k] + 6 * exactnum[i][j][k - 1]
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 Binary strings of length N having atmost M consecutive 1s or 0s alternatively exactly K times
- Count numbers formed by given two digit with sum having given digits
- Sum of sum of all subsets of a set formed by first N natural numbers
- Find the count of numbers that can be formed using digits 3, 4 only and having length at max N.
- GCD of two numbers formed by n repeating x and y times
- Min and max length subarray having adjacent element difference atmost K
- Sum of series formed by difference between product and sum of N natural numbers
- Longest subsequence having difference atmost K
- Sum of all subsets of a set formed by first n natural numbers
- Sum of all N digit palindromic numbers divisible by 9 formed using digits 1 to 9
- Maximum score after flipping a Binary Matrix atmost K times
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Length of array pair formed where one contains all distinct elements and other all same elements
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Number formed by adding product of its max and min digit K times
- Product of all Subsets of a set formed by first N natural numbers
- Count of times second string can be formed from the characters of first string
- Number formed after K times repeated addition of smallest divisor of N
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Minimum sum of two numbers formed from digits of an array
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.
Improved By : AnkitRai01