Skip to content

Instantly share code, notes, and snippets.

@KT-Yeh
Created January 28, 2014 14:45
Show Gist options
  • Save KT-Yeh/8668896 to your computer and use it in GitHub Desktop.
Save KT-Yeh/8668896 to your computer and use it in GitHub Desktop.
#include <cstdio>
using namespace std;
int Fib[5001][500]={0};
int main()
{
Fib[1][0]=1;
for (int i=2; i<=5000; i++){
for (int j=0; j<500; j++){
Fib[i][j] += Fib[i-1][j]+Fib[i-2][j];
if (Fib[i][j]>=1000){
Fib[i][j] -= 1000;
Fib[i][j+1]++;
}
}
}
int n;
while (scanf("%d",&n)!=EOF){
printf("The Fibonacci number for %d is ",n);
if (!n) printf("0\n");
else {
int i=500;
while (Fib[n][--i]==0);
printf("%d",Fib[n][i--]);
for (; i>=0; i--) printf("%.3d",Fib[n][i]);
printf("\n");
}
}
return 0;
}
@sourav7
Copy link

sourav7 commented Oct 9, 2015

what is the basic of following this technique ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment