题目描述:
123. The sum
time limit per test: 0.5 sec.
memory limit per test: 4096 KB
The Fibonacci sequence of numbers is known: F1 = 1; F2 = 1; Fn+1 = Fn + Fn-1, for n>1. You have to find S - the sum of the first K Fibonacci numbers.
Input
First line contains natural number K (0<K<41).
Output
First line should contain number S.
Sample Input
5
Sample Output
12
告诉你K,求前K个Fibonacci数的和。预处理一下或直接暴搞就可以,属于水题。
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<algorithm>
#include<vector>
#include<cstdlib>
#define inf 0xfffffff
#define CLR(a,b) memset((a),(b),sizeof((a)))
using namespace std;
int const nMax = 50;
typedef long long LL;
typedef pair<LL,LL> pij;
LL Fi[nMax];
LL sum[nMax];
int main(){
Fi[0]=0,Fi[1]=1;
for(int i=2;i<nMax;i++)Fi[i]=Fi[i-1]+Fi[i-2];
sum[0]=0;
for(int i=1;i<nMax;i++){
sum[i]=sum[i-1]+Fi[i];
}
int n;
while(cin>>n){
cout<<sum[n]<<endl;
}
return 0;
}