Answers to: BASE - Editorialhttps://discuss.codechef.com/questions/93077/base-editorial<h1>PROBLEM LINK:</h1>
<p><a href="https://www.codechef.com/problems/BASE">Practice</a></p>
<p><a href="https://www.codechef.com/DEC16/problems/BASE">Contest</a></p>
<p><strong>Author:</strong> <a href="https://www.codechef.com/users/code_master01">Kevin Charles Atienza</a></p>
<p><strong>Tester:</strong> <a href="https://www.codechef.com/users/kevinsogo">Kevin Charles Atienza</a></p>
<p><strong>Editorialist:</strong> <a href="https://www.codechef.com/users/wittyceaser">Vaibhav Tulsyan</a></p>
<h1>DIFFICULTY:</h1>
<p>Easy-Medium</p>
<h1>PREREQUISITES:</h1>
<p>None</p>
<h1>PROBLEM:</h1>
<p>Given an integer $N$, find the number of integer bases $B$, such that the base-$B$ representation of $N$
starts with a $1$.</p>
<h1>QUICK EXPLANATION:</h1>
<p>In base $B$, a number with $(K + 1)$ digits is in the range $[B^K, B^{(K + 1)})$.
It starts with the digit $1$ if and only if it is in the range $[B^K, 2.B^K)$.
Hence, for a base $B$ containing $(K + 1)$ digits, we need to find the no. of bases satisfying $floor({N/2}^{1/K}) \lt B \le N^{1/K}$.
The different lengths of bases we need to check are $log_2(N)$, which is at max $40$.</p>
<h1>EXPLANATION:</h1>
<p>For $N = 0$, the answer is $0$, as no base $B$ representation of $0$ can start with $1$.
For $N = 1$, the answer is "INFINITY", as $1$ written in any base $B$ starts with a $1$.</p>
<p><strong>Naive Approach:</strong></p>
<p>We can iterate over all bases in the range $[2, N]$, find the base-$B$ representation
of $N$ and check if it starts with $1$.</p>
<p>Pseudo-code:
<code>
if N == 0: return 0
if N == 1: return "INFINITY"
ans = 0
for base in [2..N]:
num = n
while num &gt; 1:
num /= base
digit %= base
if digit == 1:
ans += 1
return ans
</code></p>
<p>This approach is too slow and would pass only Subtask 1.</p>
<p><strong>Expected Solution:</strong></p>
<p>Let's say that a base in consideration $B$ contains $(K + 1)$ digits.
Such a base can represent numbers in range: $[B^K, B^{(K + 1)})$. We want the first digit
to be equal to $1$.
A number $N$ written in base $B$ starts with $1$ if and only if it is in the range $[B^K, 2.B^K)$.
This implies, $floor({N/2}) \lt B^K \le N$, which is equivalent to $floor({N/2}^{1/K}) \lt B \le N^{1/K}$.
For each value of $K$, we can now find out the bases $B$ that we require - that is, the values of $B$
satisfying this inequality.</p>
<p>Since the maximum value of $N$ in the given problem is $10^{12}$, the highest length of the
base that is possible is at most $40$.</p>
<p>Pseudo-code:
<code>
if N == 0: return 0
if N == 1: return "INFINITY"</code></p><code>
</code><p><code>ans = 0
for length in [2..40]:
val = f(N, length) - f(N / 2, length)
ans += val
return ans
</code></p>
<p><code>
function f(N, length):
x = int(round(pow(N, 1 / length)))
while pow(x, length) &lt; N: x += 1
while pow(x, length) &gt; N: x -= 1
return x
</code></p>
<p>Complexity: $O(T * (40.\alpha))$, where $\alpha$ is the complexity of the $pow(a, b)$ function, where $b$
is a floating point number less than $1$.</p>
<h1>AUTHOR'S AND TESTER'S SOLUTIONS:</h1>
<p>Setter's solution can be found <a href="https://www.codechef.com/DEC16/problems/BASE">here</a>.</p>
<p>Tester's solution can be found <a href="https://www.codechef.com/DEC16/problems/BASE">here</a>.</p>
<p>Editorialist's solution can be found <a href="https://www.codechef.com/DEC16/problems/BASE">here</a>.</p>enFri, 22 Feb 2019 22:25:55 -0000