Count alphanumeric palindromes of length N | Function to calculate ( x ^ y ) mod p ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now ; Return the final result ; Given N ; Base Case ; Check whether n is even or odd ; Function Call ; Function Call
def power ( x , y , p ) : NEW_LINE INDENT res = 1 NEW_LINE x = x % p NEW_LINE if ( x == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT while ( y > 0 ) : NEW_LINE INDENT if ( ( y & 1 ) == 1 ) : NEW_LINE INDENT res = ( res * x ) % p NEW_LINE DEDENT y = y >> 1 NEW_LINE x = ( x * x ) % p NEW_LINE DEDENT return res NEW_LINE DEDENT N = 3 NEW_LINE if ( ( N == 1 ) or ( N == 2 ) ) : NEW_LINE INDENT print ( 62 ) NEW_LINE DEDENT else : NEW_LINE INDENT m = ( 10 ** 9 ) + 7 NEW_LINE if ( N % 2 == 0 ) : NEW_LINE INDENT k = N // 2 NEW_LINE flag = True NEW_LINE DEDENT else : NEW_LINE INDENT k = ( N - 1 ) // 2 NEW_LINE flag = False NEW_LINE DEDENT if ( flag ) : NEW_LINE INDENT a = power ( 62 , k , m ) NEW_LINE print ( a ) NEW_LINE DEDENT else : NEW_LINE INDENT a = power ( 62 , ( k + 1 ) , m ) NEW_LINE print ( a ) NEW_LINE DEDENT DEDENT