[原创]POJ 3233 Matrix Power Series [矩阵快速幂]【数论】[水]

2016-09-13 17:00:13 Tabris_ 阅读数:278


博客爬取于2020-06-14 22:43:20
以下为正文

版权声明:本文为Tabris原创文章,未经博主允许不得私自转载。
https://blog.csdn.net/qq_33184171/article/details/52527503


题目链接 : http://poj.org/problem?id=3233

-----------------------------------------.
Matrix Power Series
Time Limit: 3000MS Memory Limit: 131072K
Total Submissions: 20930 Accepted: 8760
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
------------------------------------------.

题目大意:
不用解释了吧 就是求Sn
Sn = A + A^2 + A^3 + … + A^k.

解题思路:
这种题目一定想到矩阵快速幂
然后就是怎么构造矩阵了

[A O] 乘 [A E] 等 [A^2 S1]
[O O] 号 [O E]号 [O S0]

矩阵大致就是这么构造出来的

然后注意的事E只有主对角线是1 剩下的都是0 (Sb的我全写成E然后WA的都怀疑人生了)

附本题代码
-----------------------------------.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
# include <vector>
# include <queue>
# include <set>
# include <map>
# include <string>
# include <math.h>
# include <stdlib.h>
# include <time.h>
using namespace std;
using namespace std;
typedef long long int LL ;
# define INF 0x3f3f3f3f
# define pb push_back

# define lalal puts("*******");
/*************************************/

int MOD;
const int M = 32*2;

struct Matrix
{
LL m[M][M];
void display(int N)
{
for(int i=0; i<N; i++)
{
for(int j=0; j<N; j++)
{
if(j) printf(" ");
printf("%I64d",m[i][j]);
}
puts("");
}
}
void clearI()
{
for(int i=0; i<M; i++)
for(int j=0; j<M; j++)
m[i][j]=(i==j);
}
void clearO()
{
for(int i=0; i<M; i++)
for(int j=0; j<M; j++)
m[i][j]=0;
}
};
Matrix operator * (Matrix &a,Matrix &b)
{
Matrix c;
c.clearO();

for(int k=0; k<M; k++)
for(int i=0; i<M; i++)
{
if(a.m[i][k]==0) continue;
for(int j=0; j<M; j++)
{
if(b.m[k][j]==0) continue;
c.m[i][j]=(c.m[i][j]+a.m[i][k]*b.m[k][j])%MOD;
}
}
return c;
}

Matrix operator ^ (Matrix &a,LL b)
{
Matrix c;
c.clearI();
while(b)
{
if(b&1) c=c*a;
b>>=1;
a=a*a;
}
return c;
}

int main()
{
LL n,k,m;
while(~scanf("%I64d%I64d%I64d",&n,&k,&m))
{
MOD = m;
Matrix a,b;
a.clearO(),b.clearO();
for(int i=0; i<n; i++)
{
b.m[i][n+i]=b.m[n+i][n+i]=1;
for(int j=0; j<n; j++)
{
scanf("%I64d",&a.m[i][j]);
b.m[i][j]=a.m[i][j];

}
}

b=b^(k);
a=a*b;

for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
if(j) printf(" ");
printf("%I64d",a.m[i][j+n]);
}
puts("");
}

}
return 0;
}