<p>Given an integer <code>n</code> represented as a string, return <em>the smallest <strong>good base</strong> of</em> <code>n</code>.</p>

<p>We call <code>k &gt;= 2</code> a <strong>good base</strong> of <code>n</code>, if all digits of <code>n</code> base <code>k</code> are <code>1</code>&#39;s.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = &quot;13&quot;
<strong>Output:</strong> &quot;3&quot;
<strong>Explanation:</strong> 13 base 3 is 111.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = &quot;4681&quot;
<strong>Output:</strong> &quot;8&quot;
<strong>Explanation:</strong> 4681 base 8 is 11111.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> n = &quot;1000000000000000000&quot;
<strong>Output:</strong> &quot;999999999999999999&quot;
<strong>Explanation:</strong> 1000000000000000000 base 999999999999999999 is 11.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n</code> is an integer in the range <code>[3, 10<sup>18</sup>]</code>.</li>
	<li><code>n</code> does not contain any leading zeros.</li>
</ul>

class Solution {
public:
    string smallestGoodBase(string s) {
        long long n=stoll(s);
        cout << n << endl;
        for(int i=59;i>=3;i--){
            long long lo=2,hi=1e9,f=-1;
            while(lo<hi){
                long long mid=lo+(hi-lo)/2;
                long long c=0,p=1;
                for(int j=1;j<=i;j++){
                    c+=p;
                    if(c>=n)break;
                    if(j<i && (n-c)/p<mid){
                        c=n+1;
                        break;
                    }
                    if(j<i)p*=mid;
                }
                // if(i==8)cout << mid << " " << c << endl;
                if(c==n)f=mid;
                if(c>=n)hi=mid;
                else lo=mid+1;
            }
            // cout << i << " " << lo << endl;
            if(f!=-1)return to_string(f);
        }
        return to_string(n-1);
    }
};
7500