HDU-6470 矩阵快速幂

#include <bits/stdc++.h>
using namespace std;

#define MOD 123456789
#define N 6
typedef long long LL;
typedef pair<int, int> Author;
vector<pair<string, int> > VP; 

struct Matrix{
    LL a[N][N];
    Matrix(){memset(a, 0, sizeof(a));}
    Matrix operator * (const Matrix& y){
        Matrix ans;
        //矩阵相乘 
        for(int i = 0; i < N; i++)for(int j = 0; j < N; j++)for(int k = 0; k < N; k++)ans.a[i][j] = (ans.a[i][j] + a[i][k] * y.a[k][j]) % MOD;
        return ans;
    }
    void operator = (const Matrix b){for(int i = 0; i < N; i++)for(int j = 0; j < N; j++)a[i][j] = b.a[i][j];}
};

LL Solve(Matrix ans, LL x){
    Matrix trs;
    //只是为了初始化,结构体内数组无法直接初始化 
    for(int i = 0; i < N; i++) trs.a[i][i] = 1;    //构造矩阵 
    while(x){
        if(x & 1) trs = trs * ans;
        x >>= 1;
        ans = ans * ans;
    }
    return (trs.a[0][0]*2 + trs.a[0][1] + trs.a[0][2] * 27 + trs.a[0][3] * 9 + trs.a[0][4] * 3 + trs.a[0][5]) % MOD;
}

int main(void){
    ios::sync_with_stdio(false); cin.tie(0);
    LL T, LL, n, ans; Matrix base;
    long long  mm[6][6] = {
        {1, 2, 1, 0, 0, 0},
        {1, 0, 0, 0, 0, 0},
        {0, 0, 1, 3, 3, 1},
        {0, 0, 0, 1, 2, 1}, 
        {0, 0, 0, 0, 1, 1},
        {0, 0, 0, 0, 0, 1}
    };
    cin>>T;
    for(int i = 0;i < N; i++)for(int j = 0;j < N; j++) base.a[i][j] = mm[i][j];

    while(T--){
        cin>>n;
        cout<<Solve(base, n - 2)<<endl;    //求第F[n]
    }
    return EXIT_SUCCESS;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值