0
点赞
收藏
分享

微信扫一扫

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

猎书客er 2023-04-21 阅读 33


Matrix Power Series


Time Limit: 3000MS

 

Memory Limit: 131072K

Total Submissions: 16512

 

Accepted: 7035


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


Source

POJ Monthly--2007.06.03, Huang, Jinsong



这个求的是1~k次幂的A矩阵的相加即s矩阵等于S = A + A2 + A3 + … + Ak.,需要用到递归


  • Source Code

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

using namespace std;

struct node
{
    int mp[32][32];
}init,res;

int n,mod,kk;

struct node add(struct node a,struct node b)
{
    struct node c;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            c.mp[i][j] = 0;
            c.mp[i][j] = a.mp[i][j] + b.mp[i][j];
            c.mp[i][j] = c.mp[i][j]%mod;
        }
    }
    return c;
};

struct node mult(struct node a,struct node b)
{
    struct node c;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            c.mp[i][j] = 0;
            for(int k=0;k<n;k++)
            {
                c.mp[i][j] += a.mp[i][k] * b.mp[k][j];
                c.mp[i][j] %= mod;
            }
        }
    }
    return c;
};

struct node Cal(int k)
{
    struct node tmp = res,x = init;
    while(k)
    {
        if(k%2 == 1)
        {
            tmp = mult(tmp,x);
        }
        x = mult(x,x);
        k>>=1;
    }
    return tmp;
};

struct node sum(int k)
{
    if(k == 1)
    {
        return init;
    }
    struct node tmp,tnow;
    tmp = sum(k/2);
    if(k%2 == 1)
    {
        tnow = Cal(k/2+1);
        tmp = add(tmp,mult(tmp,tnow));
        tmp = add(tnow,tmp);
    }
    else
    {
        tnow = Cal(k/2);
        tmp = add(tmp,mult(tmp,tnow));
    }
    return tmp;
};

int main()
{
    int k;
    scanf("%d%d%d",&n,&kk,&mod);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                scanf("%d",&init.mp[i][j]);
                init.mp[i][j]%=mod;
                if(i == j)
                {
                    res.mp[i][j] = 1;
                }
                else
                {
                    res.mp[i][j] = 0;
                }
            }
        }
        struct node tt;
        tt = sum(kk);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(j == 0)
                {
                    printf("%d",tt.mp[i][j]);
                }
                else
                {
                    printf(" %d",tt.mp[i][j]);
                }
            }
            printf("\n");
        }
    return 0;
}


举报

相关推荐

0 条评论