HDU5015-233 Matrix(矩阵快速幂)

题目链接


题意:给定一个矩阵的第0列的第1到n个数,第一行第1个数开始每个数分别为233, 2333........,求第n行的第m个数。

思路:将第一行的数全部右移一位,用前一列递推出下一列,构造矩阵,类似如下 
1 0 0 0 0 0 0 
1 10 0 0 0 0 0 
0 1 1 0 0 0 0 
0 1 1 1 0 0 0 
0 1 1 1 1 0 0 
0 1 1 1 1 1 0

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long ll;

const int N = 15;
const int MOD = 10000007;

struct mat{
    ll s[N][N];
    int l;
    mat(int o) {
        l = o;
        memset(s, 0, sizeof(s)); 
    }
    void init() {
        s[0][0] = s[1][0] = 1; 
        s[1][1] = 10;
        for (int i = 2; i < l; i++)
            for (int j = 1; j <= i; j++)
                s[i][j] = 1;
    }
    mat operator * (const mat& c) {
        mat ans(l); 
        for (int i = 0; i < l; i++) 
            for (int j = 0; j < l; j++)
                for (int k = 0; k < l; k++)
                    ans.s[i][j] = (ans.s[i][j] + s[i][k] * c.s[k][j]) % MOD;
        return ans;
    }
};

int n, m;

mat Pow(mat c, int k) {
    mat ans = c;
    k--;
    while (k) {
        if (k & 1) 
            ans = ans * c;
        c = c * c;
        k >>= 1;
    }
    return ans;
}

int main() {
    while (scanf("%d%d", &n, &m) != EOF) {
        mat a(n + 2);  
        a.s[0][0] = 3;
        a.s[1][0] = 233;
        for (int i = 2; i <= n + 1; i++)
            scanf("%I64d", &a.s[i][0]);
        mat tmp(n + 2);
        tmp.init();
        mat ans = Pow(tmp, m);
        ans = ans * a;
        printf("%I64d\n", ans.s[n + 1][0]);
    }    
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值