Number of ways to split N as sum of K numbers from the given range | Python3 implementation to count the number of ways to divide N in K groups such that each group has elements in range [ L , R ] ; Function to count the number of ways to divide the number N in K groups such that each group has number of elements in range [ L , R ] ; Base Case ; If N is divides completely into less than k groups ; Put all possible values greater equal to prev ; Function to count the number of ways to divide the number N ; Driver Code
mod = 1000000007 NEW_LINE def calculate ( pos , left , k , L , R ) : NEW_LINE INDENT if ( pos == k ) : NEW_LINE INDENT if ( left == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT if ( left == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT answer = 0 NEW_LINE for i in range ( L , R + 1 ) : NEW_LINE INDENT if ( i > left ) : NEW_LINE INDENT break NEW_LINE DEDENT answer = ( answer + calculate ( pos + 1 , left - i , k , L , R ) ) % mod NEW_LINE DEDENT return answer NEW_LINE DEDENT def countWaystoDivide ( n , k , L , R ) : NEW_LINE INDENT return calculate ( 0 , n , k , L , R ) NEW_LINE DEDENT N = 12 NEW_LINE K = 3 NEW_LINE L = 1 NEW_LINE R = 5 NEW_LINE print ( countWaystoDivide ( N , K , L , R ) ) NEW_LINE