Count of N | Python3 program for the above approach ; Function to calculate count of ' N ' digit numbers such that bitwise AND of adjacent digits is 0. ; If digit = n + 1 , a valid n - digit number has been formed ; If the state has already been computed ; If current position is 1 , then any digit from [ 1 - 9 ] can be placed . If n = 1 , 0 can be also placed . ; For remaining positions , any digit from [ 0 - 9 ] can be placed after checking the conditions . ; Check if bitwise AND of current digit and previous digit is 0. ; Return answer ; Driver code ; Given Input ; Function call
dp = [ [ - 1 for i in range ( 10 ) ] for j in range ( 100 ) ] NEW_LINE val = 0 NEW_LINE def countOfNumbers ( digit , prev , n ) : NEW_LINE INDENT global val NEW_LINE global dp NEW_LINE if ( digit == n + 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT val = dp [ digit ] [ prev ] NEW_LINE if ( val != - 1 ) : NEW_LINE INDENT return val NEW_LINE DEDENT val = 0 NEW_LINE if ( digit == 1 ) : NEW_LINE INDENT i = 0 if n == 1 else 1 NEW_LINE while ( i <= 9 ) : NEW_LINE INDENT val += countOfNumbers ( digit + 1 , i , n ) NEW_LINE i += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT for i in range ( 10 ) : NEW_LINE INDENT if ( ( i & prev ) == 0 ) : NEW_LINE INDENT val += countOfNumbers ( digit + 1 , i , n ) NEW_LINE DEDENT DEDENT DEDENT return val NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE print ( countOfNumbers ( 1 , 0 , N ) ) NEW_LINE DEDENT