📜  用 N 颗珠子组成 2 条项链的方法计数,每颗珠子包含 N/2 颗珠子

📅  最后修改于: 2022-05-13 01:56:07.889000             🧑  作者: Mango

用 N 颗珠子组成 2 条项链的方法计数,每颗珠子包含 N/2 颗珠子

给定一个正偶整数N表示不同珠子的数量,任务是找到制作 2 条正好有N/2珠子的项链的方法数。

例子:

方法:这个问题可以通过使用循环置换和组合的概念来解决。请按照以下步骤解决问题:

  • 按照以下步骤定义一个函数,例如计算一个数的阶乘的阶乘:
    • 基本情况:如果n = 0 ,则返回1
    • 如果n != 0 ,则递归调用函数并返回n * factorial(n-1)
  • 初始化一个变量,比如说, ansC(N, N/2),即从N个珠子中选择N/2 个珠子的方法数。
  • 由于项链是圆形的,排列N/2颗珠子的方式数是阶乘(N/2 -1),所以将ans的值乘以阶乘(N/2 -1)*阶乘(N/2-1)因为有两条项链。
  • 现在将ans除以2。因为对称分布。例如,对于N=2 ,分布{1 | 2}{2 | 1}被认为是相同的。
  • 最后,完成以上步骤后,打印ans的值作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to calculate factorial
int factorial(int n)
{
    if (n == 0)
        return 1;
    return n * factorial(n - 1);
}
 
// Function to count number of ways
// to make 2 necklace having exactly
// N/2 beads if each bead is
// considered different
long long numOfNecklace(int N)
{
    // Number of ways to choose N/2 beads
    // from N beads
    long long ans = factorial(N)
                    / (factorial(N / 2) * factorial(N / 2));
 
    // Number of ways to permute N/2 beads
    ans = ans * factorial(N / 2 - 1);
    ans = ans * factorial(N / 2 - 1);
 
    // Divide ans by 2 to remove repetitions
    ans /= 2;
 
    // Return ans
    return ans;
}
 
// Driver Code
int main()
{
    // Given Input
    int N = 4;
 
    // Function Call
    cout << numOfNecklace(N) << endl;
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
class GFG
{
   
    // Function to calculate factorial
    static int factorial(int n)
    {
        if (n == 0)
            return 1;
        return n * factorial(n - 1);
    }
   
    // Function to count number of ways
    // to make 2 necklace having exactly
    // N/2 beads if each bead is
    // considered different
    static long numOfNecklace(int N)
    {
       
        // Number of ways to choose N/2 beads
        // from N beads
        long ans = factorial(N)
                   / (factorial(N / 2) * factorial(N / 2));
 
        // Number of ways to permute N/2 beads
        ans = ans * factorial(N / 2 - 1);
        ans = ans * factorial(N / 2 - 1);
 
        // Divide ans by 2 to remove repetitions
        ans /= 2;
 
        // Return ans
        return ans;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
       
        // Given Input
        int N = 4;
 
        // Function Call
        System.out.println(numOfNecklace(N));
 
    }
}
 
// This code is contributed by Potta Lokesh


Python3
# Python 3 program for the above approach
 
# Function to calculate factorial
def factorial(n):
    if (n == 0):
        return 1
    return n * factorial(n - 1)
 
# Function to count number of ways
# to make 2 necklace having exactly
# N/2 beads if each bead is
# considered different
def numOfNecklace(N):
   
    # Number of ways to choose N/2 beads
    # from N beads
    ans = factorial(N) // (factorial(N // 2) * factorial(N // 2))
 
    # Number of ways to permute N/2 beads
    ans = ans * factorial(N // 2 - 1)
    ans = ans * factorial(N // 2 - 1)
 
    # Divide ans by 2 to remove repetitions
    ans //= 2
 
    # Return ans
    return ans
 
# Driver Code
if __name__ == '__main__':
    # Given Input
    N = 4
 
    # Function Call
    print(numOfNecklace(N))
     
    # This code is contributed by ipg2016107.


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to calculate factorial
static int factorial(int n)
{
    if (n == 0)
        return 1;
         
    return n * factorial(n - 1);
}
 
// Function to count number of ways
// to make 2 necklace having exactly
// N/2 beads if each bead is
// considered different
static long numOfNecklace(int N)
{
     
    // Number of ways to choose N/2 beads
    // from N beads
    long ans = factorial(N) /
              (factorial(N / 2) *
               factorial(N / 2));
 
    // Number of ways to permute N/2 beads
    ans = ans * factorial(N / 2 - 1);
    ans = ans * factorial(N / 2 - 1);
 
    // Divide ans by 2 to remove repetitions
    ans /= 2;
 
    // Return ans
    return ans;
}
 
// Driver Code
static public void Main ()
{
     
    // Given Input
    int N = 4;
 
    // Function Call
    Console.Write( numOfNecklace(N));
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出
3

时间复杂度: O(N)
辅助空间: O(1)