zoukankan      html  css  js  c++  java
  • poj 3233 Matrix Power Series

    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('
    ');
        }
    }
    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);
    }
  • 相关阅读:
    邮件收件与草稿箱sql
    [转]全局变量与全局静态变量的区别:
    工作:8月份6-14号的工作初级经验(零碎) 关于传值id经常犯的错误
    网络婚礼之AFNetWorking3.0
    网络热恋之NSURLSession
    网络热恋之json解析
    网络热恋之SDWebImage
    网络热恋之XML解析
    网络&热恋NSURLConnection代理及GET¥POST请求
    网络邂逅&网络异步请求
  • 原文地址:https://www.cnblogs.com/8023spz/p/9433002.html
Copyright © 2011-2022 走看看