编程之美: 第二章 数字之魅 2.9斐波那契数列

/*
斐波那契数列:
解法二:求通项公式的特征方程为
x^2 = x + 1
所以x1,2 = (1+ - 根号5 )/2;
所以存在A,B使得
F(n) = A*((1+根号5)/2)^n  + b*((1-根号5)/2)^n
带入F(0)=0,F(1) = 1
解得A = 根号5/5,B= -根号5/5
F(n) = 根号5/5 * ((1+根号5)/2)^n   - 根号5/5 * ((1-根号5)/2)^n
可以在O(1)时间得到F(n)

解法三:
分治策略

注意Fibonacci是二阶递推数列,存在一个2*2矩阵A,使得
(Fn,Fn-1) = (Fn-1,Fn-2)*A = (F1,F0)*A^(n-1),A= [1 1],F1 = 1,F0 = 0
                                                  [1 0]

输入:
11
输出:
89
*/

/*
guanjian :
1 	matrixOrigin = power_matrix(matrixOrigin,n-1);//注意Fibonacci是二阶递推数列,存在一个2*2矩阵A,使得
//(Fn,Fn-1) = (Fn-1,Fn-2)*A = (F1,F0)*A^(n-1),A= [1 1],F1 = 1,F0 = 0
//                                                  [1 0]注意Fibonacci是二阶递推数列,存在一个2*2矩阵A,使得
//(Fn,Fn-1) = (Fn-1,Fn-2)*A = (F1,F0)*A^(n-1),A= [1 1],F1 = 1,F0 = 0
//                                                  [1 0]
2 	Matrix operator*(const Matrix& rhs)//定义矩阵乘法
	{
		Matrix ret(0,0,0,0);
		ret._lMatrix[0][0] = _lMatrix[0][0] * rhs._lMatrix[0][0] + _lMatrix[0][1] * rhs._lMatrix[1][0];//参见举证乘法运算
3 	Matrix ret(1,0,0,1);
	Matrix tmp = m;
	for( ; n ; n >>= 1 )//循环的结束条件是n为0,每次n都除以2
	{
		if(n & 1)//如果n为奇数,等于把最后一次的值乘起来
		{
			ret = ret * tmp;
		}
		tmp = tmp * tmp;
4 	double a = pow( (1.0 + sqrt(5.0))/2.0,n);//F(n) = 根号5/5 * ((1+根号5)/2)^n   - 根号5/5 * ((1-根号5)/2)^n
*/

#include <stdio.h>
#include <iostream>
#include <math.h>

using namespace std;

class Matrix
{
public:
	long long _lMatrix[2][2];
	Matrix(long long a,long long b,long long c,long long d)
	{
		_lMatrix[0][0] = a;
		_lMatrix[0][1] = b;
		_lMatrix[1][0] = c;
		_lMatrix[1][1] =d;
	}
	Matrix& operator=(const Matrix& other)
	{
		if(this != &other)
		{
			_lMatrix[0][0] = other._lMatrix[0][0];
			_lMatrix[0][1] = other._lMatrix[0][1];
			_lMatrix[1][0] = other._lMatrix[1][0];
			_lMatrix[1][1] = other._lMatrix[1][1];
		}
		return *this;
	}
	Matrix operator*(const Matrix& rhs)//定义矩阵乘法
	{
		Matrix ret(0,0,0,0);
		ret._lMatrix[0][0] = _lMatrix[0][0] * rhs._lMatrix[0][0] + _lMatrix[0][1] * rhs._lMatrix[1][0];//参见举证乘法运算
		ret._lMatrix[0][1] = _lMatrix[0][0] * rhs._lMatrix[0][1] + _lMatrix[0][1] * rhs._lMatrix[1][1];
		ret._lMatrix[1][0] = _lMatrix[1][0] * rhs._lMatrix[0][0] + _lMatrix[1][1] * rhs._lMatrix[1][0]; 
		ret._lMatrix[1][1] = _lMatrix[1][0] * rhs._lMatrix[0][1] + _lMatrix[1][1] * rhs._lMatrix[1][1];
		return ret;
	}
};

Matrix power_matrix(Matrix& m,int n)//O(log2N)
{
	Matrix ret(1,0,0,1);
	Matrix tmp = m;
	for( ; n ; n >>= 1 )//循环的结束条件是n为0,每次n都除以2
	{
		if(n & 1)//如果n为奇数,等于把最后一次的值乘起来
		{
			ret = ret * tmp;
		}
		tmp = tmp * tmp;
	}
	return ret;
}

long long fibonacci_matrix(int n)
{
	Matrix matrixOrigin(1,1,1,0);
	matrixOrigin = power_matrix(matrixOrigin,n-1);//注意Fibonacci是二阶递推数列,存在一个2*2矩阵A,使得
//(Fn,Fn-1) = (Fn-1,Fn-2)*A = (F1,F0)*A^(n-1),A= [1 1],F1 = 1,F0 = 0
//                                                  [1 0]注意Fibonacci是二阶递推数列,存在一个2*2矩阵A,使得
//(Fn,Fn-1) = (Fn-1,Fn-2)*A = (F1,F0)*A^(n-1),A= [1 1],F1 = 1,F0 = 0
//                                                  [1 0]
	return matrixOrigin._lMatrix[0][0];//其实应该返回F1*an(0,0) + F0*an(1,0),但是由于F1为1,F0 = 0简化了
}

long long fibonacci_equation(int n)
{
	double f = sqrt(5.0)/5.0;
	double a = pow( (1.0 + sqrt(5.0))/2.0,n);//F(n) = 根号5/5 * ((1+根号5)/2)^n   - 根号5/5 * ((1-根号5)/2)^n
	double b = pow( (1.0 - sqrt(5.0))/2.0,n);
	double dRes = f*(a - b);
	return (long long)dRes;
}

void process()
{
	int n;
	while(EOF != scanf("%d",&n))
	{
		printf("%lld\n",fibonacci_equation(n));
		printf("%lld\n",fibonacci_matrix(n));
	}
}

int main(int argc,char* argv[])
{
	process();
	getchar();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值