Cpp dp solution


#1

int i,j,k;
if(sum==0 || n==0){
return 0;
}
int dp[n+1][sum+1];
memset(dp,0,sizeof(dp));
for(i=0;i<=9;i++)
dp[1][i]=1;
for(i=2;i<n;i++){
for(j=0;j<=sum;j++){
dp[i][j]=0;
for(k=0;k<=9;k++){
if(j-k>=0)
dp[i][j]=(dp[i][j]+(dp[i-1][j-k])%1000000007)%1000000007;
else
break;
}
}
}
dp[n][sum]=0;
for(k=1;n>1 &&k<=9;k++){
if((sum-k)>=0)
dp[n][sum]=(dp[n][sum]+(dp[n-1][sum-k])%1000000007)%1000000007;
// cout<<dp[n][sum]<<" ";
}
return dp[n][sum];


#2

The 7th line of your solution should be:
for(i=0; i<=min(9,sum); i++)


#3

it won’t affect anything anyways, because the number is so small.