E - Matrix Power Series (矩阵数列)

Posted alingmaomao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了E - Matrix Power Series (矩阵数列)相关的知识,希望对你有一定的参考价值。

然后,怎么来求这个前k项的和,我把式子推一下

技术分享图片

当k为奇数的时候直接SK-1+AK  就又化为偶数的情况了。代码如下:

#include<iostream>
#include<cstring>
using namespace std;
#define ll int
ll n, mod, k;
struct jz
{
    ll num[35][35];
    jz(){ memset(num, 0, sizeof(num)); }
    jz operator*(const jz&p)const
    {
        jz ans;
        for (int k = 0; k < n; ++k){
            for (int i = 0; i < n; ++i){
                if (num[i][k] == 0)continue;
                for (int j = 0; j < n; ++j)
                {
                    if (p.num[k][j] == 0)continue;
                    ans.num[i][j] = (ans.num[i][j] + num[i][k] * p.num[k][j] % mod) % mod;
                }
            }
        }
        return ans;
    }
    jz operator+(const jz&p)const
    {
        jz ans;
        for (int i = 0; i < n;++i)
        for (int j = 0; j < n; ++j)
            ans.num[i][j] = (num[i][j] + p.num[i][j]) % mod;
        return ans;
    }
}mat, E;
jz pow(jz x, ll m)
{
    jz ans;
    for (int i = 0; i < n; ++i)ans.num[i][i] = 1;
    for (; m; m >>= 1, x = x*x)
    if (m & 1)ans = ans*x;
    return ans;
}
jz sum(ll h)
{
    if (h == 1)return mat;
    else if (h & 1) return sum(h - 1) + pow(mat, h);
    else return (pow(mat, h / 2) + E)*sum(h / 2);
}
int main()
{
    ios::sync_with_stdio(false); cin.tie(0);
    cin >> n >> k >> mod;
    for (int i = 0; i < n; ++i)E.num[i][i] = 1;

    for (int i = 0; i < n;++i)
    for (int j = 0; j < n; ++j)
        cin >> mat.num[i][j];
    jz ans = sum(k);
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < n; ++j)
            cout << ans.num[i][j] << " ";
        cout << endl;
    }
}

 

以上是关于E - Matrix Power Series (矩阵数列)的主要内容,如果未能解决你的问题,请参考以下文章

Matrix Power Series

poj3233 Matrix Power Series

POJ 3233 Matrix Power Series

POJ3233 Matrix Power Series

poj 3233 Matrix Power Series

POJ3233 Matrix Power Series