UESTC 1335 Fibonacci
来源:互联网 发布:匈牙利算法步骤 编辑:程序博客网 时间:2024/06/05 17:54
点击打开UESTC 1335
思路: 矩阵快速幂
分析:
1 最简单的矩阵快速幂
代码:
/************************************************ * By: chenguolin * * Date: 2013-08-28 * * Address: http://blog.csdn.net/chenguolinblog * ************************************************/#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int MOD = 10000;const int N = 2;int n;struct Matrix{ int mat[N][N]; Matrix operator*(const Matrix& m)const{ Matrix tmp; for(int i = 0 ; i < N ; i++){ for(int j = 0 ; j < N ; j++){ tmp.mat[i][j] = 0; for(int k = 0 ; k < N ; k++) tmp.mat[i][j] += mat[i][k]*m.mat[k][j]%MOD; tmp.mat[i][j] %= MOD; } } return tmp; }};int Pow(Matrix m){ if(n <= 1) return n; Matrix ans; memset(ans.mat , 0 , sizeof(ans.mat)); for(int i = 0 ; i < N ; i++) ans.mat[i][i] = 1; n--; while(n){ if(n%2) ans = ans*m; n /= 2; m = m*m; } return ans.mat[0][0]%MOD;}int main(){ Matrix m; m.mat[0][0] = m.mat[0][1] = 1; m.mat[1][0] = 1 ; m.mat[1][1] = 0; while(scanf("%d" , &n) && n != -1) printf("%d\n" , Pow(m)); return 0;}
- UESTC 1335 Fibonacci
- UESTC - 278 Fibonacci
- UESTC
- UESTC
- UESTC
- UESTC
- UESTC
- UESTC
- UESTC
- UESTC
- UESTC
- UESTC
- UESTC
- UESTC
- UESTC
- UESTC
- UESTC
- UESTC
- 深度解析 TypeConverter & TypeConverterAttribute (二)
- VB语言基础
- Yii自动生成项目
- C语言完整笔记
- thinksns 核心架构及目录结构
- UESTC 1335 Fibonacci
- c#实现震动窗体
- 各种光照算法
- javascript对象小探之二——length
- 成功源于细节-河边的苹果
- 窗口大小改变事件handler
- Python Ctypes结构体指针处理(函数参数,函数返回)
- 哈佛经济学家关于工作效率的意外发现
- 在cocos2dx中製作人物文本框