📌  相关文章
📜  每个小组中只有一位领导者的N个人中的小组数

📅  最后修改于: 2021-04-27 05:08:35             🧑  作者: Mango

给定N个人,任务是计算形成大小组的方式的数量? N ,在每个组中,组的第一个元素是组的领导者。
笔记:

  • 具有相同领导者的人的组被视为不同的组。例如:组{1,2,3}和{2,1,3}被视为不同的组,因为它们分别具有不同的领导者1和2。
  • 具有相同领导者且具有相同人员的组被视为同一组。例如:组{1、3、2}和{1、2、3}被视为同一组,因为它们具有相同的领导者和相同的人员。
  • 答案可能非常大,取(1e9 + 7)为

例子:

方法:可以使用二项式系数和模幂的概念来解决此问题。下面是对该问题陈述的观察:

  • N人中选择一位领导者的方式有C(N,1)个
  • 对于每个领导者,我们都可以选择大小为K的一组,其中0≤K≤N-1,以进行可能的分组数量。
  • 因此,总数的方式由N与其余(N – 1)个元素中选择K个元素之和的乘积给出:

通过使用二项式定理,二项式系数的总和可以写为:

因此,选择只有一位领导者的小组的方式是

下面是上述方法的实现:

C++
// C++ program for the above approach
  
#include 
using namespace std;
  
long long mod = 1000000007;
  
// Function to find 2^x using
// modular exponentiation
int exponentMod(int A, int B)
{
    // Base cases
    if (A == 0)
        return 0;
    if (B == 0)
        return 1;
  
    // If B is even
    long long y;
    if (B % 2 == 0) {
        y = exponentMod(A, B / 2);
        y = (y * y) % mod;
    }
  
    // If B is odd
    else {
        y = A % mod;
        y = (y * exponentMod(A, B - 1)
             % mod)
            % mod;
    }
  
    return (int)((y + mod) % mod);
}
  
// Function to count the number of
// ways to form the group having
// one leader
void countWays(int N)
{
  
    // Find 2^(N-1) using modular
    // exponentiation
    long long select = exponentMod(2,
                                   N - 1);
  
    // Count total ways
    long long ways
        = ((N % mod)
           * (select % mod));
  
    ways %= mod;
  
    // Print the total ways
    cout << ways;
}
  
// Driver Code
int main()
{
  
    // Given N number of peoples
    int N = 5;
  
    // Function Call
    countWays(N);
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
  
static long mod = 1000000007;
  
// Function to find 2^x using
// modular exponentiation
static int exponentMod(int A, int B)
{
    // Base cases
    if (A == 0)
        return 0;
    if (B == 0)
        return 1;
  
    // If B is even
    long y;
    if (B % 2 == 0) 
    {
        y = exponentMod(A, B / 2);
        y = (y * y) % mod;
    }
  
    // If B is odd
    else 
    {
        y = A % mod;
        y = (y * exponentMod(A, B - 1) % 
                                  mod) % mod;
    }
  
    return (int)((y + mod) % mod);
}
  
// Function to count the number of
// ways to form the group having
// one leader
static void countWays(int N)
{
  
    // Find 2^(N-1) using modular
    // exponentiation
    long select = exponentMod(2, N - 1);
  
    // Count total ways
    long ways = ((N % mod) * (select % mod));
  
    ways %= mod;
  
    // Print the total ways
    System.out.print(ways);
}
  
// Driver Code
public static void main(String[] args)
{
  
    // Given N number of peoples
    int N = 5;
  
    // Function Call
    countWays(N);
}
}
  
// This code is contributed by sapnasingh4991


Python3
# Python3 program for the above approach
mod = 1000000007
  
# Function to find 2^x using
# modular exponentiation
def exponentMod(A, B):
      
    # Base cases
    if (A == 0):
        return 0;
    if (B == 0):
        return 1;
  
    # If B is even
    y = 0;
      
    if (B % 2 == 0):
        y = exponentMod(A, B // 2);
        y = (y * y) % mod;
  
    # If B is odd
    else:
        y = A % mod;
        y = (y * exponentMod(A, B - 1) %
                                  mod) % mod;
                               
    return ((y + mod) % mod);
  
# Function to count the number of
# ways to form the group having
# one leader
def countWays(N):
      
    # Find 2^(N-1) using modular
    # exponentiation
    select = exponentMod(2, N - 1);
  
    # Count total ways
    ways = ((N % mod) * (select % mod));
  
    ways %= mod;
  
    # Print the total ways
    print(ways)
      
# Driver code        
if __name__=='__main__':
      
    # Given N number of people
    N = 5;
  
    # Function call
    countWays(N);
  
# This code is contributed by rutvik_56


C#
// C# program for the above approach
using System;
class GFG{
   
static long mod = 1000000007;
   
// Function to find 2^x using
// modular exponentiation
static int exponentMod(int A, int B)
{
    // Base cases
    if (A == 0)
        return 0;
    if (B == 0)
        return 1;
   
    // If B is even
    long y;
    if (B % 2 == 0) 
    {
        y = exponentMod(A, B / 2);
        y = (y * y) % mod;
    }
   
    // If B is odd
    else
    {
        y = A % mod;
        y = (y * exponentMod(A, B - 1) % 
                                  mod) % mod;
    }
   
    return (int)((y + mod) % mod);
}
   
// Function to count the number of
// ways to form the group having
// one leader
static void countWays(int N)
{
   
    // Find 2^(N-1) using modular
    // exponentiation
    long select = exponentMod(2, N - 1);
   
    // Count total ways
    long ways = ((N % mod) * (select % mod));
   
    ways %= mod;
   
    // Print the total ways
    Console.Write(ways);
}
   
// Driver Code
public static void Main(String[] args)
{
   
    // Given N number of peoples
    int N = 5;
   
    // Function Call
    countWays(N);
}
}
  
// This code is contributed by sapnasingh4991


输出:
80

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