Largest value of x such that axx is N | C ++ implementation of the above approach ; Function to find log_b ( a ) ; Set two pointer for binary search ; Calculating number of digits of a * mid ^ mid in base b ; If number of digits > n we can simply ignore it and decrease our pointer ; if number of digits <= n , we can go higher to reach value exactly equal to n ; return the largest value of x ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double log ( int a , int b ) { return log10 ( a ) / log10 ( b ) ; } int get ( int a , int b , int n ) { int lo = 0 , hi = 1e6 ; int ans = 0 ; while ( lo <= hi ) { int mid = ( lo + hi ) / 2 ; int dig = ceil ( ( mid * log ( mid , b ) + log ( a , b ) ) ) ; if ( dig > n ) { hi = mid - 1 ; } else { ans = mid ; lo = mid + 1 ; } } return ans ; } int main ( ) { int a = 2 , b = 2 , n = 6 ; cout << get ( a , b , n ) << " STRNEWLINE " ; return 0 ; }