Fibonacci----poj3070(矩阵快速幂, 模板)
2015-09-20 21:26
573 查看
题目链接:http://poj.org/problem?id=3070
View Code
对比着想想快速幂;
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include<math.h> using namespace std; #define N 10 struct node { int a ; }s,e; node mul(node p, node q)///求两个矩阵的积; { node tem; for(int i=0; i<2; i++) { for(int j=0; j<2; j++) { tem.a[i][j] = 0; for(int k=0; k<2; k++) tem.a[i][j] = (tem.a[i][j]+p.a[i][k]*q.a[k][j])%10000; } } return tem; } node Pow(node p, int n) { node tem; for(int i=0; i<2; i++) for(int j=0; j<2; j++) tem.a[i][j] = (i==j); while(n) { if(n&1)///n是奇数; tem = mul(tem, p);///让矩阵e和矩阵a相乘; n/=2; p = mul(p, p); } return tem; } int main() { s.a[0][0] = 1; s.a[0][1] = 1; s.a[1][0] = 1; s.a[1][1] = 0; int n; while(scanf("%d", &n),n!=-1) { e = Pow(s, n); printf("%d\n", e.a[0][1]); } return 0; }
View Code
对比着想想快速幂;
相关文章推荐
- 九点二十
- 利用caffe训练网络的步骤
- 我的无助
- wordpress Oops! That page can’t be found.
- andriod开发点滴(使用真机调试)
- cocos2d-x Scale9Sprite的使用方法 lua篇
- 安卓编程实现打开关闭屏幕常亮
- jdbc原理
- 保护隐私:Windows五大保密技巧
- 暴力求包含n个点的圆的最小半径
- 如何删除wordpress侧边栏功能的wordpress.org链接
- java使用jgit提交代码
- 文章标题
- 一个Bug的反思
- iOS 中常用的四种数据持久化的方法
- iOS ARC简要介绍
- 二维数组的表示及输出
- 优先队列
- 自定义结构作为map的key,比较函数写法
- hdu1285(拓扑排序)