2021-06-15 00:49:18 -07:00
|
|
|
|
---
|
|
|
|
|
id: 5900f3e61000cf542c50fef9
|
2021-10-18 08:17:43 -07:00
|
|
|
|
title: 'Problema 122: Exponenciação eficiente'
|
2021-06-15 00:49:18 -07:00
|
|
|
|
challengeType: 5
|
|
|
|
|
forumTopicId: 301749
|
|
|
|
|
dashedName: problem-122-efficient-exponentiation
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
# --description--
|
|
|
|
|
|
2021-10-18 08:17:43 -07:00
|
|
|
|
A maneira mais ingênua de calcular $n^{15}$ requer 14 multiplicações:
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
2021-10-18 08:17:43 -07:00
|
|
|
|
$$n × n × \ldots × n = n^{15}$$
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
2021-10-18 08:17:43 -07:00
|
|
|
|
Mas usando um método "binário" você pode calculá-lo em seis multiplicações:
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
2022-04-05 23:36:59 +05:30
|
|
|
|
$$\begin{align} & n × n = n^2\\\\
|
|
|
|
|
& n^2 × n^2 = n^4\\\\ & n^4 × n^4 = n^8\\\\
|
|
|
|
|
& n^8 × n^4 = n^{12}\\\\ & n^{12} × n^2 = n^{14}\\\\
|
|
|
|
|
& n^{14} × n = n^{15} \end{align}$$
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
2021-10-18 08:17:43 -07:00
|
|
|
|
No entanto, ainda é possível calculá-lo em apenas cinco multiplicações:
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
2022-04-05 23:36:59 +05:30
|
|
|
|
$$\begin{align} & n × n = n^2\\\\
|
|
|
|
|
& n^2 × n = n^3\\\\ & n^3 × n^3 = n^6\\\\
|
|
|
|
|
& n^6 × n^6 = n^{12}\\\\ & n^{12} × n^3 = n^{15} \end{align}$$
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
2021-10-18 08:17:43 -07:00
|
|
|
|
Definiremos $m(k)$ como o número mínimo de multiplicações para calcular $n^k$; por exemplo, $m(15) = 5$.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
2021-10-18 08:17:43 -07:00
|
|
|
|
Para $1 ≤ k ≤ 200$, encontre $\sum{m(k)}$.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
|
2021-10-18 08:17:43 -07:00
|
|
|
|
`efficientExponentation()` deve retornar `1582`.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
|
|
```js
|
2021-10-18 08:17:43 -07:00
|
|
|
|
assert.strictEqual(efficientExponentation(), 1582);
|
2021-06-15 00:49:18 -07:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```js
|
2021-10-18 08:17:43 -07:00
|
|
|
|
function efficientExponentation() {
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-18 08:17:43 -07:00
|
|
|
|
efficientExponentation();
|
2021-06-15 00:49:18 -07:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
# --solutions--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// solution required
|
|
|
|
|
```
|