Advertisement
nikunjsoni

483-1

May 20th, 2021
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     typedef unsigned long long ull;
  4.     string smallestGoodBase(string n) {
  5.         ull N = stoll(n), maxM = log2(N);
  6.         for(int m=maxM; m>=2; m--){
  7.             ull k = ull(pow(N, 1.0/m));
  8.             ull prod = 1, sum = 1;
  9.             for(int j=1; j<=m; j++){
  10.                 prod *= k;
  11.                 sum += prod;
  12.             }
  13.             if(sum == N)
  14.                 return to_string(k);
  15.         }
  16.         return to_string(N-1);
  17.     }
  18. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement