Climbing the stairs, the hare jumps either to the next step, or one, or two. In how many ways can he climb the step with the number K?
Everything seems to be clear ...
#include <iostream> using namespace std; int vr(int n) { if (n<3) return n; else if (n==3) return 4; else return vr(n-1)+vr(n-2)+vr(n-3); } int main() { int n; cin >> n; cout << vr(n); return 0; } But when the program did not pass the time test on 32 steps, it finally reached me that such an algorithm generates an unmeasured number of repeated calculations, thereby wasting precious resources. I ask wise heads to suggest a true idea, how to reduce the amount of calculations beyond the clouds to the optimal number.
nand cycle through it. A better option: since we use only the last 3 elements, it is enough to create an array of all three elements and walk through it several times. - HolyBlackCat