📜  纳拉亚纳数

📅  最后修改于: 2021-06-25 15:42:45             🧑  作者: Mango

在组合算术中,Narayana数N(n,k) ,n = 1、2、3…,1≤k≤n形成自然数的三角形阵列,称为Narayana三角形。它由下式给出:
{\displaystyle N(n,k)={\frac {1}{n}}{n \choose k}{n \choose k-1}.}
Narayana数字N(n,k)可用于查找包含n对括号的表达式的数目,这些括号正确匹配并且包含k个不同的嵌套。
例如,N(4,2)= 6以及四对括号可以创建六个序列,每个序列包含子模式’()’的两倍:

()((()))  (())(())  (()(()))  ((()()))  ((())())  ((()))()

例子:

Input : n = 6, k = 2
Output : 15

Input : n = 8, k = 5
Output : 490

下面是找到N(n,k)的实现:

C++
// CPP program to find Narayana number N(n, k)
#include
using namespace std;
 
// Return product of coefficient terms in formula
int productofCoefficiet(int n, int k)
{
    int C[n + 1][k + 1];
 
    // Calculate value of Binomial Coefficient
    // in bottom up manner
    for (int i = 0; i <= n; i++)
    {
        for (int j = 0; j <= min(i, k); j++)
        {
            // Base Cases
            if (j == 0 || j == i)
                C[i][j] = 1;
 
            // Calculate value using previously
            // stored values
            else
                C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
        }
    }
 
    return C[n][k] * C[n][k - 1];
}
 
// Returns Narayana number N(n, k)
int findNN(int n, int k)
{
    return (productofCoefficiet(n, k)) / n;
}
 
// Driven Program
int main()
{
    int n = 8, k = 5;
    cout << findNN(n, k) << endl;
    return 0;
}


Java
// Java program to find
// Narayana number N(n, k)
class GFG
{
         
    // Return product of coefficient
    // terms in formula
    static int productofCoefficiet(int n,
                                    int k)
    {
        int C[][] = new int[n + 1][k + 1];
     
        // Calculate value of Binomial
        // Coefficient in bottom up manner
        for (int i = 0; i <= n; i++)
        {
            for (int j = 0;
                    j <= Math.min(i, k); j++)
            {
                // Base Cases
                if (j == 0 || j == i)
                    C[i][j] = 1;
     
                // Calculate value using
                // previously stored values
                else
                    C[i][j] = C[i - 1][j - 1]
                                + C[i - 1][j];
            }
        }
     
        return C[n][k] * C[n][k - 1];
    }
     
    // Returns Narayana number N(n, k)
    static int findNN(int n, int k)
    {
        return (productofCoefficiet(n, k)) / n;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int n = 8, k = 5;
        System.out.println(findNN(n, k));
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python3 program to find Narayana number N(n, k)
 
# Return product of coefficient terms in formula
def productofCoefficiet(n, k):
    C = [[0 for x in range(k+1)] for y in range(n+1)]
 
    # Calculate value of Binomial Coefficient
    # in bottom up manner
    for i in range(0, n+1):
        for j in range(0, min(i+1,k+1)):
 
            # Base Cases
            if (j == 0 or j == i):
                C[i][j] = 1
  
            # Calculate value using previously
            # stored values
            else :
                C[i][j] = C[i - 1][j - 1] + C[i - 1][j]
  
    return C[n][k] * C[n][k - 1]
    
# Returns Narayana number N(n, k)
def findNN(n, k):   
    return (productofCoefficiet(n, k)) / n
 
# Driven Program
n = 8
k = 5
print(int(findNN(n, k)))
 
# This code is contributed by Prasad Kshirsagar


C#
// C# program to find
// Narayana number N(n, k)
using System;
 
class GFG {
 
    // Return product of coefficient
    // terms in formula
    static int productofCoefficiet(int n,
                                int k)
    {
        int[, ] C = new int[n + 1, k + 1];
 
        // Calculate value of Binomial
        // Coefficient in bottom up manner
        for (int i = 0; i <= n; i++) {
            for (int j = 0;
                j <= Math.Min(i, k); j++) {
                     
                // Base Cases
                if (j == 0 || j == i)
                    C[i, j] = 1;
 
                // Calculate value using
                // previously stored values
                else
                    C[i, j] = C[i - 1, j - 1]
                            + C[i - 1, j];
            }
        }
 
        return C[n, k] * C[n, k - 1];
    }
 
    // Returns Narayana number N(n, k)
    static int findNN(int n, int k)
    {
        return (productofCoefficiet(n, k)) / n;
    }
 
    // Driver code
    public static void Main()
    {
        int n = 8, k = 5;
        Console.WriteLine(findNN(n, k));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出:

490