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:
- Maximum score after flipping a Binary Matrix atmost K times
- GCD of two numbers formed by n repeating x and y times
- Times required by Simple interest for the Principal to become Y times itself
- Longest subsequence having difference atmost K
- Maximum sum of non-overlapping subarrays of length atmost K
- Next higher number using atmost one swap operation
- Maximize the maximum subarray sum after removing atmost one element
- Length of the longest subsegment which is UpDown after inserting atmost one integer
- Count different numbers possible using all the digits their frequency times
- Count of Numbers in a Range where digit d occurs exactly K times
- Count of times second string can be formed from the characters of first string
- Difference between two given times
- Find the value of N XOR'ed to itself K times
- Probability of getting a sum on throwing 2 Dices N times
- Find the value of XXXX.....(N times) % M where N is large
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