给定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
Javascript
输出:
80
时间复杂度: O(log N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。