51nod 1242 斐波那契数列的第N项【矩阵快速幂】
来源:互联网 发布:unity3d建筑可视化 编辑:程序博客网 时间:2024/05/20 13:15
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1242
代码:
#include <stdio.h>#include <iostream>#include <string.h>#include <algorithm>#include <math.h>#include <ctype.h>#include <time.h>#include <queue>using namespace std;const int MOD = 1000000009;struct node{ long long m[2][2];}ans, base;long long n;node multi(node a, node b){ node tmp; for (int i = 0;i<2;i++) for (int j = 0;j<2;j++) { tmp.m[i][j] = 0; for (int k = 0;k<2;k++) { tmp.m[i][j] += (a.m[i][k] * b.m[k][j]); tmp.m[i][j] %= MOD; } } return tmp;}long long fast_mod(long long n)// 求矩阵 base 的 n 次幂 { base.m[0][0] = base.m[0][1] = base.m[1][0] = 1; base.m[1][1] = 0; ans.m[0][0] = ans.m[1][1] = 1;// ans 初始化为单位矩阵 ans.m[0][1] = ans.m[1][0] = 0; while (n) { if (n & 1) //实现 ans *= t; 其中要先把 ans赋值给 tmp,然后用 ans = tmp * t ans = multi(ans, base); base = multi(base, base); n >>= 1; } return ans.m[0][1];}int main(){ while (scanf("%lld", &n) != EOF) { int ans = fast_mod(n); printf("%lld\n", ans); } return 0;}
0 0
- 51nod 1242 斐波那契数列的第N项(矩阵快速幂)
- 51nod 1242 斐波那契数列的第N项【矩阵快速幂】
- 51nod 1242 斐波那契数列的第N项 (矩阵快速幂)
- 【51Nod】1242 - 斐波那契数列的第N项(矩阵快速幂)
- 51nod 1242 斐波那契数列的第N项(矩阵快速幂)
- [51NOD]-1242 斐波那契数列的第N项 [矩阵快速幂]
- 51 nod 1242 斐波那契数列的第N项 矩阵快速幂
- 51Nod 1242 斐波那契数列的第N项(矩阵快速幂)
- 51Nod-斐波那契数列的第N项(矩阵快速幂)
- 51nod 1242 斐波那契数列的第N项(矩阵幂运算)
- 矩阵快速幂1242斐波那契数列的第N项
- 1242 斐波那契数列的第N项(矩阵快速幂)
- 1242 斐波那契数列的第N项(矩阵快速幂)
- 【51NOD1242】斐波那契数列的第N项(矩阵快速幂)
- 51nod--1242 斐波那契数列第N项 (矩阵乘法优化)
- 斐波那契数列的第N项 矩阵快速幂
- 斐波那契数列的第N项(矩阵快速幂模板)
- 1242 斐波那契数列的第N项 运用矩阵快速幂来求解斐波那契数列问题
- ThinkPHP3.2.3升级的若干问题和注意事项
- HTTP协议详解
- 51nod 1256 乘法逆元
- 欢迎使用CSDN-markdown编辑器
- 3D models 加载
- 51nod 1242 斐波那契数列的第N项【矩阵快速幂】
- Python学习笔记<LearnPythonHardWay>
- 编译原理自学开坑
- Windows多线程技术
- 存储管理:内存、磁盘
- LA 5095 Transportation
- 博文第二天,一切刚刚开始
- 51nod 1212 无向图最小生成树
- 使用layout方法实现控件的移动并且不影响点击事件