📌  相关文章
📜  Schröder–Hipparchus数

📅  最后修改于: 2021-05-07 18:37:24             🧑  作者: Mango

Schröder–Hipparchus数形成一个整数序列,该整数序列可用于计算具有给定叶子集的平面树的数量,在序列中插入括号的方式的数量以及将凸多边形剖成较小的方式的数量多边形通过插入对角线。

Schröder–Hipparchus数可以通过递归关系定义:
S\left ( n \right )= \begin{cases} 1 & \text{ if } n = 1 \ n = 2 \\ \frac{1 }{n}\left ( \left (6n-9 \right )S\left ( n-1 \right ) -\left (n-3 \right )S \left (n-2 \right )\right ) & otherwise \end{cases}

Schröder–Hipparchus数可用于计算几个紧密相关的组合对象:

  1. 序列中的第n个数字计算通过将原始多边形的对角线相加,将具有n +1个边的多边形细分为更小的多边形的不同方式的数量。有关详细信息,请参考此图像。
    图片来源:Wikiepedia
  2. 第n个数字计算具有n个叶子且所有内部顶点具有两个或多个子代的不同平面树的数量。
  3. 第n个数字用于计数将括号插入n个符号序列的不同方式的数量,其中每对括号都围绕两个或多个符号或括号组,而整个序列都没有任何括号。
  4. 第n个数字用于计算尺寸n为n的关联体Kn + 1的所有尺寸的面数。 1,包括缔合体本身作为面,但不包括空集。例如,二维缔合面体K4是五边形;反之亦然。它有5个顶点,5个面和一个完整的关联面体,总共11个面。
  5. 计算避免排列模式12312和121323的双排列数(从1到n的数的序列,每个数出现两次,每个数的第一次出现按排序顺序)。

例子:

Input : n = 5
Output : 45

Input : n = 6
Output : 197

一个简单的解决方案是简单地为数字实现递归公式。

C++
// A simple recursive CPP program to find n-th
// Schröder–Hipparchus number
#include 
using namespace std;
int nthSHN(int n)
{
    if (n == 1 || n == 2)
        return 1;
 
    return ((6 * n - 9) * nthSHN(n - 1) -
           (n - 3) * nthSHN(n - 2)) / n;
}
 
// Driven Program
int main()
{
    int n = 6;
    cout << nthSHN(n) << endl;
    return 0;
}


Java
// A simple recursive Java program to
// find n-th Schroder-Hipparchus number
class GFG
{
static int nthSHN(int n)
{
    if (n == 1 || n == 2)
        return 1;
 
    return ((6 * n - 9) * nthSHN(n - 1) -
        (n - 3) * nthSHN(n - 2)) / n;
}
 
// Driver code
public static void main (String[] args)
{
    int n = 6;
    System.out.println(nthSHN(n));
}
}
 
// This code is contributed by Anant Agarwal.


Python3
# A simple recursive Python3 program to
# find n-th Schröder–Hipparchus numberr
 
def nthSHN(n):
    if (n == 1 or n == 2):
        return 1
    else:
        return ((6 * n - 9) * nthSHN(n - 1) -
               ((n - 3) * nthSHN(n - 2))) / n
 
# Driven Program
n = 6
print (nthSHN(n))
 
# This code is contributed by Sachin Bisht


C#
// A simple recursive C# program to
// find n-th Schroder-Hipparchus number
using System;
 
class GFG
{
    static int nthSHN(int n)
    {
        if (n == 1 || n == 2)
            return 1;
     
        return ((6 * n - 9) * nthSHN(n - 1) -
                (n - 3) * nthSHN(n - 2)) / n;
    }
     
    // Driver code
    public static void Main ()
    {
        int n = 6;
        Console.WriteLine(nthSHN(n));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


C++
// A memoization based optimized CPP program to
// find n-th Schröder–Hipparchus number
#include 
#define MAX 500
using namespace std;
 
int nthSHN(int n, int dp[])
{
    if (n == 1 || n == 2)
        return dp[n] = 1;
 
    if (dp[n] != -1)
        return dp[n];
 
    return dp[n] = ((6 * n - 9) * nthSHN(n - 1, dp) -
                   (n - 3) * nthSHN(n - 2, dp)) / n;
}
 
// Driven Program
int main()
{
    int n = 6;
    int dp[MAX];
    memset(dp, -1, sizeof dp);
    cout << nthSHN(n, dp) << endl;
    return 0;
}


Java
// A memoization based optimized
// Java program to find n-th
// Schroder-Hipparchus number
import java.util.Arrays;
 
class GFG
{
static final int MAX=500;
 
static int nthSHN(int n, int dp[])
{
    if (n == 1 || n == 2)
        return dp[n] = 1;
 
    if (dp[n] != -1)
        return dp[n];
 
    return dp[n] = ((6 * n - 9) * nthSHN(n - 1, dp) -
                     (n - 3) * nthSHN(n - 2, dp)) / n;
}
 
// Driver code
public static void main (String[] args)
{
    int n = 6;
    int dp[] = new int[MAX];
    Arrays.fill(dp, -1);
    System.out.println(nthSHN(n, dp));
}
}
 
// This code is contributed by Anant Agarwal.


Python3
# A memoization based optimized
# Python3 program to find n-th
# Schröder–Hipparchus number
 
def nthSHN(n, dp):
    if (n == 1 or n == 2):
        dp[n] = 1
        return dp[n]
 
    if (dp[n] != -1):
        return dp[n]
 
    dp[n] = ((6 * n - 9) * nthSHN(n - 1, dp) -
             (n - 3) * nthSHN(n - 2, dp)) / n
    return dp[n]
 
# Driven Program
n = 6;
dp = [-1 for i in range(500)]
print (nthSHN(n, dp))
 
# This code is contributed by Sachin Bisht


C#
// A memoization based optimized
// C# program to find n-th
// Schroder-Hipparchus number
using System;
 
class GFG
{
static int MAX = 500;
 
static int nthSHN(int n, int[] dp)
{
    if (n == 1 || n == 2)
        return dp[n] = 1;
 
    if (dp[n] != -1)
        return dp[n];
 
    return dp[n] = ((6 * n - 9) *
                     nthSHN(n - 1, dp) -
                    (n - 3) *
                     nthSHN(n - 2, dp)) / n;
}
 
// Driver code
public static void Main ()
{
    int n = 6;
    int[] dp = new int[MAX];
    for(int i = 0; i < dp.Length; i++)
    dp[i] = -1;
    Console.Write(nthSHN(n, dp));
}
}
 
// This code is contributed by mits.


PHP


输出:

197

以下是找到第n个Schröder–Hipparchus编号的动态编程解决方案:

C++

// A memoization based optimized CPP program to
// find n-th Schröder–Hipparchus number
#include 
#define MAX 500
using namespace std;
 
int nthSHN(int n, int dp[])
{
    if (n == 1 || n == 2)
        return dp[n] = 1;
 
    if (dp[n] != -1)
        return dp[n];
 
    return dp[n] = ((6 * n - 9) * nthSHN(n - 1, dp) -
                   (n - 3) * nthSHN(n - 2, dp)) / n;
}
 
// Driven Program
int main()
{
    int n = 6;
    int dp[MAX];
    memset(dp, -1, sizeof dp);
    cout << nthSHN(n, dp) << endl;
    return 0;
}

Java

// A memoization based optimized
// Java program to find n-th
// Schroder-Hipparchus number
import java.util.Arrays;
 
class GFG
{
static final int MAX=500;
 
static int nthSHN(int n, int dp[])
{
    if (n == 1 || n == 2)
        return dp[n] = 1;
 
    if (dp[n] != -1)
        return dp[n];
 
    return dp[n] = ((6 * n - 9) * nthSHN(n - 1, dp) -
                     (n - 3) * nthSHN(n - 2, dp)) / n;
}
 
// Driver code
public static void main (String[] args)
{
    int n = 6;
    int dp[] = new int[MAX];
    Arrays.fill(dp, -1);
    System.out.println(nthSHN(n, dp));
}
}
 
// This code is contributed by Anant Agarwal.

Python3

# A memoization based optimized
# Python3 program to find n-th
# Schröder–Hipparchus number
 
def nthSHN(n, dp):
    if (n == 1 or n == 2):
        dp[n] = 1
        return dp[n]
 
    if (dp[n] != -1):
        return dp[n]
 
    dp[n] = ((6 * n - 9) * nthSHN(n - 1, dp) -
             (n - 3) * nthSHN(n - 2, dp)) / n
    return dp[n]
 
# Driven Program
n = 6;
dp = [-1 for i in range(500)]
print (nthSHN(n, dp))
 
# This code is contributed by Sachin Bisht

C#

// A memoization based optimized
// C# program to find n-th
// Schroder-Hipparchus number
using System;
 
class GFG
{
static int MAX = 500;
 
static int nthSHN(int n, int[] dp)
{
    if (n == 1 || n == 2)
        return dp[n] = 1;
 
    if (dp[n] != -1)
        return dp[n];
 
    return dp[n] = ((6 * n - 9) *
                     nthSHN(n - 1, dp) -
                    (n - 3) *
                     nthSHN(n - 2, dp)) / n;
}
 
// Driver code
public static void Main ()
{
    int n = 6;
    int[] dp = new int[MAX];
    for(int i = 0; i < dp.Length; i++)
    dp[i] = -1;
    Console.Write(nthSHN(n, dp));
}
}
 
// This code is contributed by mits.

的PHP


输出:

197

来源:
https://zh.wikipedia.org/wiki/Schr%C3%B6der%E2%80%93Hipparchus_number