Count alphanumeric palindromes of length N | C ++ program for the above approach ; 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 ; Driver Code ; Given N ; Base Case ; Check whether n is even or odd ; Function Call ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } int main ( ) { int N = 3 ; int flag , k , m ; if ( ( N == 1 ) || ( N == 2 ) ) cout << 62 ; else m = 1000000000 + 7 ; if ( N % 2 == 0 ) { k = N / 2 ; flag = true ; } else { k = ( N - 1 ) / 2 ; flag = false ; } if ( flag != 0 ) { int a = power ( 62 , k , m ) ; cout << a ; } else { int a = power ( 62 , ( k + 1 ) , m ) ; cout << a ; } }