Count of non | C ++ Program to count non - palindromic strings of length M using N distinct characters ; Iterative Function to calculate base ^ pow in O ( log y ) ; Function to return the count of non palindromic strings ; Count of strings using n characters with repetitions allowed ; Count of palindromic strings ; Count of non - palindromic strings ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; unsigned long long power ( unsigned long long base , unsigned long long pow ) { unsigned long long res = 1 ; while ( pow > 0 ) { if ( pow & 1 ) res = ( res * base ) ; base = ( base * base ) ; pow >>= 1 ; } return res ; } unsigned long long countNonPalindromicString ( unsigned long long n , unsigned long long m ) { unsigned long long total = power ( n , m ) ; unsigned long long palindrome = power ( n , m / 2 + m % 2 ) ; unsigned long long count = total - palindrome ; return count ; } int main ( ) { int n = 3 , m = 5 ; cout << countNonPalindromicString ( n , m ) ; return 0 ; }