จำนวนคำตอบจำนวนเต็มของสมการ:
- Non-negative: C(n+k-1, k)
- Positive: C(k-1, n-1)
ตัวอย่าง:
Input: n = 4, k = 7 Output: 140
อัลกอริทึม
- กำหนด n, k
- คำนวณ factorial และ nCr
- คำตอบ = nCr(n+k-1, k) + nCr(k-1, n-1)
การเขียนโปรแกรม C++
#include <bits/stdc++.h> using namespace std; int factorial(int n) { int product = 1; for (int i = 2; i <= n; i++) product *= i; return product; } int nCr(int n, int r) { return factorial(n) / (factorial(n - r) * factorial(r)); } int main() { int n = 4, k = 7; cout << nCr(n + k - 1, k) + nCr(k - 1, n - 1) << endl; return 0; }
ผลลัพธ์
140