📜  三项三角形

📅  最后修改于: 2021-06-25 10:03:59             🧑  作者: Mango

三项式三角形是帕斯卡三角形的变体。两者之间的区别在于,三项式三角形中的一个条目是其上方三个(而不是Pasacal三角形中的两个)条目的总和:

6

n行的第k个条目由表示:
\binom nk _2
从0开始对行进行计数。从左边开始从-n索引第n行的条目,中间条目的索引为0。关于中间条目的行条目的对称性由以下关系表示:
\binom nk _2 = {\binom {n}{-k}} _2
特性 :

  • n行对应于被提升到第n次幂的三项式(1 + x + x 2 )的展开的多项式展开中的系数。
    {\huge (1 + x + x^2)^n = $\sum_{j=0}^{2n} {\binom {n}{j-n}}_2 x^j = \sum_{k=-n}^{n} {\binom {n}{k}}_2 x^{n+k}$ }
    或对称地
    {\huge (1 + x + \frac{1}{x})^n = $\sum_{k={-n}}^{n} {\binom {n}{k}}_2 x^k$}
    因此,替代名称三项式系数是因为它们与多项式系数的关系:
    \binom {n}{k}_2 = $\sum_{u+2v=n+k}^{} \frac{n!}{u! v! (n-u-v)!}$
  • 对角线具有相交的属性,例如它们与三角数的关系。
  • n行的元素之和为3 n

递归公式
可以使用以下递归公式生成三项式系数:
{\huge \binom {0}{0} = 1}
{\huge \binom {n+1}{k} = \binom{n}{k-1} + \binom{n}{k} + \binom{n}{k+1}}
在哪里,
{\huge \binom {n}{k} = 0    ,对于kn
应用范围:

  • 三角形对应于国王在国际象棋中可以采取的可能路径的数量。牢房中的条目代表国王到达牢房可采取的不同路径的数量(使用最小移动量)。

6

  • 多项式(1 + x + x 2 ) n中x k系数指定从两组n张相同的扑克牌中随机抽取k张牌的不同方式的数量。例如,在这样的纸牌游戏中,三张纸牌A,B,C中有两组,选择看起来像这样:

7

  • 给定正数n 。任务是打印高度为n的三项三角形。
    例子:
Input : n = 4
Output :
1
1 1 1
1 2 3 2 1
1 3 6 7 6 3 1

Input : n = 5
Output :
1
1 1 1 
1 2 3 2 1
1 3 6 7 6 3 1
1 4 10 16 19 16 10 4 1
  • 以下是打印三项三角og height n的实现:
C++
// CPP Program to print trinomial triangle.
#include
using namespace std;
 
// Function to find the trinomial triangle value.
int TrinomialValue(int n, int k)
{
    // base case
    if (n == 0 && k == 0)
        return 1;
         
    // base case
    if(k < -n || k > n)
        return 0;
         
    // recursive step.
    return TrinomialValue (n - 1, k - 1)
           + TrinomialValue (n - 1, k)
           + TrinomialValue (n - 1, k + 1);
}
 
// Function to print Trinomial Triangle of height n.
void printTrinomial(int n)
{
    // printing n rows.
    for (int i = 0; i < n; i++)
    {
        // printing first half of triangle
        for (int j = -i; j <= 0; j++)
            cout << TrinomialValue(i, j) << " ";
             
        // printing second half of triangle.
        for (int j = 1; j <= i; j++)
            cout << TrinomialValue(i, j) << " ";
         
        cout << endl;
    }
}
 
// Driven Program
int main()
{
    int n = 4;
 
    printTrinomial(n);
    return 0;
}


Java
// Java Program to print trinomial triangle.
import java.util.*;
import java.lang.*;
  
public class GfG {
  
    // Function to find the trinomial
    // triangle value.
    public static int TrinomialValue(int n,
                                     int k)
    {
        // base case
        if (n == 0 && k == 0)
            return 1;
  
        // base case
        if (k < -n || k > n)
            return 0;
  
        // recursive step.
        return TrinomialValue(n - 1, k - 1)
            + TrinomialValue(n - 1, k)
            + TrinomialValue(n - 1, k + 1);
    }
  
    // Function to print Trinomial
    // Triangle of height n.
    public static void printTrinomial(int n)
    {
        // printing n rows.
        for (int i = 0; i < n; i++)
        {
            // printing first half of triangle
            for (int j = -i; j <= 0; j++)
                System.out.print(TrinomialValue(i, j)
                                    + " ");
  
            // printing second half of triangle.
            for (int j = 1; j <= i; j++)
                System.out.print(TrinomialValue(i, j)
                                  + " ");
  
            System.out.println();
        }
    }
  
    // driver function
    public static void main(String argc[])
    {
        int n = 4;
  
        printTrinomial(n);
    }
  
}
 
/* This code is contributed by Sagar Shukla */


Python3
# Python3 code to print trinomial triangle.
 
# Function to find the trinomial triangle value.
def TrinomialValue(n, k):
    # base case
    if n == 0 and k == 0:
        return 1
     
    # base cas
    if k < -n or k > n:
        return 0
         
    # recursive step.
    return (TrinomialValue (n - 1, k - 1)+
                TrinomialValue (n - 1, k)+
                        TrinomialValue (n - 1, k + 1))
 
# Function to print Trinomial Triangle of height n.
def printTrinomial( n ):
 
    # printing n rows.
    for i in range(n):
 
        # printing first half of triangle
        for j in range(-i, 1):
            print(TrinomialValue(i, j),end=" ")
         
        # printing second half of triangle.
        for j in range(1, i+1):
            print( TrinomialValue(i, j),end=" ")
         
        print("\n",end='')
         
# Driven Code
n = 4
printTrinomial(n)
 
# This code is contributed by "Sharad_Bhardwaj".


C#
// C# Program to print trinomial triangle.
using System;
 
public class GfG {
 
    // Function to find the trinomial
    // triangle value.
    public static int TrinomialValue(int n,
                                    int k)
    {
        // base case
        if (n == 0 && k == 0)
            return 1;
 
        // base case
        if (k < -n || k > n)
            return 0;
 
        // recursive step.
        return TrinomialValue(n - 1, k - 1)
            + TrinomialValue(n - 1, k)
            + TrinomialValue(n - 1, k + 1);
    }
 
    // Function to print Trinomial
    // Triangle of height n.
    public static void printTrinomial(int n)
    {
        // printing n rows.
        for (int i = 0; i < n; i++)
        {
            // printing first half of triangle
            for (int j = -i; j <= 0; j++)
                Console.Write(TrinomialValue(i, j)
                                    + " ");
 
            // printing second half of triangle.
            for (int j = 1; j <= i; j++)
                Console.Write(TrinomialValue(i, j)
                                + " ");
 
            Console.WriteLine();
        }
    }
 
    // Driver function
    public static void Main()
    {
        int n = 4;
 
        printTrinomial(n);
    }
 
}
 
/* This code is contributed by Vt_m */


PHP
 $n)
        return 0;
         
    // recursive step.
    return TrinomialValue ($n - 1, $k - 1) +
           TrinomialValue ($n - 1, $k) +
           TrinomialValue ($n - 1, $k + 1);
}
 
// Function to print Trinomial
// Triangle of height n.
function printTrinomial($n)
{
    // printing n rows.
    for ($i = 0; $i < $n; $i++)
    {
        // printing first
        // half of triangle
        for ($j = -$i; $j <= 0; $j++)
            echo TrinomialValue($i, $j), " ";
             
        // printing second
        // half of triangle.
        for ($j = 1; $j <= $i; $j++)
            echo TrinomialValue($i, $j) , " ";
         
        echo "\n";
    }
}
 
// Driver Code
$n = 4;
 
printTrinomial($n);
     
// This code is contributed
// by ajit
?>


Javascript


C
// CPP Program to print trinomial triangle.
#include
#define MAX 10
using namespace std;
 
// Function to find the trinomial triangle value.
int TrinomialValue(int dp[MAX][MAX], int n, int k)
{
    // Using property of trinomial triangle.
    if (k < 0)
        k = -k;
         
    // If value already calculated, return that.
    if (dp[n][k] != 0)
        return dp[n][k];
         
    // base case
    if (n == 0 && k == 0)
        return 1;
         
    // base case
    if(k < -n || k > n)
        return 0;
         
    // recursive step and storing the value.
    return (dp[n][k] = TrinomialValue(dp, n - 1, k - 1)
           + TrinomialValue(dp, n - 1, k)
           + TrinomialValue(dp, n - 1, k + 1));
}
 
// Function to print Trinomial Triangle of height n.
void printTrinomial(int n)
{
    int dp[MAX][MAX] = { 0 };
         
    // printing n rows.
    for (int i = 0; i < n; i++)
    {
        // printing first half of triangle
        for (int j = -i; j <= 0; j++)
            cout << TrinomialValue(dp, i, j) << " ";
             
        // printing second half of triangle.
        for (int j = 1; j <= i; j++)
            cout << TrinomialValue(dp, i, j) << " ";
         
        cout << endl;
    }
}
 
// Driven Program
int main()
{
    int n = 4;
 
    printTrinomial(n);
    return 0;
}


Java
// Java Program to print trinomial triangle.
import java.util.*;
import java.lang.*;
  
public class GfG {
  
    private static final int MAX = 10;
  
    // Function to find the trinomial triangle value.
    public static int TrinomialValue(int dp[][], int n, int k)
    {
        // Using property of trinomial triangle.
        if (k < 0)
            k = -k;
  
        // If value already calculated, return that.
        if (dp[n][k] != 0)
            return dp[n][k];
  
        // base case
        if (n == 0 && k == 0)
            return 1;
  
        // base case
        if (k < -n || k > n)
            return 0;
  
        // recursive step and storing the value.
        return (dp[n][k] = TrinomialValue(dp, n - 1, k - 1)
                           + TrinomialValue(dp, n - 1, k)
                           + TrinomialValue(dp, n - 1, k + 1));
    }
  
    // Function to print Trinomial Triangle of height n.
    public static void printTrinomial(int n)
    {
        int[][] dp = new int[MAX][MAX];
  
        // printing n rows.
        for (int i = 0; i < n; i++) {
            // printing first half of triangle
            for (int j = -i; j <= 0; j++)
                System.out.print(TrinomialValue(dp, i, j) + " ");
  
            // printing second half of triangle.
            for (int j = 1; j <= i; j++)
                System.out.print(TrinomialValue(dp, i, j) + " ");
  
            System.out.println();
        }
    }
  
    // driver function
    public static void main(String argc[])
    {
        int n = 4;
        printTrinomial(n);
    }
  
}
/* This code is contributed by Sagar Shukla */


Python3
# Python3 code to print trinomial triangle.
 
# Function to find the trinomial triangle value.
def TrinomialValue(dp , n , k):
 
    # Using property of trinomial triangle.
    if k < 0:
        k = -k
     
    # If value already calculated, return that.
    if dp[n][k] != 0:
        return dp[n][k]
     
    # base case
    if n == 0 and k == 0:
        return 1
         
    # base case
    if k < -n or k > n:
        return 0
     
    # recursive step and storing the value.
    return  (TrinomialValue(dp, n - 1, k - 1) +
                TrinomialValue(dp, n - 1, k)+
                    TrinomialValue(dp, n - 1, k + 1))
 
# Function to print Trinomial Triangle of height n.
def printTrinomial(n):
    dp = [[0]*10]*10
     
    # printing n rows.
    for i in range(n):
 
        # printing first half of triangle
        for j in range(-i,1):
            print(TrinomialValue(dp, i, j),end=" ")
         
        # printing second half of triangle.
        for j in range(1,i+1):
            print(TrinomialValue(dp, i, j),end=" ")
        print("\n",end='')
 
# Driven Program
n = 4
printTrinomial(n)
 
# This code is contributed by "Sharad_Bhardwaj".


C#
// C# Program to print
// trinomial triangle.
using System;
 
class GFG
{
     
    private static int MAX = 10;
 
    // Function to find the
    // trinomial triangle value.
    public static int TrinomialValue(int [,]dp,
                                     int n, int k)
    {
        // Using property of
        // trinomial triangle.
        if (k < 0)
            k = -k;
 
        // If value already
        // calculated, return that.
        if (dp[n, k] != 0)
            return dp[n, k];
 
        // base case
        if (n == 0 && k == 0)
            return 1;
 
        // base case
        if (k < -n || k > n)
            return 0;
 
        // recursive step and storing the value.
        return (dp[n, k] = TrinomialValue(dp, n - 1,
                                              k - 1) +
                           TrinomialValue(dp, n - 1,
                                                  k) +
                           TrinomialValue(dp, n - 1,
                                              k + 1));
    }
 
    // Function to print Trinomial
    // Triangle of height n.
    public static void printTrinomial(int n)
    {
        int[,] dp = new int[MAX, MAX];
 
        // printing n rows.
        for (int i = 0; i < n; i++)
        {
            // printing first
            // half of triangle
            for (int j = -i; j <= 0; j++)
            Console.Write(TrinomialValue(dp, i,
                                         j) + " ");
 
            // printing second half
            // of triangle.
            for (int j = 1; j <= i; j++)
                Console.Write(TrinomialValue(dp, i,
                                             j) + " ");
 
            Console.WriteLine();
        }
    }
 
    // Driver Code
    static public void Main ()
    {
        int n = 4;
        printTrinomial(n);
    }
}
 
// This code is contributed by ajit


PHP
 $n)
        return 0;
         
    // recursive step and
    // storing the value.
    return ($dp[$n][$k] = TrinomialValue($dp, $n - 1, $k - 1) +
                          TrinomialValue($dp, $n - 1, $k) +
                          TrinomialValue($dp, $n - 1, $k + 1));
}
 
// Function to print Trinomial
// Triangle of height n.
function printTrinomial($n)
{
    global $MAX;
    $dp;
    for ($i = 0; $i < $MAX; $i++)
    for ($j = 0; $j < $MAX; $j++)
        $dp[$i][$j] = 0;
         
    // printing n rows.
    for ($i = 0; $i < $n; $i++)
    {
        // printing first
        // half of triangle
        for ($j = -$i; $j <= 0; $j++)
            echo TrinomialValue($dp, $i, $j)." ";
             
        // printing second
        // half of triangle.
        for ($j = 1; $j <= $i; $j++)
            echo TrinomialValue($dp, $i, $j)." ";
         
        echo "\n";
    }
}
 
// Driven Code
$n = 4;
printTrinomial($n);
 
// This code is contributed by mits
?>


Javascript


输出:

1 
1 1 1 
1 2 3 2 1 
1 3 6 7 6 3 1 
  • 以下是使用动态编程打印三项式三角形的实现以及三项式三角形的属性,即{\huge \binom {n}{k} = \binom {n}{-k}_2}

C

// CPP Program to print trinomial triangle.
#include
#define MAX 10
using namespace std;
 
// Function to find the trinomial triangle value.
int TrinomialValue(int dp[MAX][MAX], int n, int k)
{
    // Using property of trinomial triangle.
    if (k < 0)
        k = -k;
         
    // If value already calculated, return that.
    if (dp[n][k] != 0)
        return dp[n][k];
         
    // base case
    if (n == 0 && k == 0)
        return 1;
         
    // base case
    if(k < -n || k > n)
        return 0;
         
    // recursive step and storing the value.
    return (dp[n][k] = TrinomialValue(dp, n - 1, k - 1)
           + TrinomialValue(dp, n - 1, k)
           + TrinomialValue(dp, n - 1, k + 1));
}
 
// Function to print Trinomial Triangle of height n.
void printTrinomial(int n)
{
    int dp[MAX][MAX] = { 0 };
         
    // printing n rows.
    for (int i = 0; i < n; i++)
    {
        // printing first half of triangle
        for (int j = -i; j <= 0; j++)
            cout << TrinomialValue(dp, i, j) << " ";
             
        // printing second half of triangle.
        for (int j = 1; j <= i; j++)
            cout << TrinomialValue(dp, i, j) << " ";
         
        cout << endl;
    }
}
 
// Driven Program
int main()
{
    int n = 4;
 
    printTrinomial(n);
    return 0;
}

Java

// Java Program to print trinomial triangle.
import java.util.*;
import java.lang.*;
  
public class GfG {
  
    private static final int MAX = 10;
  
    // Function to find the trinomial triangle value.
    public static int TrinomialValue(int dp[][], int n, int k)
    {
        // Using property of trinomial triangle.
        if (k < 0)
            k = -k;
  
        // If value already calculated, return that.
        if (dp[n][k] != 0)
            return dp[n][k];
  
        // base case
        if (n == 0 && k == 0)
            return 1;
  
        // base case
        if (k < -n || k > n)
            return 0;
  
        // recursive step and storing the value.
        return (dp[n][k] = TrinomialValue(dp, n - 1, k - 1)
                           + TrinomialValue(dp, n - 1, k)
                           + TrinomialValue(dp, n - 1, k + 1));
    }
  
    // Function to print Trinomial Triangle of height n.
    public static void printTrinomial(int n)
    {
        int[][] dp = new int[MAX][MAX];
  
        // printing n rows.
        for (int i = 0; i < n; i++) {
            // printing first half of triangle
            for (int j = -i; j <= 0; j++)
                System.out.print(TrinomialValue(dp, i, j) + " ");
  
            // printing second half of triangle.
            for (int j = 1; j <= i; j++)
                System.out.print(TrinomialValue(dp, i, j) + " ");
  
            System.out.println();
        }
    }
  
    // driver function
    public static void main(String argc[])
    {
        int n = 4;
        printTrinomial(n);
    }
  
}
/* This code is contributed by Sagar Shukla */

Python3

# Python3 code to print trinomial triangle.
 
# Function to find the trinomial triangle value.
def TrinomialValue(dp , n , k):
 
    # Using property of trinomial triangle.
    if k < 0:
        k = -k
     
    # If value already calculated, return that.
    if dp[n][k] != 0:
        return dp[n][k]
     
    # base case
    if n == 0 and k == 0:
        return 1
         
    # base case
    if k < -n or k > n:
        return 0
     
    # recursive step and storing the value.
    return  (TrinomialValue(dp, n - 1, k - 1) +
                TrinomialValue(dp, n - 1, k)+
                    TrinomialValue(dp, n - 1, k + 1))
 
# Function to print Trinomial Triangle of height n.
def printTrinomial(n):
    dp = [[0]*10]*10
     
    # printing n rows.
    for i in range(n):
 
        # printing first half of triangle
        for j in range(-i,1):
            print(TrinomialValue(dp, i, j),end=" ")
         
        # printing second half of triangle.
        for j in range(1,i+1):
            print(TrinomialValue(dp, i, j),end=" ")
        print("\n",end='')
 
# Driven Program
n = 4
printTrinomial(n)
 
# This code is contributed by "Sharad_Bhardwaj".

C#

// C# Program to print
// trinomial triangle.
using System;
 
class GFG
{
     
    private static int MAX = 10;
 
    // Function to find the
    // trinomial triangle value.
    public static int TrinomialValue(int [,]dp,
                                     int n, int k)
    {
        // Using property of
        // trinomial triangle.
        if (k < 0)
            k = -k;
 
        // If value already
        // calculated, return that.
        if (dp[n, k] != 0)
            return dp[n, k];
 
        // base case
        if (n == 0 && k == 0)
            return 1;
 
        // base case
        if (k < -n || k > n)
            return 0;
 
        // recursive step and storing the value.
        return (dp[n, k] = TrinomialValue(dp, n - 1,
                                              k - 1) +
                           TrinomialValue(dp, n - 1,
                                                  k) +
                           TrinomialValue(dp, n - 1,
                                              k + 1));
    }
 
    // Function to print Trinomial
    // Triangle of height n.
    public static void printTrinomial(int n)
    {
        int[,] dp = new int[MAX, MAX];
 
        // printing n rows.
        for (int i = 0; i < n; i++)
        {
            // printing first
            // half of triangle
            for (int j = -i; j <= 0; j++)
            Console.Write(TrinomialValue(dp, i,
                                         j) + " ");
 
            // printing second half
            // of triangle.
            for (int j = 1; j <= i; j++)
                Console.Write(TrinomialValue(dp, i,
                                             j) + " ");
 
            Console.WriteLine();
        }
    }
 
    // Driver Code
    static public void Main ()
    {
        int n = 4;
        printTrinomial(n);
    }
}
 
// This code is contributed by ajit

的PHP

 $n)
        return 0;
         
    // recursive step and
    // storing the value.
    return ($dp[$n][$k] = TrinomialValue($dp, $n - 1, $k - 1) +
                          TrinomialValue($dp, $n - 1, $k) +
                          TrinomialValue($dp, $n - 1, $k + 1));
}
 
// Function to print Trinomial
// Triangle of height n.
function printTrinomial($n)
{
    global $MAX;
    $dp;
    for ($i = 0; $i < $MAX; $i++)
    for ($j = 0; $j < $MAX; $j++)
        $dp[$i][$j] = 0;
         
    // printing n rows.
    for ($i = 0; $i < $n; $i++)
    {
        // printing first
        // half of triangle
        for ($j = -$i; $j <= 0; $j++)
            echo TrinomialValue($dp, $i, $j)." ";
             
        // printing second
        // half of triangle.
        for ($j = 1; $j <= $i; $j++)
            echo TrinomialValue($dp, $i, $j)." ";
         
        echo "\n";
    }
}
 
// Driven Code
$n = 4;
printTrinomial($n);
 
// This code is contributed by mits
?>

Java脚本


输出:

1 
1 1 1 
1 2 3 2 1 
1 3 6 7 6 3 1