NYOJ299 Matrix Power Series

Matrix Power Series
时间限制:1000 ms  |  内存限制:65535 KB
难度:4
描述
Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + … + Ak.
输入
The input contains exactly one test case. The first line of input contains three positive integers n (n ≤ 30), k (k ≤ 10^9) and m (m < 10^4). Then follow n lines each containing n nonnegative integers below 32,768, giving A’s elements in row-major order.
输出
Output the elements of S modulo m in the same way as A is given.
样例输入
2 2 4
0 1
1 1
样例输出
1 2
2 3
来源
POJ Monthly
上传者
张云聪

这道题直接构造矩阵,然后矩阵快速幂。

 S = A + A2 + A3 + … + Ak.
 | A O | * | A | = | Ak+1 |
 | I I |   | O |   |  Sk  |    A代表输入矩阵,O代表0矩阵,I代表单位矩阵

构造矩阵的矩阵其实就是构造一个大的矩阵, 这里的N<<=1是因为构造的矩阵是2*2
的矩阵,2*2矩阵中的元素也是一个n阶的矩阵,这样我们就把他们构造成一个n*2阶矩阵。
例如输入矩阵是:
0 1
1 1
——> 化为:
0 1 0 0
1 1 0 0
1 0 1 0
0 1 0 1

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<ctime>
#include<string>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#include<set>
#include<map>
#include<cstdio>
#include<limits.h>
#define fir first
#define sec second
#define fin freopen("/home/ostreambaba/文档/input.txt", "r", stdin)
#define fout freopen("/home/ostreambaba/文档/output.txt", "w", stdout)
#define mes(x, m) memset(x, m, sizeof(x))
#define pii pair<int, int>
#define Pll pair<ll, ll>
#define INF 1e9+7
#define Pi 4.0*atan(1.0)
#define MOD 1000000007

#define lowbit(x) (x&(-x))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ls rt<<1
#define rs rt<<1|1

#define scini(n) scanf("%d", &n)
#define scinl(n) scanf("%lld", &n)
#define scinii(n, m) scanf("%d%d", &n, &m)
#define scinll(n, m) scanf("%lld%lld", &n, &m)
#define scouti(n) printf("%d\n", n);
#define scoutii(n, m) printf("%d\n%d\n", n, m);
#define IamBigSB main

typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-12;
const int maxn = 65;
using namespace std;
//#define TIME

inline int read(){
    int x(0),f(1);
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int N, mod;
struct matrix{
    int mat[maxn][maxn];
    void init(){
        mes(mat, 0);
        for(int i = 0; i < N; ++i){
            mat[i][i] = 1;
        }
    }
    void clear(){
        mes(mat, 0);
    }
    void output(){
        for(int i = 0; i < N; ++i){
            for(int j = 0; j < N; ++j){
                printf("%d ", mat[i][j]);
            }
            printf("\n");
        }
    }
    matrix operator *(const matrix &base){
        matrix tmp;
        tmp.clear();
        for(int i = 0; i < N; ++i){
            for(int j = 0; j < N; ++j){
                for(int k = 0; k < N; ++k){
                    tmp.mat[i][j] = (tmp.mat[i][j] + mat[i][k]*base.mat[k][j])%mod;
                }
            }
        }
        return tmp;
    }
};
matrix matrix_fast_mod(int &m, matrix &base)
{
    matrix res;
  //  res.output();
    res.init();
    while(m){
        if(m&1){
            res = res*base;
        }
        base = base*base;
        m >>= 1;
    }
    return res;
}

void work(int &m)
{
    matrix base;
    matrix res;
    res.clear();
    base.clear();
    for(int i = 0; i < N; ++i){
        for(int j = 0; j < N; ++j){
            base.mat[i][j] = read();
            if(i == j){
            base.mat[i+N][j] = 1;
            base.mat[i+N][j+N] = 1;
            }
            res.mat[i][j] = base.mat[i][j];
        }
    }
    N <<= 1;
  //res.output();
  //base.output();
    base = matrix_fast_mod(m, base);
    base = base*res;
    for(int i = N/2; i < N; ++i){
        for(int j = 0; j < N/2; ++j){
            printf("%d ", base.mat[i][j]);
        }
        printf("\n");
    }
  //  base.output();
}

int main()
{
    int m;
    N = read();
    m = read();
    mod = read();
    work(m);
    return 0;
#ifdef TEST
    clock_t start, end;
    start = clock();
    end = clock();
    double _time = double(end-start)/CLOCKS_PER_SEC*1000;
    cout << _time << endl;
#endif

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值