2021-06-15 00:49:18 -07:00
|
|
|
|
---
|
|
|
|
|
id: 5900f3e61000cf542c50fef9
|
2022-02-28 13:29:21 +05:30
|
|
|
|
title: 'Problema 122: Esponenziazione efficiente'
|
2021-06-15 00:49:18 -07:00
|
|
|
|
challengeType: 5
|
|
|
|
|
forumTopicId: 301749
|
|
|
|
|
dashedName: problem-122-efficient-exponentiation
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
# --description--
|
|
|
|
|
|
2022-02-28 13:29:21 +05:30
|
|
|
|
Il modo più ingenuo di calcolare $n^{15}$ richiede quattordici moltiplicazioni:
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
2022-02-28 13:29:21 +05:30
|
|
|
|
$$n × n × \ldots × n = n^{15}$$
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
2022-02-28 13:29:21 +05:30
|
|
|
|
Ma usando un metodo "binario" è possibile calcolarlo in sei moltiplicazioni:
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
2022-03-31 22:31:59 +05:30
|
|
|
|
$$$\start{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
|
|
|
|
|
2022-02-28 13:29:21 +05:30
|
|
|
|
Tuttavia è ancora possibile calcolarlo in sole cinque moltiplicazioni:
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
2022-03-31 22:31: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
|
|
|
|
|
2022-02-28 13:29:21 +05:30
|
|
|
|
Definiremo $m(k)$ in modo che sia il numero minimo di moltiplicazioni per calcolare $n^k$; per esempio $m(15) = 5$.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
2022-02-28 13:29:21 +05:30
|
|
|
|
Per $1 ≤ k ≤ 200$, trova $\sum{m(k)}$.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
|
2022-02-28 13:29:21 +05:30
|
|
|
|
`efficientExponentation()` dovrebbe restituire `1582`.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
|
|
```js
|
2022-02-28 13:29:21 +05:30
|
|
|
|
assert.strictEqual(efficientExponentation(), 1582);
|
2021-06-15 00:49:18 -07:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```js
|
2022-02-28 13:29:21 +05:30
|
|
|
|
function efficientExponentation() {
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-28 13:29:21 +05:30
|
|
|
|
efficientExponentation();
|
2021-06-15 00:49:18 -07:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
# --solutions--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// solution required
|
|
|
|
|
```
|