大菲波数
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17183 Accepted Submission(s): 5708
Problem Description
Fibonacci数列,定义如下:
f(1)=f(2)=1
f(n)=f(n-1)+f(n-2) n>=3。
计算第n项Fibonacci数值。
f(1)=f(2)=1
f(n)=f(n-1)+f(n-2) n>=3。
计算第n项Fibonacci数值。
Input
输入第一行为一个整数N,接下来N行为整数Pi(1<=Pi<=1000)。
Output
输出为N行,每行为对应的f(Pi)。
Sample Input
5 1 2 3 4 5
Sample Output
1 1 2 3 5
Source
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <iostream>
#define MAX_N 1005
#define MAX(a, b) (a > b)? a: b
#define MIN(a, b) (a < b)? a: b
using namespace std;
int fib[MAX_N][MAX_N];
void init() {
fib[1][0] = 1, fib [2][0] = 1;
for (int i = 3; i < 1001; i++) {
int p = 0, q = 0;
for (int j = 0; j < 550; j++) {
p = fib[i - 1][j] + fib[i - 2][j] + q;
fib[i][j] = p%10;
q = p/10;
}
}
}
int main() {
int t, n, i;
init();
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (i = 550; i >= 0; i--) {
if (fib[n][i] != 0) break;
}
for (; i >= 0; i--) {
printf("%d", fib[n][i]);
}
printf("\n");
}
return 0;
}