51Nod 1242:斐波那契数列的第N项
来源:互联网 发布:前端ajax获取json数据 编辑:程序博客网 时间:2024/06/04 08:54
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1242
N比较大,用矩阵快速幂解决。
与POJ 3070题目一样。
AC代码:
#include <iostream>#include <stdio.h>#include <math.h>#include <string.h>using namespace std;typedef long long LL;const int N = 5;LL temp[N][N];LL res[N][N];const int mod = 1000000009;void Mul(LL a[][N],LL b[][N]){ memset(temp,0,sizeof(temp)); for(int i = 0; i < 2; i++) ///i行 for(int j = 0; j < 2; j++) ///j列 for(int k = 0; k < 2; k++) temp[i][j] = (temp[i][j]+(a[i][k]*b[k][j])%mod)%mod; for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++) a[i][j] = temp[i][j];}void Solve(LL a[][N],LL n) ///n是求的幂次{ memset(res,0,sizeof(res)); for(int i = 0; i < 2; i++) res[i][i] = 1; while(n) { if(n&1) Mul(res,a); Mul(a,a); n>>=1; }}int main(){ LL T; while(~scanf("%lld",&T)) { LL a[N][N]; a[0][0] = 1; a[0][1] = 1; a[1][0] = 1; a[1][1] = 0; if(T == 0 || T == 1) printf("%lld\n",T); else { Solve(a,T); printf("%lld\n",res[0][1]); } } return 0;}
阅读全文
0 0
- 51nod 1242 1242 斐波那契数列的第N项
- 20140911 【 初等数论 】 51nod 1242 . 斐波那契数列的第N项
- 51nod 1242 斐波那契数列的第N项(矩阵快速幂)
- 51nod 1242 斐波那契数列的第N项【矩阵快速幂】
- 51nod 1242 斐波那契数列的第N项 (矩阵快速幂)
- 51Nod 1242 斐波那契数列的第N项
- 51Nod-1242-斐波那契数列的第N项
- 51nod:1242 斐波那契数列的第N项
- 【51Nod】1242 - 斐波那契数列的第N项(矩阵快速幂)
- 51nod 1242 斐波那契数列的第N项(矩阵快速幂)
- 51nod 1242 斐波那契数列的第N项(O(logn)求递推式)
- 51nod 1242 斐波那契数列的第N项
- 51nod 1242 斐波那契数列的第N项
- 51NOD 1242 斐波那契数列的第N项
- [51NOD]-1242 斐波那契数列的第N项 [矩阵快速幂]
- 【51Nod】1242 斐波那契数列的第N项
- 51nod 1242 斐波那契数列的第N项
- 51 nod 1242 斐波那契数列的第N项 矩阵快速幂
- 【Java-Spring开发】aop注解 自定义切面的注解写法
- hdu 1286 找新朋友,欧拉函数
- angularjs2 组件的生命周期
- 自然数立方的乐趣
- TCP与UPD知识总结
- 51Nod 1242:斐波那契数列的第N项
- 【牛客网】直通bat-面试算法精品课_第2章 排序 2.5 归并排序练习题(JAVA版)
- hdu-1258-Sum It Up(DFS)
- 区别#define、enum和const
- A Dangerous Maze LightOJ
- Polycarp and Letters
- hdu2853 Assignment
- poj 3984 bfs+路径还原
- 安卓 手写消息机制