Skip to content

Instantly share code, notes, and snippets.

@LucaDantas
Created June 26, 2021 01:26
Show Gist options
  • Save LucaDantas/6b927b5e17b91b7c9475111ae7f5b62d to your computer and use it in GitHub Desktop.
Save LucaDantas/6b927b5e17b91b7c9475111ae7f5b62d to your computer and use it in GitHub Desktop.
DP Fibonacci atualizado
#include <cstdio>
const int maxn = 100010;
int n, dp[maxn];
bool mark[maxn]; // inicialmente o vetor é preenchido com 0
int fib(int x) {
// caso base
if(x <= 1) return 1;
// Se já visitamos esse estado retornamos o valor calculado
if(mark[x] == 1) return dp[x];
// marco o estado como visitado
mark[x] = 1;
// calculamos esse estado
dp[x] = fib(x-1) + fib(x-2);
return dp[x];
}
int main() {
scanf("%d", &n);
printf("%d\n", fib(n));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment