poj 3233 S = A + A^2 + A^3 + … + A^k A是一个n X n矩阵

S = A + A^2 + A^3 + … + A^k A是一个n*n矩阵

Sample Input

2 2 4 //n k MOD
0 1
1 1
Sample Output

1 2
2 3

先求 I + A + A^2 + A^3 + … + A^k  I为单位矩阵

我们来设置这样一个矩阵 B=
A I
O I
其中O是零矩阵,I是单位矩阵

将它乘方,得到
A^2 I+A
O I
乘三方,得到
A^3 I+A+A^2
O I
乘四方,得到
A^4 I+A+A^2+A^3
O I

然后B^(k+1) 的右上小矩阵 就是 I + A + A^2 + A^3 + … + A^k

记得 数组开大一点...因为要扩展成2n*2n的矩阵, 第一次开小了,数组越界=.=

 1 # include <iostream>
 2 # include <cstdio>
 3 # include <algorithm>
 4 # include <cmath>
 5 # define LL long long
 6 using namespace std ;
 7
 8 int MOD ;
 9 int n ;
10
11 struct Matrix
12 {
13     LL mat[70][70];
14 };
15
16 Matrix mul(Matrix a,Matrix b)
17 {
18     Matrix c;
19     for(int i=0;i<2*n;i++)
20         for(int j=0;j<2*n;j++)
21         {
22             c.mat[i][j]=0;
23             for(int k=0;k<2*n;k++)
24             {
25                 c.mat[i][j]=(c.mat[i][j] + a.mat[i][k]*b.mat[k][j])%MOD;
26             }
27         }
28     return c;
29 }
30 Matrix pow_M(Matrix a,int k )
31 {
32     Matrix ans;
33     memset(ans.mat,0,sizeof(ans.mat));
34     for (int i=0;i<2*n;i++)
35         ans.mat[i][i]=1;
36     Matrix temp=a;
37     while(k)
38     {
39         if(k&1)ans=mul(ans,temp);
40         temp=mul(temp,temp);
41         k>>=1;
42     }
43     return ans;
44 }
45
46
47
48 int main ()
49 {
50     //freopen("in.txt","r",stdin) ;
51     int  k ;
52     while(cin>>n>>k>>MOD)
53     {
54         Matrix A ;
55         int i , j ;
56         for (i = 0 ; i < n ; i++)
57             for (j = 0 ; j < n ; j++)
58                cin>>A.mat[i][j] ;
59         Matrix B ;
60         memset(B.mat,0,sizeof(B.mat));
61         for (i = 0 ; i < n ; i++) //扩展成2n * 2n的矩阵
62         {
63             for (j = 0 ; j < n ; j++)
64             {
65                 B.mat[i][j] = A.mat[i][j] ;
66             }
67             B.mat[n+i][n+i] = 1 ;
68             B.mat[i][n+i] = 1 ;
69
70         }
71         B = pow_M(B,k+1) ;
72         LL t ;
73         for (i = 0 ; i < n ; i++)
74             for (j = 0 ; j < n ; j++)
75             {
76                 t = B.mat[i][n+j] % MOD ;
77                 if (i == j)
78                     t = (t + MOD - 1)%MOD ;
79                 if (j+1 != n)
80                     cout<<t<<" " ;
81                 else
82                     cout<<t<<endl ;
83             }
84
85     }
86
87
88
89     return 0 ;
90 }

时间: 2024-08-07 04:31:50

poj 3233 S = A + A^2 + A^3 + … + A^k A是一个n X n矩阵的相关文章

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 题意:给一个N*N的矩阵(N<=30),求S = A + A^2 + A^3 + - + A^k(k<=10^9). 思路:非常明显直接用矩阵高速幂暴力求和的方法复杂度O(klogk).肯定会超时.我採用的是二分的方法, A + A^2 + A^3 + - + A^k=(1+A^(k/2)) *(A + A^2 + A^3 + - + A^(k/2)).这样就能够提出一个(1+A^(k/2)),假设k是奇数,单独处理A^k.

poj 3233 Matrix Power Series - 矩阵快速幂

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 li

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

poj 3233(矩阵快速幂)

题目链接:http://poj.org/problem?id=3233: 题意:给出一个公式求这个式子模m的解: 分析:本题就是给的矩阵,所以很显然是矩阵快速幂,但有一点,本题k的值非常大,所以要用二分求和来减少运行时间. 代码: #include <set> #include <map> #include <stack> #include <queue> #include <math.h> #include <vector> #in

矩阵运算+快速幂+二分法 poj 3233

题目链接:http://poj.org/problem?id=3233 思路:矩阵运算,快速幂,二分法..  妈的 b>>=1 我写成了b>>=2 wrong了一天. 真棒 如何求a+a^2+a^3+ ..... a^n = ? 用二分思想做:我们用S(n/2) =  (a+a^2+a^3+...a^n/2); 即 n为奇数时 S(n) = S(n-1) + a^n; n为偶数时候 S(n) = S(n/2) + S(n/2)*a^n/2; 知道这个就好办了~   注意细节~...