📜  第n个Fuss–Catalan号程序

📅  最后修改于: 2021-05-04 09:59:33             🧑  作者: Mango

弗斯-加泰罗尼亚数字Catalan数的推广使用三胞胎,而不是对

Fuss-Catalan数字可以由具有以下公式的系列表示:

前几个Fuss–Catalan数字是

福斯-加泰罗尼亚语编号的应用:

  1. 计算将2n + 1个数字中的括号放在一起的方式,这些括号将被一次分组为三个。
    示例:有3种方法将{1、2、3、4、5}括起来作为三元组:
    {{1,2,3},4,5},{1,{2,3,4},5},{1,2,{3,4,5}}
  2. 计算具有n个内部节点的完整三叉树的数量。

  1. 通过不跨越主对角线的2n x n网格计算长度为3n的路径数
    示例:从(0,0)到(4,2)的3条路径不跨越对角线:

  1. 还有很多。请参考此链接以了解更多应用

Fuss-Catalan编号的实现:

C++
// C++ program for nth Fuss–Catalan Number
 
#include 
using namespace std;
 
// Returns value of Binomial Coefficient C(n, k)
unsigned long int binomialCoeff(unsigned int n,
                                unsigned int k)
{
    unsigned long 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;
}
 
// A Binomial coefficient based function
// to find nth Fuss–Catalan number in O(n) time
unsigned long int Fuss_catalan(unsigned int n)
{
    // Calculate value of 3nCn
    unsigned long int c = binomialCoeff(3 * n, n);
 
    // return 3nCn/(2n+1)
    return c / (2 * n + 1);
}
 
// Driver code
int main()
{
    for (int i = 0; i < 10; i++)
        cout << Fuss_catalan(i) << " ";
    return 0;
}


Java
// Java program for nth Fuss-Catalan Number
class GFG
{
 
// 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;
}
 
// A Binomial coefficient based function
// to find nth Fuss-Catalan number in O(n) time
static int Fuss_catalan(int n)
{
    // Calculate value of 3nCn
    int c = binomialCoeff(3 * n, n);
 
    // return 3nCn/(2n+1)
    return c / (2 * n + 1);
}
 
// Driver code
public static void main(String []args)
{
    for (int i = 0; i < 10; i++)
        System.out.print(Fuss_catalan(i) + " ");
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for nth Fuss–Catalan Number
 
# Returns value of Binomial Coefficient C(n, k)
def binomialCoeff(n, k) :
 
    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 i in range(k) :
         
        res *= (n - i);
        res //= (i + 1);
 
    return res;
 
# A Binomial coefficient based function
# to find nth Fuss–Catalan number in O(n) time
def Fuss_catalan(n) :
 
    # Calculate value of 3nCn
    c = binomialCoeff(3 * n, n);
     
    # return 3nCn/(2n+1)
    return c // (2 * n + 1);
 
# Driver code
if __name__ == "__main__" :
 
    for i in range(10) :
        print(Fuss_catalan(i), end = " ");
 
# This code is contributed by AnkitRai01


C#
// C# program for nth Fuss-Catalan Number
using System;
 
class GFG
{
 
// 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;
}
 
// A Binomial coefficient based function
// to find nth Fuss-Catalan number in O(n) time
static int Fuss_catalan(int n)
{
    // Calculate value of 3nCn
    int c = binomialCoeff(3 * n, n);
 
    // return 3nCn/(2n+1)
    return c / (2 * n + 1);
}
 
// Driver code
public static void Main(String []args)
{
    for (int i = 0; i < 10; i++)
        Console.Write(Fuss_catalan(i) + " ");
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
1 1 3 12 55 273 1428 7752 43263 246675

时间复杂度: O(n)