fibonacci封闭公式及矩阵连乘
Description
What is the numerical value of the nth Fibonacci number?
Input
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
1
2
3
4
5
35
36
37
38
39
40
64
65
Sample Output
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封闭公式及矩阵连乘的更多相关文章
- fibonacci封闭公式
Description 2007年到来了.经过2006年一年的修炼,数学神童zouyu终于把0到100000000的Fibonacci数列 (f[0]=0,f[1]=1;f[i] = f[i-1]+f ...
- fibonacci数列(二)_矩阵快速幂
描述 In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For exampl ...
- UVA10655 Contemplation! Algebra —— 推公式、矩阵快速幂
题目链接:https://vjudge.net/problem/UVA-10655 题意: a+b.ab的值分别为p.q,求a^n+b^n. 题解: 1.a.b未知,且直接求出a.b也不太实际. 2. ...
- 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* ...
- 2017多校第10场 HDU 6172 Array Challenge 猜公式,矩阵幂
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6172 题意:如题. 解法: #include <bits/stdc++.h> using ...
- hdu5171(矩阵快速幂)
传送门:GTY's birthday gift 题意:GTY的朋友ZZF的生日要来了,GTY问他的基友送什么礼物比较好,他的一个基友说送一个可重集吧!于是GTY找到了一个可重集S,GTY能使用神犇魔法 ...
- CF633H Fibonacci-ish II 莫队、线段树、矩阵乘法
传送门 这题除了暴力踩标程和正解卡常数以外是道很好的题目 首先看到我们要求的东西与\(Fibonacci\)有关,考虑矩阵乘法进行维护.又看到\(n \leq 30000\),这告诉我们正解算法其实比 ...
- POJ2779 线性DP 或 杨氏三角 和 钩子公式
POJ2779 线性DP 或 杨氏三角 和 钩子公式 本来就想回顾一下基础的线性DP谁知道今早碰到的都是这种大难题,QQQQ,不会 这个也没有去理解线性DP的解法,了解了杨氏三角和钩子公式,做出了PO ...
- 介绍Unity中相机的投影矩阵与剪切图像、投影概念
这篇作为上一篇的补充介绍,主要讲Unity里面的投影矩阵的问题: 上篇的链接写给VR手游开发小白的教程:(三)UnityVR插件CardboardSDKForUnity解析(二) 关于Unity中的C ...
随机推荐
- React 根据官方总结的规范
1.语法上,根据生命周期方法执行的顺序编写代码 (1 生命周期方法[getDefaultProps, getInitialState, componentWillMount, componentDid ...
- OpenGL cubeMap
glsl 的reflect(I,N)其中I是 眼睛(camera)位置到顶点位置的方向向量,N为顶点法线,必须要归一化 橙宝书里给出的计算过程是这样的:reflect(I,N) = I - 2 *do ...
- ODAC(V9.5.15) 学习笔记(十七)主从模式
主从模式(Master/Detail mode)是指建立主表和从表关系的多个数据集集合模式. 1. 关系设置 要设置主从模式,必须有一个主表数据集(TDataSet)和一个从表数据集(TDataSet ...
- php报错: PHP Warning: PHP Startup: memcache: Unable to initialize module
在mac上通过brew 安装php的memcache扩展(brew install php56-memcache)后运行 ~ php -mPHP Warning: PHP Startup: mem ...
- DevExpress.XtraGrid winform试用分享
DevExpress.XtraGrid在winform里使用还挺麻烦,为了减少重复代码和代码复用,本人针对DevExpress.XtraGrid封装了一个Form的基类,其中涵盖了DevExpress ...
- Codeforces Round #237 (Div. 2) C. Restore Graph(水构造)
题目大意 一个含有 n 个顶点的无向图,顶点编号为 1~n.给出一个距离数组:d[i] 表示顶点 i 距离图中某个定点的最短距离.这个图有个限制:每个点的度不能超过 k 现在,请构造一个这样的无向图, ...
- Privacy Policy
MINE Privacy Policy This unit values your privacy. You are using our service, we may collect and use ...
- Frogger(floyd变形)
Frogger Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Stat ...
- [Ubuntu] bash: warning: setlocale: LC_ALL: cannot change locale
问题症状 -bash: warning: setlocale: LC_ALL: cannot change locale (en_US.utf8) 解决方法 本地化是指不同地区用户在键盘上输入不同语言 ...
- PE渲染引擎 二
增加了DOF