Sum of all numbers formed having 4 atmost X times, 5 atmost Y times and 6 atmost Z times

Given three integers X, Y and Z, the task is to find the sum of all the numbers formed having 4 at most X times, 5 at most Y times, and 6 at most Z times, under mod 10^9+7.
Examples:Â
Â
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
Â
Approach:Â
Â
- 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 the 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:Â
Â
C++
// C++ program to find sum of all numbers// formed having 4 atmost X times, 5 atmost// Y times and 6 atmost Z times#include <bits/stdc++.h>using namespace std;Â
const int N = 101;const int mod = 1e9 + 7;Â
// exactsum[i][j][k] stores the sum of// all the numbers having exact// i 4's, j 5's and k 6'sint exactsum[N][N][N];Â
// exactnum[i][j][k] stores numbers// of numbers having exact// i 4's, j 5's and k 6'sint exactnum[N][N][N];Â
// Utility function to calculate the// sum for x 4's, y 5's and z 6'sint getSum(int x, int y, int z){Â Â Â Â int ans = 0;Â Â Â Â exactnum[0][0][0] = 1;Â Â Â Â for (int i = 0; i <= x; ++i) {Â Â Â Â Â Â Â Â for (int j = 0; j <= y; ++j) {Â Â Â Â Â Â Â Â Â Â Â Â for (int k = 0; k <= z; ++k) {Â
                // Computing exactsum[i][j][k]                // as explained above                if (i > 0) {                    exactsum[i][j][k]                        += (exactsum[i - 1][j][k] * 10                            + 4 * exactnum[i - 1][j][k])                           % mod;                    exactnum[i][j][k]                        += exactnum[i - 1][j][k] % mod;                }                if (j > 0) {                    exactsum[i][j][k]                        += (exactsum[i][j - 1][k] * 10                            + 5 * exactnum[i][j - 1][k])                           % mod;                    exactnum[i][j][k]                        += exactnum[i][j - 1][k] % mod;                }                if (k > 0) {                    exactsum[i][j][k]                        += (exactsum[i][j][k - 1] * 10                            + 6 * exactnum[i][j][k - 1])                           % mod;                    exactnum[i][j][k]                        += exactnum[i][j][k - 1] % mod;                }Â
                ans += exactsum[i][j][k] % mod;                ans %= mod;            }        }    }    return ans;}Â
// Driver codeint main(){Â Â Â Â int x = 1, y = 1, z = 1;Â
    cout << (getSum(x, y, z) % mod);Â
    return 0;} |
Java
// Java program to find sum of all numbers // formed having 4 atmost X times, 5 atmost // Y times and 6 atmost Z times      class GFG {         static int N = 101;     static int mod = (int)1e9 + 7;          // exactsum[i][j][k] stores the sum of     // all the numbers having exact     // i 4's, j 5's and k 6's     static int exactsum[][][] = new int[N][N][N];          // exactnum[i][j][k] stores numbers     // of numbers having exact     // i 4's, j 5's and k 6's     static int exactnum[][][] = new int[N][N][N];          // Utility function to calculate the     // sum for x 4's, y 5's and z 6's     static int getSum(int x, int y, int z)     {         int ans = 0;         exactnum[0][0][0] = 1;         for (int i = 0; i <= x; ++i)        {             for (int j = 0; j <= y; ++j)             {                 for (int k = 0; k <= z; ++k)                 {                          // Computing exactsum[i][j][k]                     // as explained above                     if (i > 0)                    {                         exactsum[i][j][k]                         += (exactsum[i - 1][j][k] * 10                        + 4 * exactnum[i - 1][j][k]) % mod;                                                  exactnum[i][j][k]                         += exactnum[i - 1][j][k] % mod;                     }                     if (j > 0)                    {                         exactsum[i][j][k]                         += (exactsum[i][j - 1][k] * 10                        + 5 * exactnum[i][j - 1][k]) % mod;                                                  exactnum[i][j][k]                         += exactnum[i][j - 1][k] % mod;                     }                     if (k > 0)                    {                         exactsum[i][j][k]                         += (exactsum[i][j][k - 1] * 10                        + 6 * exactnum[i][j][k - 1]) % mod;                                                  exactnum[i][j][k]                         += exactnum[i][j][k - 1] % mod;                     }                          ans += exactsum[i][j][k] % mod;                     ans %= mod;                 }             }         }         return ans;     }          // Driver code     public static void main (String[] args)    {         int x = 1, y = 1, z = 1;              System.out.println(getSum(x, y, z) % mod);          } }Â
// This code is contributed by AnkitRai01 |
Python3
# Python3 program to find sum of all numbers # formed having 4 atmost X times, 5 atmost # Y times and 6 atmost Z times import numpy as npÂ
N = 101; mod = int(1e9) + 7; Â
# exactsum[i][j][k] stores the sum of # all the numbers having exact # i 4's, j 5's and k 6's exactsum = np.zeros((N, N, N)); Â
# exactnum[i][j][k] stores numbers # of numbers having exact # i 4's, j 5's and k 6's exactnum = np.zeros((N, N, N)); Â
# Utility function to calculate the # sum for x 4's, y 5's and z 6's def getSum(x, y, z) : Â Â Â Â ans = 0; Â Â Â Â exactnum[0][0][0] = 1; Â Â Â Â for i in range(x + 1) :Â Â Â Â Â Â Â Â for j in range(y + 1) :Â Â Â Â Â Â Â Â Â Â Â Â for k in range(z + 1) :Â
                # Computing exactsum[i][j][k]                 # as explained above                 if (i > 0) :                    exactsum[i][j][k] += (exactsum[i - 1][j][k] * 10 +                                            4 * exactnum[i - 1][j][k]) % mod;                                                                 exactnum[i][j][k] += exactnum[i - 1][j][k] % mod;                                  if (j > 0) :                    exactsum[i][j][k] += (exactsum[i][j - 1][k] * 10+                                        5 * exactnum[i][j - 1][k]) % mod;                                                              exactnum[i][j][k] += exactnum[i][j - 1][k] % mod;                                  if (k > 0) :                    exactsum[i][j][k] += (exactsum[i][j][k - 1] * 10                                            + 6 * exactnum[i][j][k - 1]) % mod;                     exactnum[i][j][k] += exactnum[i][j][k - 1] % mod; Â
                ans += exactsum[i][j][k] % mod;                 ans %= mod;                      return ans; Â
# Driver code if __name__ == "__main__" : Â
    x = 1; y = 1; z = 1; Â
    print((getSum(x, y, z) % mod)); Â
# This code is contributed by AnkitRai01 |
C#
// C# program to find sum of all numbers // formed having 4 atmost X times, 5 atmost // Y times and 6 atmost Z times using System;Â
class GFG {         static int N = 101;     static int mod = (int)1e9 + 7;          // exactsum[i][j][k] stores the sum of     // all the numbers having exact     // i 4's, j 5's and k 6's     static int [,,]exactsum = new int[N, N, N];          // exactnum[i][j][k] stores numbers     // of numbers having exact     // i 4's, j 5's and k 6's     static int [,,]exactnum= new int[N, N, N];          // Utility function to calculate the     // sum for x 4's, y 5's and z 6's     static int getSum(int x, int y, int z)     {         int ans = 0;         exactnum[0, 0, 0] = 1;         for (int i = 0; i <= x; ++i)        {             for (int j = 0; j <= y; ++j)             {                 for (int k = 0; k <= z; ++k)                 {                          // Computing exactsum[i, j, k]                     // as explained above                     if (i > 0)                    {                         exactsum[i, j, k]                         += (exactsum[i - 1, j, k] * 10                         + 4 * exactnum[i - 1, j, k]) % mod;                                                  exactnum[i, j, k]                         += exactnum[i - 1, j, k] % mod;                     }                     if (j > 0)                    {                         exactsum[i, j, k]                         += (exactsum[i, j - 1, k] * 10                         + 5 * exactnum[i, j - 1, k]) % mod;                                                  exactnum[i, j, k]                         += exactnum[i, j - 1, k] % mod;                     }                     if (k > 0)                    {                         exactsum[i, j, k]                         += (exactsum[i, j, k - 1] * 10                         + 6 * exactnum[i, j, k - 1]) % mod;                                                  exactnum[i, j, k]                         += exactnum[i, j, k - 1] % mod;                     }                          ans += exactsum[i, j, k] % mod;                     ans %= mod;                 }             }         }         return ans;     }          // Driver code     public static void Main ()    {         int x = 1, y = 1, z = 1;              Console.WriteLine(getSum(x, y, z) % mod);          } }     // This code is contributed by AnkitRai01 |
Javascript
<script>    // Javascript program to find sum of all numbers     // formed having 4 atmost X times, 5 atmost     // Y times and 6 atmost Z times         let N = 101;     let mod = 1e9 + 7;            // exactsum[i][j][k] stores the sum of     // all the numbers having exact     // i 4's, j 5's and k 6's     let exactsum = new Array(N);            // exactnum[i][j][k] stores numbers     // of numbers having exact     // i 4's, j 5's and k 6's     let exactnum = new Array(N);         for(let i = 0; i < N; i++)    {        exactsum[i] = new Array(N);        exactnum[i] = new Array(N);        for(let j = 0; j < N; j++)        {            exactsum[i][j] = new Array(N);            exactnum[i][j] = new Array(N);            for(let k = 0; k < N; k++)            {                exactsum[i][j][k] = 0;                exactnum[i][j][k] = 0;            }        }    }           // Utility function to calculate the     // sum for x 4's, y 5's and z 6's     function getSum(x, y, z)     {         let ans = 0;         exactnum[0][0][0] = 1;         for (let i = 0; i <= x; ++i)        {             for (let j = 0; j <= y; ++j)             {                 for (let k = 0; k <= z; ++k)                 {                            // Computing exactsum[i][j][k]                     // as explained above                     if (i > 0)                    {                         exactsum[i][j][k]                         += (exactsum[i - 1][j][k] * 10                         + 4 * exactnum[i - 1][j][k]) % mod;                                                    exactnum[i][j][k]                         += exactnum[i - 1][j][k] % mod;                     }                     if (j > 0)                    {                         exactsum[i][j][k]                         += (exactsum[i][j - 1][k] * 10                         + 5 * exactnum[i][j - 1][k]) % mod;                                                    exactnum[i][j][k]                         += exactnum[i][j - 1][k] % mod;                     }                     if (k > 0)                    {                         exactsum[i][j][k]                         += (exactsum[i][j][k - 1] * 10                         + 6 * exactnum[i][j][k - 1]) % mod;                                                    exactnum[i][j][k]                         += exactnum[i][j][k - 1] % mod;                     }                            ans += exactsum[i][j][k] % mod;                     ans %= mod;                 }             }         }         return ans;     }          let x = 1, y = 1, z = 1;            document.write(getSum(x, y, z) % mod);Â
</script> |
Output:Â
3675
Â
Time Complexity: O(x*y*z)
Auxiliary Space: O(N3)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!


