斐波那契数列的一些应用

斐波拉契数列的一些推理:

1.F(0)+F(1)+…+F(n)=F(n+2)-1

2.F(1)+F(3)+…F(2n-1)=F(2n)

3.F(2)+F(4)+…+F(2n)=F(2n+1)-1

4.[F(0)]^2  +[F(1)]^2  +…+[F(n)]^2=F(n)·F(n+1)

5.F(0)-F(1)+F(2)-…+(-1)^n·F(n)   = (-1)^n.[F(n+1)-F(n)]+1

6.F(m+n)=F(m-1)·F(n)+F(m)·F(n+1)

7.[F(n)]^2  =(-1)^(n-1)+F(n-1)·F(n+1)

8.F(2n-1)=[F(n)]^2  -[F(n-2)]^2。

9.F(n)=F(n+2)+F(n-2)

10.F(2n-2m-2)[F(2n)+F(2n+2)]=F(2m+2)+F(4n-2m) [ n>m≥-1,且n≥1]

11.F(2n+1)=[F(n)]^2  +[F(n+1)]^2

12.F(n)=C(n-1,0)+C(n-2,1)+…+C(n-1-m,m) (m<=n-1-m)

13.求斐波拉契数列的每一项
求斐波拉契数列的每一项
14.斐波拉契数列数列涉及的黄金分割线0.618…,等于斐波那契数列前两项的比
https://blog.csdn.net/weixin_43187268/article/details/103230695
15.对于斐波拉契数列n的值很大时,求F[n]。采用矩阵快速幂和快速乘的方法。
题目:朱朱的斐波那契数列https://blog.csdn.net/weixin_43187268/article/details/103073389
16.[蓝桥杯][2014年第五届真题]斐波那契
题目的地址:https://www.dotcpp.com/oj/problem1444.html

这个代码只适用m值比较小,不会爆usigned long long 的,但题目的测试点就是没有爆。做这题真的好坑好坑,难搞哦

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
typedef struct { //定义数组结构体 
	ll a[2][2];
}matrix;
ll add(ll a, ll b, ll mod){ //快速乘,防止越界ll 
	if(a < b) swap(a,b);
 	ll sum = 0;
	while(b){
		if(b & 1) sum = (sum+a) % mod;
		a =(2 * a) % mod;
		b /= 2;
	}
	return sum;
}
matrix mul(matrix b, matrix c, ll mod){ //矩阵乘法 
	matrix res;
	memset(res.a,0,sizeof(res.a));
	for(int i = 0; i < 2; i++){
		for(int j = 0; j < 2; j++){
			for(int k = 0; k < 2; k++){
				res.a[i][j] += add(b.a[i][k],c.a[k][j],mod);
				res.a[i][j] %= mod;
			}
		}
	}
	return res;
}
matrix mul(matrix b, matrix c){ //矩阵乘法 
	matrix res;
	memset(res.a,0,sizeof(res.a));
	for(int i = 0; i < 2; i++){
		for(int j = 0; j < 2; j++){
			for(int k = 0; k < 2; k++){
				res.a[i][j] += b.a[i][k]*c.a[k][j];
			}
		}
	}
	return res;
}
matrix kmi(matrix b, int n){ //矩阵的快速幂 
	matrix res;
	memset(res.a,0,sizeof(res.a));
	for(int i = 0; i < 2; i++){
		res.a[i][i] = 1;
	}
	while(n){
		if(n & 1) res = mul(res,b);
		b = mul(b,b);
		n /= 2;
	}
	return res;
}
matrix kmi(matrix b, ll n, ll mod){ //矩阵的快速幂 
	matrix res;
	memset(res.a,0,sizeof(res.a));
	for(int i = 0; i < 2; i++){
		res.a[i][i] = 1;
	}
	while(n){
		if(n & 1) res = mul(res,b,mod);
		b = mul(b,b,mod);
		n /= 2;
	}
	return res;
}
ll getF(ll n){ //得到f[n]; 
	matrix b, c;
	memset(b.a,0,sizeof(b.a));
	memset(c.a,0,sizeof(c.a));
	b.a[0][0] = 1;
	b.a[1][1] = 1;
	c.a[1][0] = c.a[1][1] = c.a[0][1] = 1;
	c = kmi(c,n);
	b = mul(b,c);
	return b.a[0][1];
}
ll getF(ll n, ll mod){ //得到f[n]; 
	matrix b, c;
	memset(b.a,0,sizeof(b.a));
	memset(c.a,0,sizeof(c.a));
	b.a[0][0] = 1;
	b.a[1][1] = 1;
	c.a[1][0] = c.a[1][1] = c.a[0][1] = 1;
	c = kmi(c,n,mod);
	b = mul(b,c,mod);
	return b.a[0][1];
}
int main (){
  ll n, m, k;
 	cin >> n >> m >> k;
 	ll sum = 0;
 	if(m >= n+2){
 		sum = getF(n+2,k)-1;
	}else{
		ll t = getF(m);
		sum = getF(n+2,t)%k-1;
	}
	cout << sum << endl;
} 

这题就没看见有几个博客写对了,好多都只是只能过67%的测试点,下面几篇博客有方法,但还是有问题,本人弄了好久,搞不出来。在此附上一篇java题解,本人弄不清楚怎么来的,谁弄出来了教教我
https://blog.csdn.net/lixiaomu2/article/details/60333020
借助下面的博客来完成的
https://blog.csdn.net/qq_37919863/article/details/78905198

https://blog.csdn.net/acdreamers/article/details/21822165

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值