숫자 n이 있다고 가정하고 $$\left(\begin{array}{c}n\\ 0\end{array}\right)+\left(\begin {array}{c}n\\ 2\end{array}\right)+\left(\begin{array}{c}n\\ 4\end{array}\right)+\left(\begin{array }{c}n\\ 6\end{array}\right)+...\left(\begin{array}{c}4\\ 0\end{array}\right)+\left(\begin{ array}{c}4\\ 2\end{array}\right)+\left(\begin{array}{c}4\\ 4\end{array}\right)++=1+6+1=8$$
그래서 여기에서 모든 이항 계수를 찾은 다음 짝수 인덱스 값의 합만 찾습니다.
예
#include<iostream> using namespace std; int evenIndexedTermSum(int n) { int coeff[n + 1][n + 1]; for (int i = 0; i <= n; i++) { for (int j = 0; j <= min(i, n); j++) { if (j == 0 || j == i) coeff[i][j] = 1; else coeff[i][j] = coeff[i - 1][j - 1] + coeff[i - 1][j]; } } int sum = 0; for (int i = 0; i <= n; i += 2) sum += coeff[n][i]; return sum; } int main() { int n = 8; cout << "Sum of even placed binomial coefficients: " <<evenIndexedTermSum(n); }
출력
Sum of even placed binomial coefficients: 128