给定N个人,任务是计算形成大小组的方式的数量? N ,在每个组中,组的第一个元素是组的领导者。
笔记:
- 具有相同领导者的人的组被视为不同的组。例如:组{1,2,3}和{2,1,3}被视为不同的组,因为它们分别具有不同的领导者1和2。
- 具有相同领导者且具有相同人员的组被视为同一组。例如:组{1、3、2}和{1、2、3}被视为同一组,因为它们具有相同的领导者和相同的人员。
- 答案可能非常大,取(1e9 + 7)为模。
例子:
Input: N = 3
Output: 12
Explanation:
Total Groups with leaders are:
Groups with Leader 1:
1. {1}
2. {1, 2}
3. {1, 3}
4. {1, 2, 3}
Groups with Leader 2:
5. {2}
6. {2, 1}
7. {2, 3}
8. {2, 1, 3}
Groups with Leader 3:
9. {3}
10. {3, 1}
11. {3, 2}
12. {3, 1, 2}
Input: N = 5
Output: 80
方法:可以使用二项式系数和模幂的概念来解决此问题。下面是对该问题陈述的观察:
- 在N人中选择一位领导者的方式有C(N,1)个。
- 对于每个领导者,我们都可以选择大小为K的一组,其中0≤K≤N-1,以进行可能的分组数量。
- 因此,总数的方式由N与其余(N – 1)个元素中选择K个元素之和的乘积给出:
Total Ways =
通过使用二项式定理,二项式系数的总和可以写为:
因此,选择只有一位领导者的小组的方式是
下面是上述方法的实现:
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)