483 Smallest Good Base
给定一个范围在[3, 10^18]中的一个数, 找到一个进制base, 使得该数在base进制下表示的数的每一位都是1。
详见:https://leetcode.com/problems/smallest-good-base/description/
C++:
class Solution { public: string smallestGoodBase(string n) { long long num = stol(n); for (int i = log(num + 1) / log(2); i >= 2; --i) { long long left = 2, right = pow(num, 1.0 / (i - 1)) + 1; while (left < right) { long long mid = left + (right - left) / 2, sum = 0; for (int j = 0; j < i; ++j) { sum = sum * mid + 1; } if (sum == num) { return to_string(mid); } else if (sum < num) { left = mid + 1; } else { right = mid; } } } return to_string(num - 1); } };
参考:http://www.cnblogs.com/grandyang/p/6620351.html