POJ - 3233 Matrix Power Series

本文介绍了一种解决矩阵幂级数求和问题的方法。给定一个 n×n 的矩阵 A 和一个正整数 k,文章提供了一个 C++ 程序来计算从 A 到 Ak 的所有矩阵幂的和,并取模 m。程序使用了快速幂算法来提高效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Matrix Power Series
Time Limit: 3000MS Memory Limit: 131072K
Total Submissions: 24975 Accepted: 10346

Description

Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + … + Ak.

Input

The input contains exactly one test case. The first line of input contains three positive integers n (n ≤ 30), k (k ≤ 109) and m (m < 104). Then follow n lines each containing n nonnegative integers below 32,768, giving A’s elements in row-major order.

Output

Output the elements of S modulo m in the same way as A is given.

Sample Input

2 2 4
0 1
1 1

Sample Output

1 2


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#define max_ 200010
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;

struct mat
{
    ll num[70][70];
    int n;
}a;
int m,n,k;
mat mul(struct mat a,struct mat b)
{
    struct mat ans;
    ans.n=a.n;
    memset(ans.num,0,sizeof(ans.num));
    for(int i=1;i<=a.n;i++)
    {
        for(int j=1;j<=a.n;j++)
        {
            for(int k=1;k<=a.n;k++)
            {
                ans.num[i][j]+=(a.num[i][k]*b.num[k][j])%m;
                ans.num[i][j]%=m;
            }
        }
    }
    return ans;
}
void show(struct mat a)
{
    printf("%d\n",a.n);
    for(int i=1;i<=a.n;i++)
    {
        for(int j=1;j<=a.n;j++)
        {
            printf("%d ",a.num[i][j]);
        }
        printf("\n" );
    }
}
mat fpow(struct mat a,int k)
{
    struct mat ans,tmp=a;
    ans.n=a.n;
    for(int i=1;i<=a.n;i++)
    {
        for(int j=1;j<=a.n;j++)
        {
            if(i==j)
            ans.num[i][j]=1;
            else
            ans.num[i][j]=0;
        }
    }
    while(k!=0)
    {
        if(k&1)
            ans=mul(ans,tmp);
        tmp=mul(tmp,tmp);
        k/=2;
    }
    return ans;
}
int main(int argc, char const *argv[]) {
    cin>>n>>k>>m;
    a.n=n<<1;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            cin>>a.num[i][j];
            if(i==j)
            {
                a.num[i+n][j+n]=1;
                a.num[i][j+n]=1;
            }
        }
    }
    mat ans=fpow(a,k+1);
    for(int i=1;i<=n;i++)
    {
        for(int j=1+n;j<=n*2;j++)
        {
            if(i+n==j)
            ans.num[i][j]--;
            if(ans.num[i][j]<0)
            ans.num[i][j]+=m;
            printf("%d",ans.num[i][j]);
            if(j!=n<<1)
            printf(" ");
        }
        printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值