51Nod-斐波那契数列的第N项(矩阵快速幂)


基准时间限制:1  秒 空间限制:131072  KB 分值:  0   难度:基础题
 收藏
 关注
斐波那契数列的定义如下:

F(0) = 0
F(1) = 1
F(n) = F(n - 1) + F(n - 2) (n >= 2)

(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, ...)
给出n,求F(n),由于结果很大,输出F(n) % 1000000009的结果即可。
Input
输入1个数n(1 <= n <= 10^18)。
Output
输出F(n) % 1000000009的结果。
Input示例
11
Output示例
89
李陶冶   (题目提供者)
大神链接:https://zhuanlan.zhihu.com/p/19768646
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<limits.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<math.h>
#include<map>
using namespace std;
#define Mod 1000000009
struct node
{
	__int64 c[2][2];
};
node a;
node work(node x,node y)
{
	node t={0};
	int i,j,k;
	for(i=0;i<2;i++)
	 for(j=0;j<2;j++)
	   for(k=0;k<2;k++)
	   {
	     t.c[i][j]+=(x.c[i][k]*y.c[k][j])%Mod;
	     t.c[i][j]%=Mod;
	   }
	return t;
}
node Pow(node x,__int64 n)
{
	node temp=x;
	if(n<0) return temp;
	while(n)
	{
		if(n&1)
		{
			temp=work(temp,x);
			n--;
		}
		x=work(x,x);
		n/=2;
	}
	return temp;
}
int  main()
{
	__int64 n;
	scanf("%I64d",&n);
	a.c[0][0]=1;
	a.c[0][1]=1;
	a.c[1][0]=1;
	a.c[1][1]=0;
	node ans=Pow(a,n-2);
	printf("%I64d\n",ans.c[0][0]);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值