LeetCode 483
LeetCode 483 Smallest Good Base
题目大意
解法:
实在想不出什么办法,就二分吧。。。
代码:https://www.cnblogs.com/grandyang/p/6620351.html
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); if(sum<num) left = mid+1; else right = mid; } } return to_string(num-1); } };