矩阵快速幂求斐波那契模板

本文介绍了一种使用矩阵快速幂算法高效求解斐波那契数的方法,适用于大整数运算,通过矩阵乘法和幂运算,可以在O(logn)的时间复杂度内求得斐波那契数列的第n项。

矩阵快速幂求斐波那契模板:

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<sstream>
#include<cstdlib>
#include<time.h>
#define inf 1000000000
#define mem(a,b) memset((a),b,sizeof(a))
typedef long long ll;
const int N=100010;
using namespace std;
const int MOD=998244353;
struct mat
{
    ll a[2][2];
};
mat mat_mul(mat x,mat y)
{
    mat 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]=(res.a[i][j]+x.a[i][k]*y.a[k][j])%MOD;
    return res;
}
int mat_pow(int n)
{
//返回斐波那契的第n项(%MOD)
//1 1 2 3 5
    mat c,res;
    c.a[0][0]=c.a[0][1]=c.a[1][0]=1;
    c.a[1][1]=0;
    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=mat_mul(res,c);
        c=mat_mul(c,c);
        n=n>>1;
    }
    return res.a[0][1];
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int ans=mat_pow(n);
        printf("%d\n",ans);
    }
    return 0;
}

一道例题,输入n,输出 f (2*n+3)-1 (n<1e9)

HDU 6198

ACcode:

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<sstream>
#include<cstdlib>
#include<time.h>
#define inf 1000000000
#define mem(a,b) memset((a),b,sizeof(a))
typedef long long ll;
const int N=100010;
using namespace std;
const int MOD=998244353;
struct mat
{
    ll a[2][2];
};
mat mat_mul(mat x,mat y)
{
    mat 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]=(res.a[i][j]+x.a[i][k]*y.a[k][j])%MOD;
    return res;
}
int mat_pow(int n)
{
    mat c,res;
    c.a[0][0]=c.a[0][1]=c.a[1][0]=1;
    c.a[1][1]=0;
    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=mat_mul(res,c);
        c=mat_mul(c,c);
        n=n>>1;
    }
    return res.a[0][1];
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int ans=mat_pow(2*n+3)-1;
        printf("%d\n",ans);
    }
    return 0;
}

 

### C++ 实现 Fibonacci 数列的矩阵快速幂算法 以下是一个基于矩阵快速幂方法实现的 C++ 示例代码,用于高效计算斐波那契数列的第 \( n \) 项。此方法的时间复杂度为 \( O(\log{n}) \),相较于传统的线性迭代方法更加高效。 ```cpp #include <iostream> #include <vector> using namespace std; const long long MOD = 1e9 + 7; // 定义矩阵结构体 struct Matrix { vector<vector<long long>> data; int rows, cols; Matrix(int r, int c) : rows(r), cols(c), data(vector<vector<long long>>(r, vector<long long>(c, 0))) {} }; // 矩阵乘法函数 Matrix multiply(const Matrix& a, const Matrix& b) { Matrix res(a.rows, b.cols); for (int i = 0; i < a.rows; ++i) { for (int j = 0; j < b.cols; ++j) { for (int k = 0; k < a.cols; ++k) { res.data[i][j] = (res.data[i][j] + a.data[i][k] * b.data[k][j]) % MOD; } } } return res; } // 矩阵快速幂函数 Matrix matrixPower(Matrix base, long long exp) { Matrix res(base.rows, base.cols); for (int i = 0; i < base.rows; ++i) res.data[i][i] = 1; // 初始化单位矩阵 while (exp > 0) { if (exp % 2 == 1) { res = multiply(res, base); } base = multiply(base, base); exp /= 2; } return res; } long long fibonacci(long long n) { if (n == 0) return 0; if (n == 1) return 1; Matrix T(2, 2); // 转移矩阵 [[1, 1], [1, 0]] T.data[0][0] = 1; T.data[0][1] = 1; T.data[1][0] = 1; T.data[1][1] = 0; Matrix F(2, 1); // 初始状态向量 [[1], [0]] F.data[0][0] = 1; F.data[1][0] = 0; T = matrixPower(T, n - 1); // 计算T^(n-1) F = multiply(T, F); return F.data[0][0]; // 返回结果即为F[n] } int main() { long long n; cin >> n; cout << "Fibonacci number at position " << n << " is: " << fibonacci(n) << endl; return 0; } ``` #### 解析 1. **矩阵定义** 使用 `Matrix` 结构体表示二维矩阵,并支持初始化和存储操作[^4]。 2. **矩阵乘法** 函数 `multiply` 实现两个矩阵之间的乘法运算,同时对结果取模以防止溢出[^3]。 3. **矩阵快速幂** 函数 `matrixPower` 借助分治思想实现了高效的矩阵幂次计算,其核心逻辑类似于整数快速幂算法[^2]。 4. **初始条件与转移矩阵** 斐波那契数列的状态可以通过如下关系表达: \[ \begin{bmatrix} f_{n+1} \\ f_n \end{bmatrix} = \begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}^n \cdot \begin{bmatrix} f_1 \\ f_0 \end{bmatrix} \] 5. **最终结果提取** 对于输入 \( n \geq 2 \),通过计算 \( T^{n-1} \times F \) 得到目标值 \( f_n \)[^5]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值