【难】【数学】斐波那契数列的O(logn)解法
来源:互联网 发布:域名可以一年一年续费 编辑:程序博客网 时间:2024/06/06 04:12
题目:《编程之美》 P163
class Matrix{public:unsigned int a11, a12, a21, a22;Matrix(int a, int b, int c, int d) :a11(a), a12(b), a21(c), a22(d){}//重载矩阵的乘法Matrix operator*(const Matrix &other){Matrix r(0,0,0,0);r.a11 = a11*other.a11 + a12*other.a21;r.a12 = a11*other.a12 + a12*other.a22;r.a21 = a21*other.a11 + a22*other.a21;r.a22 = a21*other.a12 + a22*other.a22;return r;}};//计算矩阵A的m次方Matrix MatrixPow(const Matrix &A,unsigned int m){Matrix res(1, 0, 0, 1);//单位矩阵Matrix cur = A;while (m){if (m & 1)res = res*cur;cur = cur*cur;m = m >> 1;}return res;}unsigned int Fibonacci(int n){if (n < 0)throw new exception;if (n == 0 || n == 1)return n;Matrix A(1, 1, 1, 0);Matrix r = MatrixPow(A, n - 1);return r.a11;}
0 0
- 【难】【数学】斐波那契数列的O(logn)解法
- 斐波那契数列的最优算法(O(logN))
- 51nod 1242 斐波那契数列的第N项(O(logn)求递推式)
- O(logN) 计算经典斐波那契数列的某个数
- 求斐波那契数列O(logn算法)
- 斐波那契数列 logn
- Fibonacci 数列O(logn)解法
- 斐波那契数列的解法?
- 斐波那契数列的解法
- 斐波那契数列解法
- 高效斐波那契数列解法
- 斐波那契数列解法
- 斐波那契数列实用解法
- 斐波那契数列的几种计算机解法
- 关于斐波那契数列的几种解法
- 斐波那契数列的几种不同解法
- 斐波那契数列的低效与高效解法
- 斐波那契数列的几种计算机解法
- js基础巩固之--变量类型以及声明
- 数据可视化:基本图表
- LeetCode 3Sum Closest
- day05-servlet
- 第一章 python中的变量
- 【难】【数学】斐波那契数列的O(logn)解法
- [Debug]Android 编辑xml中Incorrect line ending错误解决方法
- [LeetCode]Add Two Numbers
- HTTP协议详解
- 个人学习计划
- Java开源建站工具
- POJ 3006 Dirichlet's Theorem on Arithmetic Progressions(素数筛+等差数列)
- linux内核模块安装hello
- Linux core dump 分析及相关调试