📌  相关文章
📜  在R个不同的组中分配N个相同的对象(无组为空)的方式数量

📅  最后修改于: 2021-05-07 04:44:10             🧑  作者: Mango

给定两个整数NR ,任务是计算将N个相同的对象分配到R个不同的组中的方式的数量,以使任何组都不为空。

例子:

方法:想法是使用多项式定理。让我们假设将x 1个对象放置在第一组中,将x 2个对象放置在第二组中,将x个R对象放置在第R组中。据认为,
X 1 + X 2 + X 3 + … + X R = N代表所有X I≥1 1≤I≤[R
现在,对于所有1≤i≤R,y i +1替换每个x i 。现在,所有y变量均大于或等于零。
等式变成
Y 1 + Y 2 + Y 3 + … + Y R + R = N代表所有的Y I≥0 1≤I≤[R
y 1 + y 2 + y 3 +…+ y R = N – R
现在将其简化为标准多项式方程,其解为(N-R)+ R-1 C R-1
该方程的解由N – 1 C R – 1给出

下面是上述方法的实现:

CPP
// C++ implementation of the above approach
#include 
using namespace std;
  
// Function to return the
// value of ncr effectively
int ncr(int n, int r)
{
  
    // Initialize the answer
    int ans = 1;
  
    for (int i = 1; i <= r; i += 1) {
  
        // Divide simultaneously by
        // i to avoid overflow
        ans *= (n - r + i);
        ans /= i;
    }
    return ans;
}
  
// Function to return the number of
// ways to distribute N identical
// objects in R distinct objects
int NoOfDistributions(int N, int R)
{
    return ncr(N - 1, R - 1);
}
  
// Driver code
int main()
{
    int N = 4;
    int R = 3;
  
    cout << NoOfDistributions(N, R);
  
    return 0;
}


Java
// Java implementation of the above approach 
import java.io.*;
  
class GFG 
{
          
    // Function to return the 
    // value of ncr effectively 
    static int ncr(int n, int r) 
    { 
      
        // Initialize the answer 
        int ans = 1; 
      
        for (int i = 1; i <= r; i += 1) 
        { 
      
            // Divide simultaneously by 
            // i to avoid overflow 
            ans *= (n - r + i); 
            ans /= i; 
        } 
        return ans; 
    } 
      
    // Function to return the number of 
    // ways to distribute N identical 
    // objects in R distinct objects 
    static int NoOfDistributions(int N, int R) 
    { 
        return ncr(N - 1, R - 1); 
    } 
      
    // Driver code 
    public static void main (String[] args)
    {
  
        int N = 4; 
        int R = 3; 
      
        System.out.println(NoOfDistributions(N, R)); 
    }
}
  
// This code is contributed by ajit


Python3
# Python3 implementation of the above approach
  
# Function to return the
# value of ncr effectively
def ncr(n, r):
  
  
    # Initialize the answer
    ans = 1
  
    for i in range(1,r+1): 
  
        # Divide simultaneously by
        # i to avoid overflow
        ans *= (n - r + i)
        ans //= i
      
    return ans
  
# Function to return the number of
# ways to distribute N identical
# objects in R distinct objects
def NoOfDistributions(N, R):
  
    return ncr(N - 1, R - 1)
  
# Driver code
  
N = 4
R = 3
  
print(NoOfDistributions(N, R))
  
# This code is contributed by mohit kumar 29


C#
// C# implementation of the above approach 
using System;
  
class GFG
{
      
    // Function to return the 
    // value of ncr effectively 
    static int ncr(int n, int r) 
    { 
      
        // Initialize the answer 
        int ans = 1; 
      
        for (int i = 1; i <= r; i += 1) 
        { 
      
            // Divide simultaneously by 
            // i to avoid overflow 
            ans *= (n - r + i); 
            ans /= i; 
        } 
        return ans; 
    } 
      
    // Function to return the number of 
    // ways to distribute N identical 
    // objects in R distinct objects 
    static int NoOfDistributions(int N, int R) 
    { 
        return ncr(N - 1, R - 1); 
    } 
      
    // Driver code 
    static public void Main ()
    {
        int N = 4; 
        int R = 3; 
      
        Console.WriteLine(NoOfDistributions(N, R)); 
    }
}
  
// This code is contributed by AnkitRai01


输出:
3

时间复杂度: O(R)