编写一个函数,它接受两个参数 n 和 k,并返回二项式系数 C(n, k) 的值。
例子:
Input: n = 4 and k = 2
Output: 6
Explanation: 4 C 2 is 4!/(2!*2!) = 6
Input: n = 5 and k = 2
Output: 10
Explanation: 5 C 2 is 5!/(3!*2!) = 20
我们在这篇文章中讨论了 O(n*k) 时间和 O(k) 额外空间算法。 C(n, k) 的值可以在 O(k) 时间和 O(1) 额外空间内计算。
解决方案:
C(n, k)
= n! / (n-k)! * k!
= [n * (n-1) *....* 1] / [ ( (n-k) * (n-k-1) * .... * 1) *
( k * (k-1) * .... * 1 ) ]
After simplifying, we get
C(n, k)
= [n * (n-1) * .... * (n-k+1)] / [k * (k-1) * .... * 1]
Also, C(n, k) = C(n, n-k)
// r can be changed to n-r if r > n-r
- 如果 r 大于 nr,则将 r 更改为 nr。并创建一个变量来存储答案。
- 运行从 0 到 r-1 的循环
- 在每次迭代中将 ans 更新为 (ans*(ni))/(i+1) 其中 i 是循环计数器。
- 所以答案将等于 ((n/1)*((n-1)/2)*…*((n-r+1)/r!) 等于 nCr。
以下实现使用上述公式计算 C(n, k)。
C++
// Program to calculate C(n, k)
#include
using namespace std;
// Returns value of Binomial Coefficient C(n, k)
int binomialCoeff(int n, int k)
{
int res = 1;
// Since C(n, k) = C(n, n-k)
if (k > n - k)
k = n - k;
// Calculate value of
// [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
for (int i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
// Driver Code
int main()
{
int n = 8, k = 2;
cout << "Value of C(" << n << ", "
<< k << ") is " << binomialCoeff(n, k);
return 0;
}
// This is code is contributed by rathbhupendra
C
// Program to calculate C(n, k)
#include
// Returns value of Binomial Coefficient C(n, k)
int binomialCoeff(int n, int k)
{
int res = 1;
// Since C(n, k) = C(n, n-k)
if (k > n - k)
k = n - k;
// Calculate value of
// [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
for (int i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
/* Driver program to test above function*/
int main()
{
int n = 8, k = 2;
printf(
"Value of C(%d, %d) is %d ",
n, k, binomialCoeff(n, k));
return 0;
}
Java
// Program to calculate C(n, k) in java
class BinomialCoefficient {
// Returns value of Binomial Coefficient C(n, k)
static int binomialCoeff(int n, int k)
{
int res = 1;
// Since C(n, k) = C(n, n-k)
if (k > n - k)
k = n - k;
// Calculate value of
// [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
for (int i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
/* Driver program to test above function*/
public static void main(String[] args)
{
int n = 8;
int k = 2;
System.out.println("Value of C(" + n + ", " + k + ") "
+ "is"
+ " " + binomialCoeff(n, k));
}
}
// This Code is Contributed by Saket Kumar
Python
# Python program to calculate C(n, k)
# Returns value of Binomial Coefficient
# C(n, k)
def binomialCoefficient(n, k):
# since C(n, k) = C(n, n - k)
if(k > n - k):
k = n - k
# initialize result
res = 1
# Calculate value of
# [n * (n-1) *---* (n-k + 1)] / [k * (k-1) *----* 1]
for i in range(k):
res = res * (n - i)
res = res / (i + 1)
return res
# Driver program to test above function
n = 8
k = 2
res = binomialCoefficient(n, k)
print("Value of C(% d, % d) is % d" %(n, k, res))
# This code is contributed by Aditi Sharma
C#
// C# Program to calculate C(n, k)
using System;
class BinomialCoefficient {
// Returns value of Binomial
// Coefficient C(n, k)
static int binomialCoeff(int n, int k)
{
int res = 1;
// Since C(n, k) = C(n, n-k)
if (k > n - k)
k = n - k;
// Calculate value of [n * ( n - 1) *---* (
// n - k + 1)] / [k * (k - 1) *----* 1]
for (int i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
// Driver Code
public static void Main()
{
int n = 8;
int k = 2;
Console.Write("Value of C(" + n + ", " + k + ") "
+ "is"
+ " " + binomialCoeff(n, k));
}
}
// This Code is Contributed by
// Smitha Dinesh Semwal.
PHP
$n - $k )
$k = $n - $k;
// Calculate value of
// [n * (n-1) *---* (n-k+1)] /
// [k * (k-1) *----* 1]
for ($i = 0; $i < $k; ++$i)
{
$res *= ($n - $i);
$res /= ($i + 1);
}
return $res;
}
// Driver Code
$n = 8;
$k = 2;
echo " Value of C ($n, $k) is ",
binomialCoeff($n, $k);
// This code is contributed by ajit.
?>
Javascript
输出:
Value of C(8, 2) is 28
复杂度分析:
- 时间复杂度: O(r)。
循环必须从 0 运行到 r。因此,时间复杂度为 O(r)。 - 辅助空间: O(1)。
因为不需要额外的空间。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。