矩阵快速幂

//
// Created by Sieak Softly on 2019-12-16.
//

#include "cstdio"
#include "cstdlib"
#include "iostream"
#include "sstream"
#include "cstring"
#include "cmath"
#include "algorithm"
#include "queue"
#include "map"
#include "vector"
#include "stack"
#include "set"
#include "climits"

using namespace std;

typedef long long ll;
typedef unsigned long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = 110;
const int INF = INT_MAX;
const int mod = 1e9 + 7;

inline int read() {
    char c = getchar();
    int x = 0, f = 1;
    while (c < '0' || c > '9') {
        if (c == '-') f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * f;
}

int n;

struct mat {
    ll m[maxn][maxn];
} unit;

mat operator * (mat a, mat b) {
    mat res;
    ll x;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            x = 0;
            for (int k = 0; k < n; ++k) x = (x + a.m[i][k] * b.m[k][j]) % mod;
            res.m[i][j] = x;
        }
    }
    return res;
}

ostream & operator << (ostream & os, const mat a) {
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j) os << a.m[i][j] << (j < n - 1 ? " " : "\n");
    return os;
}

istream & operator >> (istream & is, mat & a) {
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j) is >> a.m[i][j];
    return is;
}


void init_unit() {
    for (int i = 0; i < n; ++i) unit.m[i][i] = 1;
}

mat mat_quick_pow(mat a, ll k) {
    mat res = unit;
    while (k) {
        if (k & 1) res = res * a;
        a = a * a;
        k >>= 1;
    }
    return res;
}

int main() {
    ll k;
    scanf("%d%lld", &n, &k);
    init_unit();
    mat a;
    cin >> a;
    cout << mat_quick_pow(a, k);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值