// #include<bits/stdc++.h>
#include<iostream>
// #define f(a,b) for(int i=a;i<b;i++)
// #define dbg(x); cout<<"\n"<<#x<<": "<<x<<"\n";
#define ret return
using namespace std;
const int MAX=41;
int f[MAX]={0};
int fibo(int k){
if(k==0) ret 0;
if(k==1||k==2) ret (f[k]=1);
if(f[k]) ret f[k];
int m=(k&1)?(k+1)/2:k/2;
f[k]=(k&1)?(fibo(m)*fibo(m)+fibo(m-1)*fibo(m-1)):(2*fibo(m-1)+fibo(k))*fibo(m);
ret f[k];
}
int sum(int k){
ret fibo(k+2)-1;
}
int main(){
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
int k;
cin>>k;
cout<<sum(k)<<"\n";
ret 0;
}
Don’t use a table, and no arrays. Don’t even need sum. Find the n+2th term and subtract 1
Use fib1=1
Fib2=2
For i 3 to n+2
Fib2=fib1+fib2
Fib1=fib2-fib1