fork download
  1. #include <iostream>
  2.  
  3. unsigned factorial(int n)
  4. {
  5. if (n == 1) return 1;
  6. return n * factorial(n-1);
  7. }
  8.  
  9. unsigned combinationsWithRepetition(int n, int k)
  10. {
  11. return factorial(n + k - 1) / (factorial(k) * factorial(n - 1));
  12. }
  13.  
  14. unsigned yourProblem(unsigned numberOfNumbers, unsigned result)
  15. {
  16. return combinationsWithRepetition(numberOfNumbers, result - numberOfNumbers);
  17. }
  18.  
  19. int main()
  20. {
  21. std::cout << yourProblem(4, 5) << std::endl;
  22. std::cout << yourProblem(4, 6) << std::endl;
  23. return 0;
  24. }
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
4
10