Fibonacci Numbers

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 81 Accepted Submission(s): 46
 
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 zero and one.

What is the numerical value of the nth Fibonacci number?
 
Input
For each test case, a line will contain an integer i between 0 and 108 inclusively, for which you must compute the ith Fibonacci number fi. Fibonacci numbers get large pretty quickly, so whenever the answer has more than 8 digits, output only the first and last 4 digits of the answer, separating the two parts with an ellipsis (“...”).

There is no special way to denote the end of the of the input, simply stop when the standard input terminates (after the EOF).

 
 
Sample Input
0
1
2
3
4
5
35
36
37
38
39
40
64
65
 
Sample Output
0
1
1
2
3
5
9227465
14930352
24157817
39088169
63245986
1023...4155
1061...7723
1716...7565
 
 
Source
IPCP 2005 Northern Preliminary for Northeast North-America
 
Recommend
lcy
 
/*
题意:求第n个斐波那契数列的值,只需要前四位,后四位 初步思路:后四位好说,膜一下就行了重要的就是前四位.总共1e8的时间,感觉用大数爆都会超时 #补充:后四位矩阵膜10000就行了,前四位可以用通项公式取对数的方法求。 */
#include<bits/stdc++.h>
#define ll long long
#define mod 10000
using namespace std;
/********************************矩阵模板**********************************/
class Matrix {
public:
int a[][]; void init(int x) {
memset(a,,sizeof(a));
if (x)
for (int i = ; i < ; i++)
a[i][i] = ;
} Matrix operator +(Matrix b) {
Matrix c;
for (int i = ; i < ; i++)
for (int j = ; j < ; j++)
c.a[i][j] = (a[i][j] + b.a[i][j]) % mod;
return c;
} Matrix operator +(int x) {
Matrix c = *this;
for (int i = ; i < ; i++)
c.a[i][i] += x;
return c;
} Matrix operator *(Matrix b)
{
Matrix p;
p.init();
for (int i = ; i < ; i++)
for (int j = ; j < ; j++)
for (int k = ; k < ; k++)
p.a[i][j] = (p.a[i][j] + (a[i][k]*b.a[k][j])%mod) % mod;
return p;
} Matrix power_1(int t) {
Matrix Frist,p = *this;
Frist.init();
while (t) {
if (t & )
Frist=Frist*p;
p = p*p;
t >>= ;
}
return Frist;
} Matrix power_2(Matrix a,Matrix b,int x){
while(x){
if(x&){
b=a*b;
}
a=a*a;
x>>=;
}
return b;
}
};
/********************************矩阵模板**********************************/
Matrix unit,init;
ll f[];
ll n;
int main(){
// freopen("in.txt","r",stdin);
f[]=;
f[]=;
for(int i=;i<;i++){
f[i]=f[i-]+f[i-];
}
while(scanf("%lld",&n)!=EOF){
if(n<){
printf("%lld\n",f[n]);
continue;
}
unit.a[][]=;
unit.a[][]=;
unit.a[][]=;
unit.a[][]=; init.a[][]=;
init.a[][]=;
init.a[][]=;
init.a[][]=; init=init.power_1(n-);//有问题
unit=unit*init;
int Last=unit.a[][]; long double Frist=-0.5 * log(5.0) / log(10.0) + ((long double)n) * log((sqrt(5.0)+1.0)/2.0) / log(10.0);
Frist-=floor(Frist);
Frist=pow(,Frist);
while(Frist<)
Frist*=;
printf("%d...%04d\n",(int) Frist,Last);
}
return ;
}

Fibonacci Numbers的更多相关文章

  1. codeforces 446C DZY Loves Fibonacci Numbers(数学 or 数论+线段树)(两种方法)

    In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation F1 ...

  2. Codeforces 446-C DZY Loves Fibonacci Numbers 同余 线段树 斐波那契数列

    C. DZY Loves Fibonacci Numbers time limit per test 4 seconds memory limit per test 256 megabytes inp ...

  3. cf446C DZY Loves Fibonacci Numbers

    C. DZY Loves Fibonacci Numbers time limit per test 4 seconds memory limit per test 256 megabytes inp ...

  4. Codeforces Round #FF 446 C. DZY Loves Fibonacci Numbers

    參考:http://www.cnblogs.com/chanme/p/3843859.html 然后我看到在别人的AC的方法里还有这么一种神方法,他预先设定了一个阈值K,当当前的更新操作数j<K ...

  5. HDU 3117 Fibonacci Numbers(围绕四个租赁斐波那契,通过计++乘坐高速动力矩阵)

    HDU 3117 Fibonacci Numbers(斐波那契前后四位,打表+取对+矩阵高速幂) ACM 题目地址:HDU 3117 Fibonacci Numbers 题意:  求第n个斐波那契数的 ...

  6. Codeforces446C - DZY Loves Fibonacci Numbers

    Portal Description 给出一个\(n(n\leq3\times10^5)\)个数的序列,进行\(m(m\leq3\times10^5)\)次操作,操作有两种: 给区间\([L,R]\) ...

  7. UVA 11582 Colossal Fibonacci Numbers(数学)

    Colossal Fibonacci Numbers 想先说下最近的状态吧,已经考完试了,这个暑假也应该是最后刷题的暑假了,打完今年acm就应该会退了,但是还什么都不会呢? +_+ 所以这个暑假,一定 ...

  8. HDU 3117 Fibonacci Numbers(矩阵)

    Fibonacci Numbers [题目链接]Fibonacci Numbers [题目类型]矩阵 &题解: 后4位是矩阵快速幂求,前4位是用log加Fibonacci通项公式求,详见上一篇 ...

  9. [CodeForces - 447E] E - DZY Loves Fibonacci Numbers

    E  DZY Loves Fibonacci Numbers In mathematical terms, the sequence Fn of Fibonacci numbers is define ...

随机推荐

  1. [js高手之路] es6系列教程 - Set详解与抽奖程序应用实战

    我们还是从一些现有的需求和问题出发,为什么会有set,他的存在是为了解决什么问题? 我们看一个这样的例子,为一个对象添加键值对 var obj = Object.create( null ); obj ...

  2. java类成员的访问权限

    private:这是安全级别最高的访问权限,尽在该类中可以访问到: 默认访问权限:又称包访问权限,顾名思义,仅在该包内可以访问: protected:在包访问权限的基础上,该类的子类也可以访问到: p ...

  3. Dijkstra堆优化学习

    最短路径例题 今天特地学习了Dijkstra的堆优化(主要是慕名已久). 我们需要一个堆来记录[编号,到编号这个点的最短路径值(当然只是当前的)] 与原来的Dijkstra操作基本一致,主要有以下几点 ...

  4. java编程基础复习-------第二章

    一.标识符 java中标识符的命名规则: 以数字.字母.下划线和$符号组成:不能用数字开头:不能是java的关键字. 注意:不要用$命名标识符.习惯上,$只用在机器自动产生的源代码中. 二.关键字 1 ...

  5. Kafka快速上手(2017.9官方翻译)

    为了帮助国人更好了解.上手kafka,特意翻译.修改了个文档.官方Wiki : http://kafka.apache.org/quickstart 快速开始 本教程假定您正在开始新鲜,并且没有现有的 ...

  6. 无限树Jquery插件zTree的使用方法

    其实Ztree官网已经有详细的API文档,一切以官网上的说明为准,我在此只是结合实践总结几条常用的ztree的功能特性. (ztree的语法结构是基于key-value的形式配置) 1:支持异步加载数 ...

  7. 《算法导论》学习总结 — XX.第23章 最小生成树

    一.什么叫最小生成树 一个无向连通图G=(V,E),最小生成树就是联结所有顶点的边的权值和最小时的子图T,此时T无回路且连接所有的顶点,所以它必须是棵树. 二.为什么要研究最小生成树问题 <算法 ...

  8. zoj1002 Fire Net

    Fire Net Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose that we have a square city with s ...

  9. 发布系统Git使用指南 - the Git Way to Use Git

    发布系统Git使用指南 --the Git Way to Use Git 背景 ​ 有文章曾归纳,Git是一套内容寻址文件系统,意思是,Git的核心是存储键值对^[1]^.显然,这样的形式不利于普通人 ...

  10. 学习总结---SNAT和DNAT

    1.SNAT是结合源ip+源端口号变化的NAT功能. DNAT是将目的ip直接转换成私有的目的ip.(是否转换目的端口号?) 2.SNAT的应用场景:公司内部访问互联网时,使用公共的公网ip.从内到外 ...