Count numbers from given range having odd digits at odd places and even digits at even places | Function to calculate 5 ^ p ; Stores the result ; Multiply 5 p times ; Return the result ; Function to count anumbers upto N having odd digits at odd places and even digits at even places ; Stores the count ; Stores the digits of N ; Insert the digits of N ; Reverse the vector to arrange the digits from first to last ; Stores count of digits of n ; Stores the count of numbers with i digits ; If the last digit is reached , subtract numbers eceeding range ; Iterate over athe places ; Stores the digit in the pth place ; Stores the count of numbers having a digit greater than x in the p - th position ; Calculate the count of numbers exceeding the range if p is even ; Calculate the count of numbers exceeding the range if p is odd ; Subtract the count of numbers exceeding the range from total count ; If the parity of p and the parity of x are not same ; Add count of numbers having i digits and satisfies the given conditions ; Return the total count of numbers tin ; Function to calculate the count of numbers from given range having odd digits places and even digits at even places ; Count of numbers in range [ L , R ] = Count of numbers tiR - ; Count of numbers ti ( L - 1 ) ; Driver Code
def getPower ( p ) : NEW_LINE INDENT res = 1 NEW_LINE while ( p ) : NEW_LINE INDENT res *= 5 NEW_LINE p -= 1 NEW_LINE DEDENT return res NEW_LINE DEDENT def countNumbersUtil ( N ) : NEW_LINE INDENT count = 0 NEW_LINE digits = [ ] NEW_LINE while ( N ) : NEW_LINE INDENT digits . append ( N % 10 ) NEW_LINE N //= 10 NEW_LINE DEDENT digits . reverse ( ) NEW_LINE D = len ( digits ) NEW_LINE for i in range ( 1 , D + 1 , 1 ) : NEW_LINE INDENT res = getPower ( i ) NEW_LINE if ( i == D ) : NEW_LINE INDENT for p in range ( 1 , D + 1 , 1 ) : NEW_LINE INDENT x = digits [ p - 1 ] NEW_LINE tmp = 0 NEW_LINE if ( p % 2 == 0 ) : NEW_LINE INDENT tmp = ( ( 5 - ( x // 2 + 1 ) ) * getPower ( D - p ) ) NEW_LINE DEDENT else : NEW_LINE INDENT tmp = ( ( 5 - ( x + 1 ) // 2 ) * getPower ( D - p ) ) NEW_LINE DEDENT res -= tmp NEW_LINE if ( p % 2 != x % 2 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT count += res NEW_LINE DEDENT return count NEW_LINE DEDENT def countNumbers ( L , R ) : NEW_LINE INDENT print ( countNumbersUtil ( R ) - countNumbersUtil ( L - 1 ) ) NEW_LINE DEDENT L = 128 NEW_LINE R = 162 NEW_LINE countNumbers ( L , R ) NEW_LINE