Fibonacci
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4844 Accepted Submission(s): 2245
Problem Description
2007年到来了。经过2006年一年的修炼,数学神童zouyu终于把0到100000000的Fibonacci数列
(f[0]=0,f[1]=1;f[i] = f[i-1]+f[i-2](i>=2))的值全部给背了下来。
接下来,CodeStar决定要考考他,于是每问他一个数字,他就要把答案说出来,不过有的数字太长了。所以规定超过4位的只要说出前4位就可以了,可是CodeStar自己又记不住。于是他决定编写一个程序来测验zouyu说的是否正确。
(f[0]=0,f[1]=1;f[i] = f[i-1]+f[i-2](i>=2))的值全部给背了下来。
接下来,CodeStar决定要考考他,于是每问他一个数字,他就要把答案说出来,不过有的数字太长了。所以规定超过4位的只要说出前4位就可以了,可是CodeStar自己又记不住。于是他决定编写一个程序来测验zouyu说的是否正确。
Input
输入若干数字n(0 <= n <= 100000000),每个数字一行。读到文件尾。
Output
输出f[n]的前4个数字(若不足4个数字,就全部输出)。
Sample Input
1
2
3
4
5
35
36
37
38
39
40
Sample Output
1
1
2
3
5
9227
1493
2415
3908
6324
1023
Author
daringQQ
Source
题意:求斐波那契数列的前n位数
题解:斐波那契的通项公式为

/******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
#include<bits/stdc++.h>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
#define A first
#define B second
const int mod=;
const int MOD1=;
const int MOD2=;
const double EPS=0.00000001;
typedef __int64 ll;
const ll MOD=;
const int INF=;
const ll MAX=1ll<<;
const double eps=1e-;
const double inf=~0u>>;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
int a[];
double change(double a)
{
while(a-10.0>=eps)
{
a/=;
}
return a;
}
double pow(double a,int b)
{
double ans=1.0;
while(b)
{
if(b&){
ans*=a;
ans=change(ans);
}
a*=a;
a=change(a);
b>>=;
}
return ans;
}
int main()
{
int n;
a[]=;
a[]=;
for(int i=;i<=;i++)
a[i]=a[i-]+a[i-];
double base1=(1.0+sqrt(5.0))/2.0,base2=(1.0-sqrt(5.0))/2.0;
while(scanf("%d",&n)!=EOF)
{
if(n<=)
printf("%d\n",a[n]);
else
{
double ans=(1.0)/sqrt(5.0)*pow(base1,n);
if(ans-1.0>=eps)
ans*=;
else
ans*=;
printf("%d\n",(int)ans);
}
}
return ;
}