📜  大堂编号

📅  最后修改于: 2021-09-17 07:13:04             🧑  作者: Mango

在组合数学中,Lobb 数L m, n计算 n + m 个括号可以排列以形成有效平衡括号序列的开头的方式数。
Lobb 数由两个非负整数 m 和 n 参数化,其中 n >= m >= 0。可以通过以下方式获得:
L_{m,n} = \frac{2\times m + 1}{m + n + 1}\binom{2\times n}{m + n}
Lobb Number 还用于计算值 +1 的 n + m 个副本和值 -1 的 n – m 个副本可以排列成一个序列的方式的数量,使得该序列的所有部分和都是非- 消极的。
例子 :

Input : n = 3, m = 2
Output : 5

Input : n =5, m =3
Output :35

这个想法很简单,我们使用一个函数来计算给定值的二项式系数。使用这个函数和上面的公式,我们可以计算 Lobb 数。

C++
// CPP Program to find Ln, m Lobb Number.
#include 
#define MAXN 109
using namespace std;
 
// Returns value of Binomial Coefficient C(n, k)
int binomialCoeff(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];
}
 
// Return the Lm, n Lobb Number.
int lobb(int n, int m)
{
    return ((2 * m + 1) * binomialCoeff(2 * n, m + n)) / (m + n + 1);
}
 
// Driven Program
int main()
{
    int n = 5, m = 3;
    cout << lobb(n, m) << endl;
    return 0;
}


Java
// JAVA Code For Lobb Number
import java.util.*;
 
class GFG {
     
    // Returns value of Binomial
    // Coefficient C(n, k)
    static int binomialCoeff(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];
    }
     
    // Return the Lm, n Lobb Number.
    static int lobb(int n, int m)
    {
        return ((2 * m + 1) * binomialCoeff(2 * n, m + n)) /
                                             (m + n + 1);
    }
     
    /* Driver program to test above function */
    public static void main(String[] args)
    {
        int n = 5, m = 3;
        System.out.println(lobb(n, m));
         
    }
}
 
// This code is contributed by Arnav Kr. Mandal.


Python 3
# Python 3 Program to find Ln,
# m Lobb Number.
 
# Returns value of Binomial
# Coefficient C(n, k)
def binomialCoeff(n, k):
 
    C = [[0 for j in range(k + 1)]
             for i 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, 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]
 
# Return the Lm, n Lobb Number.
def lobb(n, m):
 
    return (((2 * m + 1) *
        binomialCoeff(2 * n, m + n))
                      / (m + n + 1))
 
# Driven Program
n = 5
m = 3
print(int(lobb(n, m)))
 
# This code is contributed by
# Smitha Dinesh Semwal


C#
// C# Code For Lobb Number
using System;
 
class GFG {
 
    // Returns value of Binomial
    // Coefficient C(n, k)
    static int binomialCoeff(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];
    }
 
    // Return the Lm, n Lobb Number.
    static int lobb(int n, int m)
    {
        return ((2 * m + 1) * binomialCoeff(
                 2 * n, m + n)) / (m + n + 1);
    }
 
    /* Driver program to test above function */
    public static void Main()
    {
        int n = 5, m = 3;
         
        Console.WriteLine(lobb(n, m));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出 :

35

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程