码迷,mamicode.com
首页 > 其他好文 > 详细

poj 3233 Matrix Power Series

时间:2018-08-06 21:47:39      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:sam   图片   element   lib   algo   img   desc   putchar   pac   

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 nnonnegative 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
 
令Sk = I + A + ... + A^k-1(I为单位矩阵)
技术分享图片

求出Sk+1再减去I即可。

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int n,k,m;
void mul(int (*a)[60],int (*b)[60]) {
    int d[60][60] = {0};
    for(int i = 0;i < n * 2;i ++) {
        for(int j = 0;j < n * 2;j ++) {
            for(int k = 0;k < n * 2;k ++) {
                d[i][j] = (d[i][j] + a[i][k] * b[k][j]) % m;
            }
        }
    }
    for(int i = 0;i < n * 2;i ++) {
        for(int j = 0;j < n * 2;j ++) {
            a[i][j] = d[i][j];
        }
    }
}
void getans(int (*a)[60]) {
    int d[60][60];
    for(int i = 0;i < n * 2;i ++) {
        for(int j = 0;j < n * 2;j ++) {
            d[i][j] = a[i][j];
        }
    }
    while(k) {
        if(k % 2)mul(d,a);
        k /= 2;
        mul(a,a);
    }
    for(int i = n;i < n * 2;i ++) {
        for(int j = 0;j < n;j ++) {
            if(j)putchar( );
            printf("%d",i == j + n ? (d[i][j] + m - 1) % m : d[i][j]);
        }
        putchar(\n);
    }
}
int main() {
    int a[60][60] = {0};
    scanf("%d%d%d",&n,&k,&m);
    for(int i = 0;i < n;i ++) {
        for(int j = 0;j < n;j ++) {
            scanf("%d",&a[i][j]);
        }
    }
    for(int i = n;i < n * 2;i ++) {
        a[i][i - n] = a[i][i] = 1;
    }
    getans(a);
}

 

poj 3233 Matrix Power Series

标签:sam   图片   element   lib   algo   img   desc   putchar   pac   

原文地址:https://www.cnblogs.com/8023spz/p/9433002.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!