打印二项式展开式程序
二项式展开是一个数学公式,用于展开形如 (a+b)^n 的表达式,其中 n 是一个正整数,a 和 b 可以是任何实数或复数。展开式给出了展开式中各项的系数。
二项式展开可以表示为
$$\mathrm{(a+b)^n= ^nC_0a^nb^0+ ^nC_1a^{n-1}b^1 + ^nCa^{n-2}b^2+...+ ^nC_ra^{n-r}b^r+...+ ^nC_na^0b^n}$$
其中 $\mathrm{^nC_r}$ 是二项式系数,由下式给出
$\mathrm{^nC_r=\frac{n!}{r!\times(n−r)!}}$ 其中 n! 是 n 的阶乘
该展开式可用于使用上述公式计算所有二项式项,并将其代入展开式方程。
问题陈述
给定三个整数 a、b 和 n。求 (a+b)^n 的二项式展开式的各项。
示例 1
输入 -
a = 1, b = 2, n = 3
输出 -
[1, 6, 12, 8]
解释
(1+2)^3 的二项式展开式如下
$\mathrm{(1+2)^3 = C(3,0)a^3b^0 + C(3,1)a^2b^1 + C(3,2)a^1b^2 + C(3,3)a^0b^3}$
= 1*1*1 + 3*1*2 + 3*1*4 + 1*1*8
因此,[1, 6, 12, 8] 是二项式展开式的各项。
示例 2
输入 -
a = 7, b = 2, n = 11
输出 -
[2401, 2744, 1176, 224, 16]
方法 1:递归二项式展开
使用二项式展开公式,
$$\mathrm{(a+b)^n= ^nC_0a^nb^0+ ^nC_1a^{n-1}b^1 + ^nCa^{n-2}b^2+...+ ^nC_ra^{n-r}b^r+...+ ^nC_na^0b^n}$$
我们可以通过递归计算二项式系数来找到每一项的值。
伪代码
procedure binomialCoeff (n, r)
if r == 0 or r == n
ans = 1
else
ans = binomialCoeff (n - 1, r - 1) + binomialCoeff (n - 1, r)
end procedure
procedure binomialTerms (a, b, n)
Initialize vector: arr
for r = 0 to n
coeff = binomialCoeff(n, r)
term = coeff + a^n-r + b^r
add the term to arr
ans = arr
end procedure
示例:C++ 实现,
在以下程序中,binomialCoeff() 函数递归计算第 r 个二项式系数的值,而 binomialTerms() 函数计算展开式中二项式项的值。
#include <bits/stdc++.h>
using namespace std;
// Function for calculating binomial coefficients
int binomialCoeff(int n, int r){
if (r == 0 || r == n) {
return 1;
} else {
return binomialCoeff(n - 1, r - 1) + binomialCoeff(n - 1, r);
}
}
// Function for calculating the binomial terms
vector<int> binomialTerms(int a, int b, int n){
vector<int> ans;
for (int r = 0; r <= n; r++) {
// Calculate the rth binomial coefficients
int coeff = binomialCoeff(n, r);
// Calculate the rth binomial expansion term
int term = coeff * pow(a, n - r) * pow(b, r);
ans.push_back(term);
}
return ans;
}
int main(){
int a = 2, b = 3, n = 4;
vector<int> res = binomialTerms(a, b, n);
cout << "The binomial terms are : ";
for (int i = 0; i < res.size(); i++) {
cout << res[i] << " ";
}
return 0;
}
输出
The binomial terms are : 16 96 216 216 81
时间复杂度 - O(2^n),其中 binomialCoeff() 函数的时间复杂度为 O(2^n),因为递归树中有 2^n 个节点,而 binomialTerms() 函数由于嵌套循环调用 binomialCoeff() n+1 次,因此复杂度为 O(n^2)。因此,总体复杂度为 O(2^n)。
空间复杂度 - 由于递归调用栈,空间复杂度为 O(n)。
方法 2:迭代二项式展开
使用二项式展开公式,
$$\mathrm{(a+b)^n= ^nC_0a^nb^0+ ^nC_1a^{n-1}b^1 + ^nCa^{n-2}b^2+...+ ^nC_ra^{n-r}b^r+...+ ^nC_na^0b^n}$$
我们可以通过结合迭代和除法来找到该展开式中每一项的值。
我们将创建 2 个函数,其中第一个函数计算二项式系数,第二个函数将 a 和 b 的幂相乘以获得所需的二项式项。
伪代码
procedure binomialCoeff (n, r)
res = 1
if r > n - r
r = n - r
end if
for i = 0 to r-1
res = res * (n - i)
res = res / (i + 1)
ans = res
end procedure
procedure binomialTerms (a, b, n)
Initialize vector: arr
for r = 0 to n
coeff = binomialCoeff(n, r)
term = coeff + a^n-r + b^r
add the term to arr
ans = arr
end procedure
示例:C++ 实现
在以下程序中,binomialCoeff() 函数计算第 r 个二项式系数,而 binomialTerms() 函数计算给定 a、b 和 n 的二项式展开式的所有项。
#include <bits/stdc++.h>
using namespace std;
// Function for calculating binomial coefficients
int binomialCoeff(int n, int r){
int res = 1;
if (r > n - r) {
r = n - r;
}
for (int i = 0; i < r; i++) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
// Function for calculating the binomial terms
vector<int> binomialTerms(int a, int b, int n){
vector<int> ans;
for (int r = 0; r <= n; r++){
// Calculate the rth binomial coefficients
int coeff = binomialCoeff(n, r);
// Calculate the rth binomial expansion term
int term = coeff * pow(a, n - r) * pow(b, r);
ans.push_back(term);
}
return ans;
}
int main(){
int a = 2, b = 3, n = 4;
vector<int> res = binomialTerms(a, b, n);
cout << "The binomial terms are : ";
for (int i = 0; i < res.size(); i++){
cout << res[i] << " ";
}
return 0;
}
输出
The binomial terms are : 16 96 216 216 81
时间复杂度 - O(n^2),其中 binomialCoeff() 函数的时间复杂度为 O(r),其中 r 是 r 和 n-r 中较小的数,而 binomialTerms() 函数由于嵌套循环调用 binomialCoeff() n+1 次,因此复杂度为 O(n^2)。因此,总体复杂度为 O(n^2)。
空间复杂度 - 由于存储二项式项的向量,空间复杂度为 O(n)。
结论
总之,要找到二项式展开式的二项式项,我们可以实现上述两种方法中的任何一种,时间复杂度从 O(2^n) 到 O(n^2),其中迭代方法比递归方法更优化。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP