线性代数(矩阵乘法):POJ 3233 Matrix Power Series

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 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

  一道比较有意思的水题,水一水更健康!
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 using namespace std;
 5 const int maxn=50;
 6 int mod,n,K;
 7 struct Matrix{
 8     int mat[maxn][maxn];
 9     Matrix(){
10         memset(mat,0,sizeof(mat));
11     }
12
13     Matrix operator +(Matrix a){
14         Matrix r;
15         for(int i=1;i<=n;i++)
16             for(int j=1;j<=n;j++)
17                 (r.mat[i][j]=(mat[i][j]+a.mat[i][j])%mod)%=mod;
18         return r;
19     }
20
21     Matrix operator *(Matrix a){
22         Matrix r;
23         for(int i=1,s;i<=n;i++)
24             for(int k=1;k<=n;k++){
25                 s=mat[i][k];
26                 for(int j=1;j<=n;j++)
27                     (r.mat[i][j]+=(s*a.mat[k][j])%mod)%=mod;
28             }
29         return r;
30     }
31
32     Matrix operator ^(int k){
33         Matrix r,x;
34         for(int i=1;i<=n;i++)
35             r.mat[i][i]=1;
36         for(int i=1;i<=n;i++)
37             for(int j=1;j<=n;j++)
38                 x.mat[i][j]=mat[i][j];
39         while(k){
40             if(k&1)
41                 r=r*x;
42             k>>=1;
43             x=x*x;
44         }
45         return r;
46     }
47 }A,B,ans;
48 Matrix Solve(int k){
49     if(k==1)return A;
50     if(k%2)return A+A*Solve(k-1);
51     else return ((A^(k/2))+B)*Solve(k/2);
52 }
53 int main(){
54 #ifndef ONLINE_JUDGE
55     //freopen("","r",stdin);
56     //freopen("","w",stdout);
57 #endif
58     scanf("%d%d%d",&n,&K,&mod);
59     for(int i=1;i<=n;i++)
60         for(int j=1;j<=n;j++)
61             scanf("%d",&A.mat[i][j]);
62
63     for(int i=1;i<=n;i++)
64         B.mat[i][i]=1;
65
66     ans=Solve(K);
67
68     for(int i=1;i<=n;i++){
69         for(int j=1;j<=n;j++)
70             printf("%d ",ans.mat[i][j]);
71         printf("\n");
72     }
73     return 0;
74 }
时间: 2024-08-25 19:44:34

线性代数(矩阵乘法):POJ 3233 Matrix Power Series的相关文章

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分)

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; #define MAX_SIZE 30 #define CLR( a, b ) memset( a, b, sizeof(a) ) int MOD = 0; int n, k; st

矩阵十点【两】 poj 1575 Tr A poj 3233 Matrix Power Series

poj 1575  Tr A 主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1575 题目大意:A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. 数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n(2 <= n <= 10)和k(2 <= k < 10^9)两个数据.接下来有n行,每行有n个数据,每一个数据的范围是[0,9].表示方阵A的内容. 一个矩阵高速幂的裸题. 题解: #

Poj 3233 Matrix Power Series(矩阵二分快速幂)

题目链接:http://poj.org/problem?id=3233 解题报告:输入一个边长为n的矩阵A,然后输入一个k,要你求A + A^2 + A^3 + A^4 + A^5.......A^k,然后结果的每个元素A[i][j] % m.(n <= 30,k < 10^9,m < 10^4) 要用到矩阵快速幂,但我认为最重要的其实还是相加的那个过程,因为k的范围是10^9,一个一个加肯定是不行的,我想了一个办法就是我以k = 8为例说明: ans = A + A^2 + A^3 +

poj 3233 Matrix Power Series(等比矩阵求和)

http://poj.org/problem?id=3233 ps转: 用二分方法求等比数列前n项和:即 原理: (1)若n==0 (2)若n%2==0     (3)若n%2==1 代码如下: LL sum(LL p,LL n) { if(n==0) return 1; if(n&1) return (1+pow(p,(n>>1)+1))*sum(p,n>>1); else return (1+pow(p,(n>>1)+1))*sum(p,(n-1)>&

POJ 3233 Matrix Power Series 【经典矩阵快速幂+二分】

任意门:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 28619   Accepted: 11646 Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + - + Ak. Input The

POJ 3233 Matrix Power Series(矩阵+二分)

题目大意:求由矩阵 A构成的矩阵 S = A + A^2 + A^3 + - + A^k.k的取值范围是:10^9数据很大,应该二分. 对于一个k来说,s(k) = (1+A^(k/2)) *( A+A^2+--+A^(k/2)).如果k为奇数的话需要加上A^(k/2 + 1). 所以二分求和,复杂度就降下来了,当然还得用到矩阵快速幂. Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions

POJ 3233 Matrix Power Series (矩阵快速幂+二分)

Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 16403   Accepted: 6980 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 cas

[ACM] POJ 3233 Matrix Power Series (求矩阵A+A^2+A^3...+A^k,二分求和)

Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 15417   Accepted: 6602 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 cas

POJ 3233 Matrix Power Series(矩阵快速幂)

Default Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 15553 Accepted: 6658 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