poj 3233 Matrix Power Series(矩阵二分,快速幂)

Matrix Power Series
Time Limit: 3000MS Memory Limit: 131072K
Total Submissions: 15739 Accepted: 6724

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
2 3


当k为奇数时

Sk = A + A2 + A3 + … + Ak  

    =(1+Ak/2)*(A + A2 + A3 + … + Ak/2  )+{Ak}

    =(1+Ak/2)*(Sk/2 )+{Ak}

当k为偶数时

Sk = A + A2 + A3 + … + Ak  

    =(1+Ak/2)*(A + A2 + A3 + … + Ak/2  )+{Ak}

    =(1+Ak/2)*(Sk/2 )

就可以二分递归求Sk

代码:

//829ms
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int n,m;
struct matrix
{
    int ma[50][50];
} a;
matrix multi(matrix x,matrix y)//矩阵相乘
{
    matrix ans;
    memset(ans.ma,0,sizeof(ans.ma));
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            if(x.ma[i][j])//稀疏矩阵优化
                for(int k=1; k<=n; k++)
                {
                    ans.ma[i][k]=(ans.ma[i][k]+x.ma[i][j]*y.ma[j][k])%m;
                }
        }
    }
    return ans;
}
matrix add(matrix x,matrix y)//矩阵相加
{
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            x.ma[i][j]=(x.ma[i][j]+y.ma[i][j])%m;
    return x;
}
matrix pow(matrix a,int m)
{
    matrix ans;
    for(int i=1; i<=n; i++) //单位矩阵
    {
        for(int j=1; j<=n; j++)
        {
            if(i==j)
                ans.ma[i][j]=1;
            else
                ans.ma[i][j]=0;
        }
    }
    while(m)//矩阵快速幂
    {
        if(m&1)
        {
            ans=multi(ans,a);
        }
        a=multi(a,a);
        m=(m>>1);
    }
    return ans;
}
matrix solve(matrix x,int k)//递归求Sk
{
    if(k==1)
        return x;
    matrix ans;
    for(int i=1; i<=n; i++) //单位矩阵
    {
        for(int j=1; j<=n; j++)
        {
            if(i==j)
                ans.ma[i][j]=1;
            else
                ans.ma[i][j]=0;
        }
    }
    ans=add(ans,pow(x,k/2));
    ans=multi(ans,solve(x,k/2));
    if(k&1)
        ans=add(ans,pow(x,k));
    return ans;
}
int main()
{
    int k;
    while(~scanf("%d%d%d",&n,&k,&m))
    {
        matrix ans,a;
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
                scanf("%d",&a.ma[i][j]);
        }
        ans=solve(a,k);
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<n; j++)
                printf("%d ",ans.ma[i][j]);
            printf("%d\n",ans.ma[i][n]);
        }
    }
    return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值