📜  用关联运算将n个元素相乘的方法

📅  最后修改于: 2021-04-30 02:51:29             🧑  作者: Mango

给定数字n,请找到用关联运算将n个元素相乘的方式的数量。

例子 :

Input : 2
Output : 2
For a and b there are two ways to multiply them.
1. (a * b)        
2. (b * a)

Input : 3
Output : 12

说明(示例2):

For a, b and c there are 12 ways to multiply them.
1.  ((a * b) * c)     2.  (a * (b * c))
3.  ((a * c) * b)     4.  (a * (c * b))
5.  ((b * a) * c)     6.  (b * (a * c))
7.  ((b * c) * a)     8.  (b * (c * a))
9.  ((c * a) * b)     10.  (c * (a * b))
11.  ((c * b) * a)    12.  (c * (b * a))

方法:首先,我们尝试找出递归关系。从上面的示例中,我们可以看到h(1)= 1,h(2)= 2,h(3)= 12。现在,对于n个元素,将有n – 1个乘法和n – 1个括号。并且,(a1,a2,…,an)可以通过以下两种方式之一从(a1,a2,…,a(n – 1))获得:

  1. 取一个乘法(a1,a2,…,a(n – 1))(具有n – 2个乘法和n – 2个括号),然后将第n个元素“ an”插入任一n之一的任一侧。 2个乘法。因此,对于n个数字的每个方案,以这种方式给出n个数字的2 * 2 *(n-2)= 4 *(n-2)个方案。
  2. 采取(a1,a2,..,a(n-1))的乘法方案,并在左边或右边乘以(’an’)。因此,对于n – 1个数字的每个方案,以这种方式给出了n个数字的两个方案。

因此,将以上两个相加后,我们得到h(n)=(4 * n – 8 + 2)* h(n – 1),h(n)=(4 * n – 6)* h(n – 1) 。具有相同初始值的该递归关系由伪加泰罗尼亚数满足。因此,h(n)=(2 * n – 2)! /(n – 1)!

C++
// CPP code to find number of ways to multiply n 
// elements with an associative operation
# include 
using namespace std;
  
// Function to find the required factorial
int fact(int n)
{
    if (n == 0 || n == 1)    
        return 1 ;
  
    int ans = 1;   
    for (int i = 1 ; i <= n; i++)    
        ans = ans * i ; 
  
    return ans ;
}
  
// Function to find nCr
int nCr(int n, int r)
{
    int Nr = n , Dr = 1 , ans = 1;
    for (int i = 1 ; i <= r ; i++ ) {
        ans = ( ans * Nr ) / ( Dr ) ;
        Nr-- ;
        Dr++ ;
    }
    return ans ;
}
  
// function to find the number of ways
int solve ( int n )
{
    int N = 2*n - 2 ;
    int R = n - 1 ;    
    return nCr (N, R) * fact(n - 1) ;
}
  
// Driver code
int main()
{
    int n = 6 ;
    cout << solve (n) ;    
    return 0 ;
}


Java
// Java code to find number of 
// ways to multiply n elements 
// with an associative operation
import java.io.*;
  
class GFG 
{
// Function to find the
// required factorial
static int fact(int n)
{
    if (n == 0 || n == 1) 
        return 1 ;
  
    int ans = 1; 
    for (int i = 1 ; i <= n; i++) 
        ans = ans * i ; 
  
    return ans ;
}
  
// Function to find nCr
static int nCr(int n, int r)
{
    int Nr = n , Dr = 1 , ans = 1;
    for (int i = 1 ; i <= r ; i++ ) 
    {
        ans = ( ans * Nr ) / ( Dr ) ;
        Nr-- ;
        Dr++ ;
    }
    return ans ;
}
  
// function to find
// the number of ways
static int solve ( int n )
{
    int N = 2 * n - 2 ;
    int R = n - 1 ; 
    return nCr (N, R) * fact(n - 1) ;
}
  
// Driver Code
public static void main (String[] args) 
{
int n = 6 ;
System.out.println( solve (n)) ; 
}
}
  
// This code is contributed by anuj_67.


Python3
# Python3 code to find number
# of ways to multiply n 
# elements with an
# associative operation
  
# Function to find the
# required factorial
def fact(n):
    if (n == 0 or n == 1): 
        return 1;
  
    ans = 1; 
    for i in range(1, n + 1): 
        ans = ans * i; 
  
    return ans;
  
# Function to find nCr
def nCr(n, r):
    Nr = n ; Dr = 1 ; ans = 1;
    for i in range(1, r + 1):
        ans = int((ans * Nr) / (Dr));
        Nr = Nr - 1;
        Dr = Dr + 1;
    return ans;
  
# function to find 
# the number of ways
def solve ( n ):
    N = 2* n - 2;
    R = n - 1 ; 
    return (nCr (N, R) * 
            fact(n - 1));
  
# Driver code
n = 6 ;
print(solve (n) ); 
  
# This code is contributed
# by mits


C#
// C# code to find number of 
// ways to multiply n elements 
// with an associative operation
using System;
  
class GFG {
      
    // Function to find the
    // required factorial
    static int fact(int n)
    {
        if (n == 0 || n == 1) 
            return 1 ;
      
        int ans = 1; 
        for (int i = 1 ; i <= n; i++) 
            ans = ans * i ; 
      
        return ans ;
    }
      
    // Function to find nCr
    static int nCr(int n, int r)
    {
        int Nr = n , Dr = 1 , ans = 1;
        for (int i = 1 ; i <= r ; i++ ) 
        {
            ans = ( ans * Nr ) / ( Dr ) ;
            Nr-- ;
            Dr++ ;
        }
        return ans ;
    }
      
    // function to find
    // the number of ways
    static int solve ( int n )
    {
        int N = 2 * n - 2 ;
        int R = n - 1 ; 
        return nCr (N, R) * fact(n - 1) ;
    }
      
    // Driver Code
    public static void Main () 
    {
        int n = 6 ;
        Console.WriteLine( solve (n)) ; 
    }
}
  
// This code is contributed by anuj_67.


PHP


输出 :
30240