首页 > 其他分享 >HDU-5015

HDU-5015

时间:2022-11-18 09:45:23浏览次数:57  
标签:HDU 5015 int cin ++ mat

HDU-5015

思路

\(n \leq 10\)是一个较为明显的提示,容易想到一列一列的转移,通过对\(a_{n,m}\)的寻找,发现\(a_{n,m} = \sum_1^{n} a_{i,m-1} + a_{0,m}\),其中\(\sum_1^{n} a_{i,m-1}\)全都是前一列的元素,\(a_{0,m}\)是可以推导出的常数,因此,使用矩阵快速幂就可以求出答案。

矩阵推导

image

Code

#include <bits/stdc++.h>
using namespace std;
#define _u_u_ ios::sync_with_stdio(false), cin.tie(nullptr)
#define cf int _o_o_;cin>>_o_o_;for (int Case = 1; Case <= _o_o_;Case++)
#define SZ(x) (int)(x.size())
inline void _A_A_();
signed main() {_A_A_();return 0;}

using ll = long long;
#define int long long
const int mod = 10000007;
const int maxn = 2e5 + 10;
const int N = 210, M = 5010;
const int inf = 0x3f3f3f3f;
int n, m;

struct mat {
    vector<vector<int>> m;
    mat (){}
    mat(int n) {
        m.resize(n + 2, vector<int> (n + 2, 0));
    }
};

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

mat qpow(mat a, int b) {
    mat c(n);
    for (int i = 0;i < n+2;i++) c.m[i][i] = 1;
    while (b) {
        if (b & 1) {
            c = c * a;
        }
        a = a * a;
        b >>= 1;
    }
    return c;
}

inline void _A_A_() {
    #ifdef LOCAL
    freopen("in.in", "r", stdin);
    #endif
    _u_u_;
    while (cin >> n >> m) {
        vector<int> v(n + 2);
        for (int i = n - 1;i >= 0;i -- ) cin >> v[i];
        v[n] = 233, v[n + 1] = 1;
        mat s(n);
        for (int i = 0;i < n + 1;i++) {
            for (int j = 0;j <= i;j++) {
                s.m[i][j] = 1;
            }
        }
        s.m[n + 1][n + 1] = 1;
        s.m[n][n] = 10;
        s.m[n + 1][n] = 3;
        s = qpow(s, m);
        int ans = 0;
        for (int i = 0;i < n + 2;i++) {
            ans = (ans + v[i] * s.m[i][0]) % mod;
        }
        cout << ans << "\n";
    }
}

标签:HDU,5015,int,cin,++,mat
From: https://www.cnblogs.com/FanWQ/p/16902170.html

相关文章