Ways to sum to N using Natural Numbers up to K with repetitions allowed | C ++ program for the above approach ; Function to find the total number of ways to represent N as the sum of integers over the range [ 1 , K ] ; Initialize a list ; Update dp [ 0 ] to 1 ; Iterate over the range [ 1 , K + 1 ] ; Iterate over the range [ 1 , N + 1 ] ; If col is greater than or equal to row ; Update current dp [ col ] state ; Return the total number of ways ; Driver Code ; Given inputs
#include <bits/stdc++.h> NEW_LINE using namespace std ; int NumberOfways ( int N , int K ) { vector < int > dp ( N + 1 , 0 ) ; dp [ 0 ] = 1 ; for ( int row = 1 ; row < K + 1 ; row ++ ) { for ( int col = 1 ; col < N + 1 ; col ++ ) { if ( col >= row ) dp [ col ] = dp [ col ] + dp [ col - row ] ; } } return ( dp [ N ] ) ; } int main ( ) { int N = 8 ; int K = 2 ; cout << ( NumberOfways ( N , K ) ) ; }