Count the number of ways to give ranks for N students such that same ranks are possible | Python3 program to calculate the number of ways to give ranks for N students such that same ranks are possible ; Initializing a table in order to store the bell triangle ; Function to calculate the K - th bell number ; If we have already calculated the bell numbers until the required N ; Base case ; First Bell Number ; If the value of the bell triangle has already been calculated ; Fill the defined dp table ; Function to return the number of ways to give ranks for N students such that same ranks are possible ; Resizing the dp table for the given value of n ; Variables to store the answer and the factorial value ; Iterating till N ; Simultaneously calculate the k ! ; Computing the K - th bell number and multiplying it with K ! ; Driver code
mod = 1e9 + 7 NEW_LINE dp = [ [ - 1 for x in range ( 6 ) ] for y in range ( 6 ) ] NEW_LINE def f ( n , k ) : NEW_LINE INDENT if ( n < k ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( n == k ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( k == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( dp [ n ] [ k ] != - 1 ) : NEW_LINE INDENT return dp [ n ] [ k ] NEW_LINE DEDENT dp [ n ] [ k ] = ( ( ( ( k * f ( n - 1 , k ) ) % mod + ( f ( n - 1 , k - 1 ) ) % mod ) % mod ) ) NEW_LINE return dp [ n ] [ k ] NEW_LINE DEDENT def operation ( n ) : NEW_LINE INDENT global dp NEW_LINE ans = 0 NEW_LINE fac = 1 NEW_LINE for k in range ( 1 , n + 1 ) : NEW_LINE INDENT fac *= k NEW_LINE ans = ( ans + ( fac * f ( n , k ) ) % mod ) % mod NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 NEW_LINE print ( int ( operation ( n ) ) ) NEW_LINE DEDENT