矩阵乘法+快速幂求斐波那契
2015-10-29 21:13
344 查看
#include<iostream> #include<algorithm> #include<cmath> using namespace std; int n,m; int a[2][2]={1,1,1,0}; int b[2][2]={1,0,0,1}; void jzcf(int a[2][2],int b[2][2]) { int i,j,k; int c[2][2]; for(i=0;i<2;i++) { for(j=0;j<2;j++) { c[i][j]=0; for(k=0;k<2;k++) { c[i][j]=(c[i][j]+a[i][k]*b[k][j]); } } } for(i=0;i<2;i++) { for(j=0;j<2;j++) { b[i][j]=c[i][j]; } } } void ksm(int n) { if(n==0) { cout<<0<<endl; } else { n--; while(n) { if(n%2==1) { jzcf(a,b); } jzcf(a,a); n/=2; } cout<<b[0][0]<<endl; } } int main() { while(cin>>n) { ksm(n); a[0][0]=a[0][1]=a[1][0]=b[0][0]=b[1][1]=1; a[1][1]=b[0][1]=b[1][0]=0; } return 0; }
大神博客:
母函数http://blog.csdn.net/xuzengqiang/article/details/7464337
http://www.cnblogs.com/dongsheng/archive/2013/06/02/3114073.html
原理:
f(n+1) 0 1 1 f(n) 0 1 1 1 1 f(n-1) 0 1 1 f(1) 0
= * = * * = * ' ' '*
f(n) 0 1 0 f(n-1) 0 1 0 1 0 f(n-1) 0 1 0 f(0) 1
相关文章推荐
- 基于saltstack svn的一个发布系统
- 二分图的最大匹配
- hdoj--1251--统计难题(字典树)
- 挖掘频繁模、关联和相关性(2)
- android studio使用jcenter上的开源库
- SOM-TL28335核心板规格书
- [原创] Cygwin完全下载指南(上)-正常下载篇
- hdoj--1251--统计难题(字典树)
- Nginx缓存配置及nginx ngx_cache_purge模块的使用
- 递归算法小例子
- ios应用数据存储方式(归档)
- 初识onMeasure()——解决ScrollView内嵌ListView滑动冲突
- 《软件需求十步走》阅读笔记三
- 使用WPR和WPA分析线程等待(分析锁的影响)
- java基础语法
- Xen的安装、编译和使用(一)
- 安装文件服务器资源管理器
- 关于正向索引与反向索引
- 你不知道的JavaScript--Item22 Date对象全解析
- hdoj 1698 Just a Hook 【线段树区间修改】