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. Photoshop操作指南

    缩放工具(Z) 之后涉及到移动功能,暂时切换到小抓手工具. 图1 图2 切换到:暂时切换到:当按住空格键的时候,显示小抓手工具.松开之后,又回到了之前的显示上. 满画布显示当前画面:ctrl+0(数字 ...

  2. iOS touch事件单击双击区分响应

    如果您的 iPhone 应用里有个 view,既有单击操作又有双击操作.用户双击 view 时,总是先执行一遍单击的操作再执行双击的操作.所以直接判断时就会发现不能直接进入双击操作.下面是区分 tou ...

  3. Spring Boot 之 HelloWorld详解

    摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “以前是人放狗看家,现在是狗牵着人散步” — 随笔 一.Spring Boot 自述 世界上最好 ...

  4. jquery-migrate.js

    这个插件可以用来检测和恢复在jQuery1.9版本中已删除或已过时的API.

  5. Firefox火狐添加书签功能失灵解决办法

    一直使用firefox,书签管理用的是插件Tabmix.感觉很好,只是不知道从那天起添加书签就不灵了!不论是Ctrl+D快捷键,还是Add Bookmark Here2插件,甚至“将此页加为书签”的菜 ...

  6. {转}Unity3d+Jenkins 自动编译iOS、Android版本(U3D远程自动打包工具)

    http://www.cnblogs.com/yinghuochong/archive/2013/09/01/3294940.html

  7. php 连接redis,并登录验证

    环境: centos7 上安装了redis, 同时安装了php的redis扩展 yum install redis yum install php-pecl-redis redis服务端设置了登录密码 ...

  8. IT项目经理成长手记

    1.流程化,项目化,工业化: 2.启动,规划,实施,监控,收尾.五个过程 3.需求管理,项目策划,项目监控,集成项目管理,定量项目管理,供应商协议管理,风险管理. 4.项目经理是熬出来的,伟大都是熬出 ...

  9. MSIL解析一(转)

    转自:http://www.cnblogs.com/Yahong111/archive/2007/08/15/857140.html 在网上发现了一个非常好的MSIL教程,可惜是英文版的,于是就翻译了 ...

  10. HTML CSS——margin和padding的学习

    你在学习margin和padding的时候是不是懵了,——什么他娘的内边距,什么他娘的外边距.呵呵呵,刚开始我也有点不理解,后来通过查资料学习总算弄明白了,现在我来谈一下自己对margin和paddi ...