给定一个代表学生人数的数字N ,考虑到两个或更多学生可以具有相同的排名,任务是根据他们的 CGPA/分数计算所有可能的排名方式。由于答案可能很大,因此对 10 9 + 7 进行模运算。
例子:
Input: N = 1
Output: 1
Explanation:
There is only one way to rank a student, irrespective of his marks.
Input: N = 2
Output: 2
Explanation:
In the following two ways the ranks can be distributed among two students:
The high scoring student can be awarded the first rank and the low scoring student the second rank or both of them can be awarded the same rank if they have equal scores.
方法:这个问题的想法是使用贝尔数。
- 钟号是计算集合的可能分区的数字。因此,第 N 个铃数是一组大小为 N 的可以划分成的非空子集的数量。
- 例如,让我们考虑 N = 3 的集合 {1, 2, 3}。对应于 N = 3 的钟号是 5。这意味着给定的集合可以划分为以下 5 个非空子集:
{{1}, {2}, {3}}
{{1, 2}, {3}}
{{1, 3}, {2}}
{{2, 3}, {1}}
{{1, 2, 3}}
- 显然,上面的钟形数字表示所有可能的等级。但是,他们不计算子集的排列。
- 因此,通过将每个子集乘以 K!,其中 K 表示各个子集的大小,我们得到所有可能的排列。
- 由于相同的子问题可以重复,我们可以将每个子问题的值存储在数据结构中以优化复杂度。
下面是上述方法的实现:
C++
// C++ program to calculate the number
// of ways to give ranks for N
// students such that same ranks
// are possible
#include
using namespace std;
const int mod = 1e9 + 7;
// Initializing a table in order to
// store the bell triangle
vector > dp;
// Function to calculate the K-th
// bell number
int f(int n, int k)
{
// If we have already calculated
// the bell numbers until the
// required N
if (n < k)
return 0;
// Base case
if (n == k)
return 1;
// First Bell Number
if (k == 1)
return 1;
// If the value of the bell
// triangle has already been
// calculated
if (dp[n][k] != -1)
return dp[n][k];
// Fill the defined dp table
return dp[n][k] = ((k * f(n - 1, k)) % mod
+ (f(n - 1, k - 1)) % mod)
% mod;
}
// Function to return the number
// of ways to give ranks for N
// students such that same ranks
// are possible
long operation(int n)
{
// Resizing the dp table for the
// given value of n
dp.resize(n + 1, vector(n + 1, -1));
// Variables to store the answer
// and the factorial value
long ans = 0, fac = 1;
// Iterating till N
for (int k = 1; k <= n; k++) {
// Simultaneously calculate the k!
fac *= k;
// Computing the K-th bell number
// and multiplying it with K!
ans = (ans + (fac * f(n, k)) % mod)
% mod;
}
return ans;
}
// Driver code
int main()
{
int n = 5;
cout << operation(n) << endl;
return 0;
}
Java
// Java program to calculate the number
// of ways to give ranks for N
// students such that same ranks
// are possible
import java.util.*;
class GFG{
static int mod = (int)(1e9 + 7);
// Initializing a table in order to
// store the bell triangle
static int [][]dp;
// Function to calculate the K-th
// bell number
static int f(int n, int k)
{
// If we have already calculated
// the bell numbers until the
// required N
if (n < k)
return 0;
// Base case
if (n == k)
return 1;
// First Bell Number
if (k == 1)
return 1;
// If the value of the bell
// triangle has already been
// calculated
if (dp[n][k] != -1)
return dp[n][k];
// Fill the defined dp table
return dp[n][k] = ((k * f(n - 1, k)) % mod +
(f(n - 1, k - 1)) % mod) % mod;
}
// Function to return the number
// of ways to give ranks for N
// students such that same ranks
// are possible
static long operation(int n)
{
// Resizing the dp table for the
// given value of n
dp = new int[n + 1][n + 1];
for(int i = 0; i < n + 1; i++)
{
for(int j = 0; j < n + 1; j++)
{
dp[i][j] = -1;
}
}
// Variables to store the answer
// and the factorial value
long ans = 0, fac = 1;
// Iterating till N
for(int k = 1; k <= n; k++)
{
// Simultaneously calculate the k!
fac *= k;
// Computing the K-th bell number
// and multiplying it with K!
ans = (ans + (fac * f(n, k)) % mod) % mod;
}
return ans;
}
// Driver code
public static void main(String[] args)
{
int n = 5;
System.out.print(operation(n) + "\n");
}
}
// This code is contributed by amal kumar choubey
Python3
# Python3 program to calculate the number
# of ways to give ranks for N
# students such that same ranks
# are possible
mod = 1e9 + 7
# Initializing a table in order to
# store the bell triangle
dp = [[-1 for x in range(6)]
for y in range(6)]
# Function to calculate the K-th
# bell number
def f(n, k):
# If we have already calculated
# the bell numbers until the
# required N
if (n < k):
return 0
# Base case
if (n == k):
return 1
# First Bell Number
if (k == 1):
return 1
# If the value of the bell
# triangle has already been
# calculated
if (dp[n][k] != -1):
return dp[n][k]
# Fill the defined dp table
dp[n][k] = ((((k * f(n - 1, k)) % mod +
(f(n - 1, k - 1)) % mod) % mod))
return dp[n][k]
# Function to return the number
# of ways to give ranks for N
# students such that same ranks
# are possible
def operation(n):
# Resizing the dp table for the
# given value of n
global dp
# Variables to store the answer
# and the factorial value
ans = 0
fac = 1
# Iterating till N
for k in range(1, n + 1):
# Simultaneously calculate the k!
fac *= k
# Computing the K-th bell number
# and multiplying it with K!
ans = (ans + (fac * f(n, k)) % mod) % mod
return ans
# Driver code
if __name__ == "__main__":
n = 5
print(int(operation(n)))
# This code is contributed by ukasp
C#
// C# program to calculate the number
// of ways to give ranks for N
// students such that same ranks
// are possible
using System;
class GFG{
static int mod = (int)(1e9 + 7);
// Initializing a table in order to
// store the bell triangle
static int [,]dp;
// Function to calculate the K-th
// bell number
static int f(int n, int k)
{
// If we have already calculated
// the bell numbers until the
// required N
if (n < k)
return 0;
// Base case
if (n == k)
return 1;
// First Bell Number
if (k == 1)
return 1;
// If the value of the bell
// triangle has already been
// calculated
if (dp[n, k] != -1)
return dp[n, k];
// Fill the defined dp table
return dp[n, k] = ((k * f(n - 1, k)) % mod +
(f(n - 1, k - 1)) % mod) % mod;
}
// Function to return the number
// of ways to give ranks for N
// students such that same ranks
// are possible
static long operation(int n)
{
// Resizing the dp table for the
// given value of n
dp = new int[n + 1, n + 1];
for(int i = 0; i < n + 1; i++)
{
for(int j = 0; j < n + 1; j++)
{
dp[i, j] = -1;
}
}
// Variables to store the answer
// and the factorial value
long ans = 0, fac = 1;
// Iterating till N
for(int k = 1; k <= n; k++)
{
// Simultaneously calculate the k!
fac *= k;
// Computing the K-th bell number
// and multiplying it with K!
ans = (ans + (fac * f(n, k)) % mod) % mod;
}
return ans;
}
// Driver code
public static void Main(String[] args)
{
int n = 5;
Console.Write(operation(n) + "\n");
}
}
// This code is contributed by amal kumar choubey
Javascript
输出:
541
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。