The above C++ program that accepts different numbers n as well as an integer s which is equal to the sum of the n different numbers, can be understood with the following example :
If n = 3 and s = 6:
1 + 2 + 3 = 6
0 + 1 + 5 = 6
0 + 2 + 4 = 6
Output: Number of combination: 3
#include <iostream>
#define range(i,a,b) for(int (i)=(a);(i)<(b);(i)++)
#define rep(i,n) range(i,0,n)
using namespace std;
int n,s;
long long int combination[10][1010];
int main(void){
combination[0][0]=1LL;
rep(i,101){
for(int j=8;j>=0;j--)rep(k,1010){
if(k+i<=1010)
combination[j+1][k+i]+=combination[j][k];
}
}
cout << "Input numbers n and s: ";
cin >> n >> s;
cout << "\nNumber of combinations for the above numbers is: ";
cout << combination[n][s] << endl;
return 0;
}
Sample Output: Input numbers n and s: 4 6 Number of combinations for the above numbers is: 1