Sum of all numbers formed having 4 atmost X times , 5 atmost Y times and 6 atmost Z times | Javascript program to find sum of all numbers formed having 4 atmost X times , 5 atmost Y times and 6 atmost Z times ; exactsum [ i ] [ j ] [ k ] stores the sum of all the numbers having exact i 4 ' s , ▁ j ▁ 5' s and k 6 's ; exactnum [ i ] [ j ] [ k ] stores numbers of numbers having exact i 4 ' s , ▁ j ▁ 5' s and k 6 's ; Utility function to calculate the sum for x 4 ' s , ▁ y ▁ 5' s and z 6 's ; Computing exactsum [ i ] [ j ] [ k ] as explained above ; Driver code
let N = 101 ; let mod = 1e9 + 7 ; let exactsum = new Array ( N ) ; 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 ; } } } 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 ) { 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 ) ;