Count N | Stores the value of overlapping states ; Function to check whether a number have only digits X or Y or not ; Until sum is positive ; If any digit is not X or Y then return 0 ; Return 1 ; Function to find the count of numbers that are formed by digits X and Y and whose sum of digit also have digit X or Y ; Initialize dp array ; Base Case ; Check if sum of digits formed by only X or Y ; Return the already computed ; Place the digit X at the current position ; Place the digit Y at the current position ; Update current state result ; Driver Code ; Function Call
dp = [ [ - 1 for x in range ( 9000 + 5 ) ] for y in range ( 1000 + 5 ) ] NEW_LINE mod = 1000000007 NEW_LINE def check ( sum , x , y ) : NEW_LINE INDENT while ( sum > 0 ) : NEW_LINE INDENT ln = sum % 10 NEW_LINE if ( ln != x and ln != y ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT sum //= 10 NEW_LINE DEDENT return 1 NEW_LINE DEDENT def countNumbers ( n , x , y , sum ) : NEW_LINE INDENT global dp NEW_LINE if ( n == 0 ) : NEW_LINE INDENT return check ( sum , x , y ) NEW_LINE DEDENT if ( dp [ n ] [ sum ] != - 1 ) : NEW_LINE INDENT return dp [ n ] [ sum ] % mod NEW_LINE DEDENT option1 = countNumbers ( n - 1 , x , y , sum + x ) % mod NEW_LINE option2 = countNumbers ( n - 1 , x , y , sum + y ) % mod NEW_LINE dp [ n ] [ sum ] = ( option1 + option2 ) % mod NEW_LINE return dp [ n ] [ sum ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 3 NEW_LINE X = 1 NEW_LINE Y = 5 NEW_LINE print ( countNumbers ( N , X , Y , 0 ) % mod ) NEW_LINE DEDENT