51nod1242 斐波那契数列的第N项

链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1242

题意:中文题。。

分析:矩阵快速幂加速求斐波那契裸题。

代码:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=2000010;
const int MAX=151;
const int MOD1=1000007;
const int MOD2=100000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000009;
const ll INF=10000000010;
typedef unsigned long long ull;
struct Martx {
    ll x[3][3];
};
Martx mulmar(Martx A,Martx B) {
    Martx ret;
    memset(ret.x,0,sizeof(ret.x));
    for (int i=1;i<3;i++)
        for (int j=1;j<3;j++)
            for (int k=1;k<3;k++)
            ret.x[i][j]=(ret.x[i][j]+A.x[i][k]*B.x[k][j])%MOD;
    return ret;
}
Martx q_pow(int n,Martx A) {
    Martx ret;
    ret.x[1][2]=ret.x[2][1]=0;
    ret.x[1][1]=ret.x[2][2]=1;
    while (n) {
        if (n&1) ret=mulmar(ret,A);
        A=mulmar(A,A);
        n>>=1;
    }
    return ret;
}
int main()
{
    ll n;
    Martx A,B;
    A.x[1][1]=1;A.x[1][2]=1;
    A.x[2][1]=1;A.x[2][2]=0;
    while (scanf("%lld", &n)!=EOF) {
        n%=MOD-1;
        if (n==0) { printf("0\n");return 0; }
        if (n==1||n==2) { printf("1\n");return 0; }
        n-=2;B=q_pow(n,A);
        printf("%lld\n", ((B.x[1][1]+B.x[1][2])%MOD+MOD)%MOD);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值