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. 学习之路三十九:新手学习 - Windows API

    来到了新公司,一开始就要做个程序去获取另外一个程序里的数据,哇,挑战性很大. 经过两周的学习,终于搞定,主要还是对Windows API有了更多的了解. 文中所有的消息常量,API,结构体都整理出来了 ...

  2. Unity3D]引擎崩溃、异常、警告、BUG与提示总结及解决方法

    此贴会持续更新,都是项目中常会遇到的问题,总结成贴,提醒自己和方便日后检查,也能帮到有需要的同学. 若各位有啥好BUG好异常好警告好崩溃可以分享的话,请多多指教.xuzhiping7#qq.com. ...

  3. dsoframer控件学习小结(打开WORD,EXCEL等文件)

    根据自己对dsoframer控件的学习,想把dsoframer控件进行简单的包装为C#的usercontrol,大体需要作如下:(创建windows的usercontrol的步骤就不再说了...)我们 ...

  4. 【转】mac/linux终端光标的快捷键操作

    摘自网络:原标题是类似linux/unix命令行终端的光标及字符控制快捷键的东东. 常用的快捷键: Ctrl + d 删除一个字符,相当于通常的Delete键(命令行若无所有字符,则相当于exit:处 ...

  5. 初探Stage3D(二) 了解AGAL

    关于本文 本文并无打算事无巨细的介绍一遍AGAL,仅仅是对现有文档的一些理解及汇总,所以请先阅读相参考文档 AGAL概念 参考资料 http://www.adobe.com/devnet/flashp ...

  6. CSS基础(二):基础和语法

    CSS语法 CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明.选择器通常是您需要改变样式的 HTML 元素. selector {declaration1; declaration2; . ...

  7. STAF自动化测试框架

    STAF自动化测试框架介绍 http://baike.baidu.com/link?url=9oPZN3JntRakidI7xizqCbyGRISMvCKGfXHBB_WH7OAkKjAKZjq88q ...

  8. RadioButton 的background属性表现特征

    对于radiaoButton,应该很多人都用过.下面看一个场景     上方时radiogroup,细致观察发现左1,文字开始位置和右1文字开始位置不同,这是为何呢? 查看布局: <RadioB ...

  9. JavaScript ---属性

    获取属性 可以通过点(.)或方挂号([])运算符来获取属性的值. 对于点(.)来说,右侧必须是一个以属性名称命名的简单标识符.对于[]来说,方挂号内必须是一个计算结果为字符串的表达式. var aut ...

  10. JDBC连接SQL Server2008

    在使用JDBC连接数据库之前首先要加载相应数据库的JDBC驱动类,可以通过通用方法Class.forName来加载驱动类.   方式一:使用JDBC-ODBC连接桥 一般安装JDK后会自带JDBC-O ...