Sum of numbers formed with at most x 4s, y 5s and z 6s

Updated

Given three integers x,y and z you need to find the sum of all the numbers formed by having 4 atmost x times , having 5 atmost y times and having 6 atmost z times as a digit.

Examples :

Input : 1 1 1
Output : 3675
4+5+6+45+54+56+65+46+64+456+465+546+564+645+654=3675

Input : 4 5 6
Output : 129422134

Approach :

here as we have to find the sum of all the numbers having x 4s, y 5s,z 6s,we can notice that 
if we are able to find all the numbers having exact numbers of i 4s and j 5s and k 6s for all i<=x,j<=y,k<=z,
then we will sum them all and will output it so now we have to find the way to get all numbers as to simply considering all the
permutations it will be very costly.
As this problem have the property of subproblem overlapping and optimal substructure,we will solve it by using Dynamic  Programming
exactnum: exactnum[i][j][k] stores the exact number of numbers having i 4's, j 5's,k 6's.
Also if we already know the exactnum[i-1][j][k],exactnum[i][j-1][k] and exactnum[i][j][k-1],then we can observe that we are just
1 number away from all these numbers so we just have to sum all these,except in the case when exactnum[i-1][j][k] or exactnum[i][j-1][k]
or exactnum[i][j][k-1] doesn't exist ,inthat case we just have to skip it.
exactsum:
exactsum[i][j][k] stores the sum of exact number of numbers having i 4's, j 5's,k 6's.
in the same way as exactnum,
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]
	terms that don't exist will be simply skipped.
we will precompute the exactsum to x=100,y=100,z=100,starting from bottom.

Below is the implementation code: 

A cell (i, j, k) is built from the three cells before it by adding the digit 4, 5 or 6 at the end of their numbers, shown for i, j and k equal to 1.

Implementation :

//C++ program to find the sum of all the numbers formed by 
//having 4 atmost x times , having 5 atmost y times and 
//having 6 atmost z times as a digit.

#include<bits/stdc++.h>
using namespace std;
const  int m = 1e9+7;

//exactnum[i][j][k] stores numbers of numbers having exact number of
//4 i's,5 j's,6 k's
long long exactnum[101][101][101];
//exactsum[i][j][k] stores sum of all the numbers having exact
//number of 4 i's,5 j's, 6 k's
long long exactsum[101][101][101];


void preCompute()
{
	for( int i=0;i<=100;i++)
	{
		exactnum[0][0][i]=1;//initialize number of numbers having one 6
		exactnum[0][i][0]=1;//initialize number of numbers having one 5
		exactnum[i][0][0]=1;//initialize number of numbers having one 4
	}
	for( int i=0;i<=100;i++)
	{
		for( int j=0;j<=100;j++)
		{
			for( int k=0;k<=100;k++)
			{ //if two of the i,j,k are zero then no need to process,
				//as we already assigned
				if((i==0 and j==0) or (j==0 and k==0) or (i==0 and k==0)) 
					continue;
				exactnum[i][j][k]=0;
				//using the simple observation that number of numbers having 
				// i 4's, j 5's, k 6's is sum of number of numbers having (i-1,j,k) and
				//(i,j-1,k) and (i,j,k-1) (and obviously with mod m as it can be way big to store)
				//if i or j or k is zero then we don't have to add it as zero is minimum frequency
				if(i>0) exactnum[i][j][k]+=(exactnum[i-1][j][k])%m;
				if(j>0) exactnum[i][j][k]+=(exactnum[i][j-1][k])%m;
				if(k>0) exactnum[i][j][k]+=(exactnum[i][j][k-1])%m;
			}
		}
	}
	for( int i=0;i<=100;i++){
		for( int j=0;j<=100;j++){
			for( int k=0;k<=100;k++){
				 long long a=0,b=0,c=0;
				exactsum[i][j][k]=0;
				//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]
				//explained in explanation part
				if(i>0) a=((10*exactsum[i-1][j][k])%m+(4*exactnum[i-1][j][k])%m)%m;
				if(j>0) b=((10*exactsum[i][j-1][k])%m+(5*exactnum[i][j-1][k])%m)%m;
				if(k>0) c=((10*exactsum[i][j][k-1])%m+(6*exactnum[i][j][k-1])%m)%m;
				exactsum[i][j][k]=(a+b+c)%m;
			}
		}
	}
}


//Driver function
int main() {
	 int t,x,y,z,sumofall;
	preCompute();
	x = 1;
	y = 1;
	z = 1;
	
	long long sum=0;
	for( int i=0;i<=x;i++)
		for( int j=0;j<=y;j++)
			for( int k=0;k<=z;k++)
				sum+=(exactsum[i][j][k])%m;
			
			
		
    cout<<sum%m<<endl;
	
	return 0;
}

Output :  

3675
Comment
Next tutorial
Mithlesh Upadhyay Published 4 Dec 2026 · 5 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Related tutorials