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 10 8 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
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std; typedef struct
{
long long m[][];
}mat; mat p={,,,},I={,,,}; mat calc(mat a,mat b)
{
int i,j,k;
mat c;
for(i=;i<;i++)
for(j=;j<;j++)
{
c.m[i][j]=;
for(k=;k<;k++)
{
c.m[i][j]+=a.m[i][k]*b.m[k][j]%;
}
c.m[i][j]%=;
}
return c;
} mat matirx(int n)
{
mat m=p,b=I;
while(n>=)
{
if(n&) b=calc(b,m);
n>>=;
m=calc(m,m);
}
return b;
} int main()
{
int i,a[],len;
int n;
double s,d;
a[]=;
a[]=;
for(i=; i<; i++)
a[i]=a[i-]+a[i-];
while(scanf("%d",&n)!=EOF)
{
if(n<)
printf("%d\n",a[n]);
else
{
s=log10(1.0/sqrt())+n*log10((+sqrt())/2.0);//fibonacci封闭公式求前四位
len=(int)s;
d=s+-len;
printf("%d...",(int)pow(,d)); mat tmp;
tmp=matirx(n); //矩阵连乘求后四位
printf("%04d\n",tmp.m[][]);
}
}
return ;
}

fibonacci封闭公式及矩阵连乘的更多相关文章

  1. fibonacci封闭公式

    Description 2007年到来了.经过2006年一年的修炼,数学神童zouyu终于把0到100000000的Fibonacci数列 (f[0]=0,f[1]=1;f[i] = f[i-1]+f ...

  2. fibonacci数列(二)_矩阵快速幂

    描述 In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For exampl ...

  3. UVA10655 Contemplation! Algebra —— 推公式、矩阵快速幂

    题目链接:https://vjudge.net/problem/UVA-10655 题意: a+b.ab的值分别为p.q,求a^n+b^n. 题解: 1.a.b未知,且直接求出a.b也不太实际. 2. ...

  4. Blocks 推出矩阵公式。矩阵快速密

    Blocks 设涂到第I块时,颜色A,B都为偶数的数量为ai,一奇一偶的数量为bi,都为奇数为ci,  那么涂到第i+1快时有 a[i+1]=2*a[i]+b[i]+0*c[i]; b[i+1]=2* ...

  5. 2017多校第10场 HDU 6172 Array Challenge 猜公式,矩阵幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6172 题意:如题. 解法: #include <bits/stdc++.h> using ...

  6. hdu5171(矩阵快速幂)

    传送门:GTY's birthday gift 题意:GTY的朋友ZZF的生日要来了,GTY问他的基友送什么礼物比较好,他的一个基友说送一个可重集吧!于是GTY找到了一个可重集S,GTY能使用神犇魔法 ...

  7. CF633H Fibonacci-ish II 莫队、线段树、矩阵乘法

    传送门 这题除了暴力踩标程和正解卡常数以外是道很好的题目 首先看到我们要求的东西与\(Fibonacci\)有关,考虑矩阵乘法进行维护.又看到\(n \leq 30000\),这告诉我们正解算法其实比 ...

  8. POJ2779 线性DP 或 杨氏三角 和 钩子公式

    POJ2779 线性DP 或 杨氏三角 和 钩子公式 本来就想回顾一下基础的线性DP谁知道今早碰到的都是这种大难题,QQQQ,不会 这个也没有去理解线性DP的解法,了解了杨氏三角和钩子公式,做出了PO ...

  9. 介绍Unity中相机的投影矩阵与剪切图像、投影概念

    这篇作为上一篇的补充介绍,主要讲Unity里面的投影矩阵的问题: 上篇的链接写给VR手游开发小白的教程:(三)UnityVR插件CardboardSDKForUnity解析(二) 关于Unity中的C ...

随机推荐

  1. JAVA SE 803 考试前突击

    考试的宗旨仍然是掌握基础知识,不过鉴于Oracle的这个认证考试还有不少的大家来找茬的成份在,所以一定一定要细心为上.   关于抽象类的坑点集合: 抽象类不是必须得有抽象方法,但有抽象方法的类必须是抽 ...

  2. Spark1.0新特性-->Spark SQL

    Spark1.0出来了,变化还是挺大的,文档比以前齐全了,RDD支持的操作比以前多了一些,Spark on yarn功能我居然跑通了.但是最最重要的就是多了一个Spark SQL的功能,它能对RDD进 ...

  3. C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件

    大家可以到http://www.icsharpcode.net/opensource/sharpziplib/ 下载SharpZiplib的最新版本,本文使用的版本为0.86.0.518,支持Zip, ...

  4. 关于app.config不能即时保存读取的解决方案

    public void saveValue(string Name, string Value) { ConfigurationManager.AppSettings.Set(Name, Value) ...

  5. C++ CompletionPort(完成端口)示例

    ECHO客户端 #include <WINSOCK2.H> #include <stdio.h> #define SERVER_ADDRESS "127.0.0.1& ...

  6. Inkpad绘图原理浅析

    本文新版本已转移到开源中国,欢迎前往指导. Inkpad是一款非常优秀的iPad矢量绘图软件,保管你一看见就忘不了.我的感觉是“一览众山小”.“相见甚晚”,以至于我写的TouchVG就是“小巫见大巫” ...

  7. vmware虚拟机工具vmware tools介绍及安装排错

    VMware Tools是VMware虚拟机中自带的一种增强工具,相当于VirtualBox中的增强功能(Sun VirtualBox Guest Additions),是VMware提供的增强虚拟显 ...

  8. ArcGIS平台中PostgreSQL数据连接配置总结

    通常用户在使用要素服务时,要求数据必须是存放在空间数据库中的.同时,需要将数据库注册到ArcGIS for Server,这样在发布服务时就不需要进行数据拷贝,从而可以节省磁盘空间及服务发布时间.以下 ...

  9. 【解决方案】VS2013外部工具中添加ildasm.exe

    VS2013安装在Win8.1的操作系统中,开始屏幕中找不到ildasm.exe没有显示,于是下面提供了一种方法将ildasm.exe工具添加到VS2013外部工具中,并将反编译的代码输出到VS201 ...

  10. ES6入门系列一(基础)

    1.let命令 Tips: 块级作用域(只在当前块中有效) 不会变量提升(必须先申明在使用) 让变量独占该块,不再受外部影响 不允许重复声明 总之:let更像我们熟知的静态语言的的变量声明指令 ES6 ...