2020.7.10 --根据递推公式构造系数矩阵用于快速幂

现在水平不够,挖个坑…以后再看

非常nice的博客

https://blog.csdn.net/u012061345/article/details/52224623

7.13 upd:
用矩阵快速幂的题目,一般有两个难点

  • 寻找递推式
  • 根据递推式建立系数矩阵

这俩肯定有一个难搞的
一个是思维,一个要经验…

其他也没啥了
放个板子 结构体用的挺舒服的

#include "bits/stdc++.h"

using namespace std;
#define fi first
#define se second

#define pb push_back
#define es erase
#define in insert
#define ll long long
#define pii pair<ll, ll>
#define lb long double

#define ioss                     \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);
#define m_p(i, j) make_pair(i, j)
#define mem(a, x) memset(a, x, sizeof(a))
#define fcout(i)
#define endl "\n"
#define am ans.mat
const int N = 1e5 + 7;
const int INF = 0x3f3f3f3f;

struct Martix
{
    static const int mod = 1000000009;
    static const int N = 10;
    ll mat[N][N];
    int row, col; //一般为方阵
    Martix(int row, int col) : row(row), col(col)
    {
        memset(mat, 0, sizeof(mat));
    }
    Martix(int n) : row(n), col(n)
    { //返回一个n*n的单位矩阵
        memset(mat, 0, sizeof(mat));
        for (int i = 1; i <= n; i++)
            mat[i][i] = 1;
    }
    void output()
    {
        for (int i = 1; i <= row; i++)
        {
            for (int j = 1; j <= col; j++)
                cout << mat[i][j] << " ";
            cout << endl;
        }
    }
    Martix operator*(Martix t)
    {
        Martix ans(row, t.col);
        for (int i = 1; i <= row; i++)
            for (int j = 1; j <= t.col; j++)
                for (int k = 1; k <= col; k++)
                    ans.mat[i][j] = (ans.mat[i][j] + mat[i][k] * t.mat[k][j]) % mod;
        return ans;
    }
};

Martix mtx_pow(Martix n, ll m)
{
    Martix tmp(n.row);
    while (m > 0)
    {
        if (m & 1)
            tmp = tmp * n;
        m >>= 1;
        n = n * n;
    }
    return tmp;
}
int main()
{
    ll n;
    while (cin >> n)
    {
        Martix ans(5);
        mem(am, 0);
        am[1][1] = 2;
        am[1][2] = 0;
        am[1][3] = 1;
        am[1][4] = 2;
        am[1][5] = 1;
        am[2][1] = am[3][3] = am[3][5] = 1;
        am[4][4] = am[4][5] = am[5][5] = 1;
        am[3][4] = 2;
        ans = mtx_pow(ans, n - 1);
        //ans.output();
        ll res = am[1][1] + am[1][3] + am[1][4] + am[1][5];
        if (n)
            cout << res % 1000000009 << endl;
        else
            cout << 0 << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值