题目描述
The most naive way of computing $n^{15}$ requires fourteen multiplications:
$n \times n \times \dots \times n = n^{15}$
But using a “binary” method you can compute it in six multiplications:
\begin{eqnarray}
n \times n &= n^2\\
n^2 \times n^2 &= n^4\\
n^4 \times n^4 &= n^8\\
n^8 \times n^4 &= n^{12}\\
n^{12} \times n^2 &= n^{14}\\
n^{14} \times n &= n^{15}\\
\end{eqnarray}
However it is yet possible to compute it in only five multiplications:
\begin{eqnarray}
n \times n &= n^2\\
n^2 \times n &= n^3\\
n^3 \times n^3 &= n^6\\
n^6 \times n^6 &= n^{12}\\
n^{12} \times n^3 &= n^{15}\\
\end{eqnarray}
We shall define $m(k)$ to be the minimum number of multiplications to compute $n^k$; for example $m(15) = 5$.
For $1 \leq k \leq 200$, find $\sum m(k)$.
题意
我们知道快速计算一个数的幂次有各种不同的方法,快速幂只是其中一种而且不是最快的,然后问你对于1~200的幂次来说,最少需要几次乘法操作.
分析
有一个wiki讲的就是这个问题,可以看一看….
我们可以发现,对于所有幂次来说,我们按照最后一步操作的数来构成他的前驱,然后我们会发现他是一棵树,然后我们直接构造出这颗树出来,就是最优答案…..
代码
给大佬递上我奇丑无比的代码 (/ω\)
1 |
|