#NEWLINE# @ cacheit #NEWLINE# def _nP( n , k = None , replacement = False ) : #NEWLINE# #INDENT# from sympy.functions.combinatorial.factorials import factorial #NEWLINE# from sympy.core.mul import prod #NEWLINE# if ( k == 0 ) : #NEWLINE# #INDENT# return 1 #NEWLINE# #UNINDENT# if isinstance ( n , SYMPY_INTS ) : #NEWLINE# #INDENT# if ( k is None ) : #NEWLINE# #INDENT# return sum ( ( _nP ( n , i , replacement ) for i in range ( ( n + 1 ) ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if replacement : #NEWLINE# #INDENT# return ( n ** k ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( k > n ) : #NEWLINE# #INDENT# return 0 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( k == n ) : #NEWLINE# #INDENT# return factorial ( k ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( k == 1 ) : #NEWLINE# #INDENT# return n #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return _product ( ( ( n - k ) + 1 ) , n ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( n , _MultisetHistogram ) : #NEWLINE# #INDENT# if ( k is None ) : #NEWLINE# #INDENT# return sum ( ( _nP ( n , i , replacement ) for i in range ( ( n [ _N ] + 1 ) ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if replacement : #NEWLINE# #INDENT# return ( n [ _ITEMS ] ** k ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( k == n [ _N ] ) : #NEWLINE# #INDENT# return ( factorial ( k ) / prod ( [ factorial ( i ) for i in n [ _M ] if ( i > 1 ) ] ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( k > n [ _N ] ) : #NEWLINE# #INDENT# return 0 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( k == 1 ) : #NEWLINE# #INDENT# return n [ _ITEMS ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# tot = 0 #NEWLINE# n = list ( n ) #NEWLINE# for i in range ( len ( n [ _M ] ) ) : #NEWLINE# #INDENT# if ( not n [ i ] ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# n [ _N ] -= 1 #NEWLINE# if ( n [ i ] == 1 ) : #NEWLINE# #INDENT# n [ i ] = 0 #NEWLINE# n [ _ITEMS ] -= 1 #NEWLINE# tot += _nP ( _MultisetHistogram ( n ) , ( k - 1 ) ) #NEWLINE# n [ _ITEMS ] += 1 #NEWLINE# n [ i ] = 1 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# n [ i ] -= 1 #NEWLINE# tot += _nP ( _MultisetHistogram ( n ) , ( k - 1 ) ) #NEWLINE# n [ i ] += 1 #NEWLINE# #UNINDENT# n [ _N ] += 1 #NEWLINE# #UNINDENT# return tot