CPP
cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>
using namespace std;
static vector<int> memo{};
int q(int n){
if(n<=2)
return 1;
else if((unsigned)n>=memo.size())
memo.resize(n+1);
else if(memo[n]!=0)
return memo[n];
memo[n] = (q(n-(q(n-1)))+q(n-(q(n-2))));
return memo[n];
}
int main(){
int n;
cin>>n;
cout<<"q("<<n<<") = "<<(q(n))<<'\n';
//174661 max for me.
}
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run