zoj 1828 Fibonacci Numbers

ZOJ Problem Set - 1828

Fibonacci Numbers


Time Limit: 2 Seconds      Memory Limit: 65536 KB


A Fibonacci sequence is calculated by adding the previous two members of the sequence, with the first two members being both 1.

f(1) = 1, f(2) = 1, f(n > 2) = f(n - 1) + f(n - 2)

Your task is to take a number as input, and print that Fibonacci number.


Sample Input

100

Sample Output

354224848179261915075

Note:

No generated Fibonacci number in excess of 1000 digits will be in the test data, i.e. f(20) = 6765 has 4 digits.

 1 #include<iostream>
 2 using namespace std;
 3 int a[6001][1001];
 4 int getmaxlen(int fir,int sec)
 5 {
 6     int i,j;
 7     for(i=1000;a[fir][i]==0;i--);
 8     for(j=1000;a[sec][j]==0;j--);
 9     if(i>=j)
10         return i;
11     else
12         return j;
13 }
14 void fibonacci(int n)
15 {
16     int i,j,len;
17     int c=0;
18     for(j=2;j<n;j++)
19     {
20         len=getmaxlen(j-1,j-2);
21         for(i=0;i<=len+1;i++)
22         {
23             a[j][i]=(a[j-1][i]+a[j-2][i]+c)%10;
24             c=(a[j-1][i]+a[j-2][i]+c)/10;
25         }
26     }
27 }
28 int main()
29 {
30     int n,i,j;
31     memset(a,0,sizeof(a));
32     a[0][0]=1;
33     a[1][0]=1;
34     while(cin>>n)
35     {
36         for(i=1000;a[n-1][i]==0;i--);
37         if(i<0)
38         {    fibonacci(n);
39             for(i=1000;a[n-1][i]==0;i--);
40         }
41         for(j=i;j>=0;j--)
42             cout<<a[n-1][j];
43         cout<<endl;
44     }
45     return 0;
46 }

注:不能直接用公式,因为当数较大时用int型无法表示,这里一位一位计算,然后存储到数组中,最后输出。

时间: 2024-07-29 07:47:57

zoj 1828 Fibonacci Numbers的相关文章

zoj Fibonacci Numbers ( java , 简单 ,大数)

题目 //f(1) = 1, f(2) = 1, f(n > 2) = f(n - 1) + f(n - 2) import java.io.*; import java.util.*; import java.math.*; public class Main { /** * @xqq */ public BigInteger an(int n) { BigInteger c; BigInteger a = BigInteger.valueOf(1); BigInteger b = BigIn

hdu 3117 Fibonacci Numbers

点击此处即可传送到hdu 3117 **Fibonacci Numbers** Problem Description The Fibonacci sequence is the sequence of numbers such that every element is equal to the sum of the two previous elements, except for the first two elements f0 and f1 which are respectively

codeforces 446C DZY Loves Fibonacci Numbers(数学 or 数论+线段树)

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation F1 = 1; F2 = 1; Fn = Fn - 1 + Fn - 2 (n > 2). DZY loves Fibonacci numbers very much. Today DZY gives you an array consisting of n integers: a1, a2, ...,

ZOJ 3774 Fibonacci的K次方和

In mathematics, Fibonacci numbers or Fibonacci series or Fibonacci sequence are the numbers of the following integer sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, ... By definition, the first two numbers in the Fibonacci sequence are

Even Fibonacci numbers

problem 2:Even Fibonacci numbers 题意:求斐波那契数列不大于4百万的值为偶数的和 代码如下: 1 #ifndef PRO2_H_INCLUDED 2 #define PRO2_H_INCLUDED 3 #include<iostream> 4 using namespace std; 5 6 namespace pro2{ 7 int solve(){ 8 int p1=1,p2=2,p,sum=0; 9 for(;p1<=4000000;){ 10 if

codeforces 446C DZY Loves Fibonacci Numbers 数论+线段树成段更新

DZY Loves Fibonacci Numbers Time Limit:4000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Appoint description:  System Crawler  (2014-07-14) Description In mathematical terms, the sequence Fn of Fibonacci numbers is defi

HDU 3117 Fibonacci Numbers(斐波那契前后四位,打表+取对+矩阵快速幂)

HDU 3117 Fibonacci Numbers(斐波那契前后四位,打表+取对+矩阵快速幂) ACM 题目地址:HDU 3117 Fibonacci Numbers 题意: 求第n个斐波那契数的前四位和后四位. 不足8位直接输出. 分析: 前四位有另外一题HDU 1568,用取对的方法来做的. 后四位可以用矩阵快速幂,MOD设成10000就行了. 代码: /* * Author: illuz <iilluzen[at]gmail.com> * Blog: http://blog.csdn.

Codeforces446C DZY Loves Fibonacci Numbers(线段树 or 分块?)

第一次看到段更斐波那契数列的,整个人都不会好了.事后看了题解才明白了一些. 首先利用二次剩余的知识,以及一些数列递推式子有下面的 至于怎么解出x^2==5(mod 10^9+9),我就不知道了,但是要用的时候可以枚举一下,把这些参数求出来之后就题目就可以转化为维护等比数列. 由于前面的常数可以最后乘,所以就等于维护两个等比数列好了. 下面我们来看如何维护一个等比数列.假如我对区间[L,R]的加上1,2,4,8...2^n的话,那么我只需要加一个标记x表示这个区间被加了多少次这样的2^n. 举个例

UVA 11582 Colossal Fibonacci Numbers! 数学

n比较小,最多n*n就回出现循环节.... Colossal Fibonacci Numbers! Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Problem F: Colossal Fibonacci Numbers! The i'th Fibonacci number f (i) is recursively defined in the