I know the logic is true for small numbers,but how can i prove the correctness of the algorithm for all n; here is the code:2296715
I know the logic is true for small numbers,but how can i prove the correctness of the algorithm for all n; here is the code:2296715
| № | Пользователь | Рейтинг |
|---|---|---|
| 1 | Benq | 3792 |
| 2 | VivaciousAubergine | 3647 |
| 3 | Kevin114514 | 3603 |
| 4 | jiangly | 3583 |
| 5 | strapple | 3515 |
| 6 | tourist | 3470 |
| 7 | dXqwq | 3436 |
| 8 | Radewoosh | 3415 |
| 9 | Otomachi_Una | 3413 |
| 10 | Um_nik | 3376 |
| Страны | Города | Организации | Всё → |
| № | Пользователь | Вклад |
|---|---|---|
| 1 | Qingyu | 158 |
| 2 | adamant | 152 |
| 3 | Um_nik | 146 |
| 4 | Dominater069 | 144 |
| 5 | errorgorn | 141 |
| 6 | cry | 139 |
| 6 | Proof_by_QED | 139 |
| 8 | YuukiS | 135 |
| 9 | chromate00 | 134 |
| 9 | TheScrasse | 134 |
| Название |
|---|



http://en.wikipedia.org/wiki/Mathematical_induction
I won't provide a proof, but here's how you can look at it. For a given n > 3, the answer f(n) is : f(n) = 3^(n-1) — f(n-1), f(2) = 3. The reason this is true is that you can move arbitrarily the first n-1 moves, each time choosing from 3 possibilities (hence the 3^(n-1)), and then go back to D on last n-th move. However, this isn't possible if after n-1 moves you end up in D, which is f(n-1), hence the subtraction.
So the answer f(n) is : 3^(n-1) — 3^(n-2) + 3^(n-3) — ... until 3^1.
I think the algorithm somehow uses this idea, I'm not sure how though.