C - Blindfolded Cubing Editorial
by
yingjingxu
Solution with 292 Operations
The construction takes 292 operations, or \(\mathcal{O}(\log^2 N)\) in general.
How can we get a cube from fourth powers and squares? The difference of squares identity gives \((N^2)^2-(N^2-N)^2=2N^3-N^2=(2N-1)N^2\), which is close to what we need.
Taking \(N^2\) as our unit, this amounts to constructing \(N\) from \((2N-1,1,2N-1,0)\).
Since \(N = \lceil \dfrac{2N-1}{2} \rceil\), let’s try moving toward \(\dfrac{2N-1}{2}\) a step at a time.
Let \(T=2N-1\). Given a number \(X\) and a fixed step size \(K\), how do we move \(X\) closer to \(N\)?
Set \(X \gets X-K\), then \(X \gets \max\{X,T-X\}\). We can compute the maximum using \(\max\{X,T-X\} = T - (X-(X-(T-X))_+)\). If the initial error \(X-N\) lies in \([+0,+2K)\), these two steps bring it into \([+0,+K)\). Taking \(K=2^{10},2^9,\cdots,1\) in that order gives \(X=N\).
This gives a valid construction. The bottleneck is the subtraction \(X \gets X-K\).
Now return to the original scale, where one unit represents \(N^2\).
To carry out \(X \gets X-K\) in fewer operations when \(K=2^i\), build \(K\) by repeated doubling. The identity \(\min(X,2K)=X-(X-K-K)_+\) lets us double \(K\) in three operations, so \(X \gets X-K\) takes just \(3i+1\) operations in total.
Keep \(N\) in one register throughout. Each time we need the subtraction, compute \(N^2\) and proceed as above. Each time we take the maximum, recompute \(T\). This gives \(N^3\) using four registers. With \(M=\log_2 N\), the exact operation count is \(\dfrac{3M^2+19M+12}{2}\).
