POJ3233

来源:互联网 发布:权重轮询调度算法 编辑:程序博客网 时间:2024/06/05 14:10

1.题目描述:

Matrix Power Series
Time Limit: 3000MS Memory Limit: 131072KTotal Submissions: 22069 Accepted: 9240

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 40 11 1

Sample Output

1 22 3

Source

POJ Monthly--2007.06.03, Huang, Jinsong

[Submit]   [Go Back]   [Status]   [Discuss]

2.题意概述:

题意是给定n*n矩阵A和整数k,要求一个矩阵S=A+A^2+...+A^k,输出S的每一个元素

3.解题思路:

很显然S[i]应该有这样的关系:

 

S[i+1]=S[i]+A^(i+1)

 

而对于A^(i+1),又有

 

A^(i+1)=A^(i) * A

 

如果构造这样的矩阵,其中矩阵每一个元素也都是矩阵:

 

S[i] A^(i+1)   A

0     0     0                                                (其中0指的是零矩阵)

0     0     0

 

注意到这样的矩阵可以乘上一个矩阵而实现进行一步变换的目的,即

 

S[i] A^(i+1)  A                                I   0    0                                      S[i+1]       A^(i+2)       A

0     0    0               x                I    A   0                    =               0         0      0

0     0    0                                 0       0    I                                      0         0      0

(其中I指的是单位矩阵)

很显然这样由S[1]构造出一开始的矩阵然后连乘k-1次可以得到带有S[k]的矩阵。

 

然后因为中间乘上的k-1个矩阵完全相同,因此可以用快速幂加速。

4.AC代码:

#include <cstdio>#include <iostream>#include <cstring>#include <string>#include <algorithm>#include <cmath>#include <vector>#include <queue>#include <map>#include <set>#include <ctime>using namespace std;typedef long long ll;#define INF 0x3f3f3f3f#define N 30#define pi acos(-1.0)int MOD, n;struct Matrix {int mat[N][N];Matrix operator*(const Matrix& m)const {Matrix tmp;for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) {tmp.mat[i][j] = 0;for (int k = 0; k < n; k++)tmp.mat[i][j] += mat[i][k] * m.mat[k][j] % MOD;tmp.mat[i][j] %= MOD;}return tmp;}Matrix operator+(const Matrix& m)const {Matrix tmp;for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)tmp.mat[i][j] = (mat[i][j] + m.mat[i][j]) % MOD;return tmp;}};Matrix Pow(Matrix m, int t){Matrix ans;memset(ans.mat, 0, sizeof(ans.mat));for (int i = 0; i < n; i++)ans.mat[i][i] = 1;while (t){if (t & 1)ans = ans*m;t >>= 1;m = m*m;}return ans;}Matrix solve(Matrix m, int t) {Matrix A;memset(A.mat, 0, sizeof(A.mat));for (int i = 0; i < n; i++)A.mat[i][i] = 1;if (t == 1)return m;if (t & 1)return (Pow(m, t >> 1) + A)*solve(m, t >> 1) + Pow(m, t);elsereturn (Pow(m, t >> 1) + A)*solve(m, t >> 1);}int main(){/*#ifndef ONLINE_JUDGEfreopen("in.txt","r",stdin);freopen("out.txt","w",stdout);#endif*/int k;Matrix m, ans;while (scanf("%d%d%d", &n, &k, &MOD) != EOF){for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)scanf("%d", &m.mat[i][j]);ans = solve(m, k);for (int i = 0; i < n; i++){for (int j = 0; j < n; j++)if (j == 0)printf("%d", ans.mat[i][j]%MOD);elseprintf(" %d", ans.mat[i][j]%MOD);puts("");}}return 0;}

0 0
原创粉丝点击