Hat's Fibonacci
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12953 Accepted Submission(s): 4332
Problem Description
A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1.
F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4)
Your task is to take a number as input, and print that Fibonacci number.
Input
Each line will contain an integers. Process to end of file.
Output
For each case, output the result in a line.
Sample Input
100
Sample Output
4203968145672990846840663646
Note: No generated Fibonacci number in excess of 2005 digits will be in the test data, ie. F(20) = 66526 has 5 digits.
Author
戴帽子的
Recommend
Ignatius.L
题目给定Fibonacci 当前数是其前四项的和,规定前4项为1 ,下面提示不多于2005位,我暴力跑了一下发现 7035就大于2005位
就直接开的7035 刚开始用int的vector ,发现会爆内存, 最后想着用char 改一下就过了=-=
ac代码
#include <iostream>
#include <vector>
#include <stdio.h>
#include <queue>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int k=7037;
const int l=7038;
vector<char>v[7039];
void hanshu(int n3,int n4,int n5){
int s1=v[n4].size();
int s2=v[n3].size();
int flag=0,n1,n2;
char cmp;
for(int i=0;i<s1;i++){
n1=v[n4][i]-'0';
if(i<s2)n2=v[n3][i]-'0';
else n2=0;
if(flag){
n1+=flag;
flag=0;
}
int sn=n1+n2;
cmp=(sn%10+'0');
v[n5].push_back(cmp);
if(sn/10){
flag=sn/10;
}
}
if(flag){
cmp=(flag+'0');
v[n5].push_back(cmp);
}
}
void output(int x){
int num=v[x].size();
for(int i=num-1;i>=0;i--){
if(i==num-1&&(v[x][i]-'0')!=0)printf("%c",v[x][i]);
if(i!=num-1)printf("%c",v[x][i]);
}
printf("\n");
}
int main()
{
memset(v,0,sizeof(v));
for(int i=1;i<=4;i++)v[i].push_back('1');
for(int i=5;i<=7036;i++){
v[k].clear();
v[l].clear();
hanshu(i-2,i-1,k);
hanshu(i-4,i-3,l);
hanshu(l,k,i);
}
int n;
while(~scanf("%d",&n)){
output(n);
}
}