UVa 11149 矩阵的幂(矩阵倍增法模板题)

https://vjudge.net/problem/UVA-11149

题意:

输入一个n×n矩阵A,计算A+A^2+A^3+...A^k的值。

思路:

矩阵倍增法。

处理方法如下,一直化简下去直到变成A。

代码如下:

1 Matrix solve(Matrix base,int x)
2 {
3     if(x==1)return base;
4     Matrix temp=solve(base,x/2);
5     Matrix sum=add(temp,multi(pow(base,x/2),temp));
6     if(x&1)
7         sum=add(pow(base,x),sum);
8     return sum;
9 }
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<vector>
 6 #include<stack>
 7 #include<queue>
 8 #include<cmath>
 9 #include<map>
10 using namespace std;
11
12 const int maxn=40+5;
13 const int MOD=10;
14
15 int n,k;
16
17 struct Matrix
18 {
19     int mat[maxn][maxn];
20 }base;
21
22 Matrix multi(Matrix a,Matrix b)
23 {
24     Matrix temp;
25     for(int i=0;i<n;i++)
26     for(int j=0;j<n;j++)
27     {
28         temp.mat[i][j]=0;
29         for(int k=0;k<n;k++)
30             temp.mat[i][j]=(temp.mat[i][j]+a.mat[i][k]*b.mat[k][j])%MOD;
31     }
32     return temp;
33 }
34
35 Matrix pow(Matrix a,int x)
36 {
37     Matrix res;
38     memset(res.mat,0,sizeof(res.mat));
39     for(int i=0;i<n;i++)  res.mat[i][i]=1;
40     while(x)
41     {
42         if(x&1)  res=multi(res,a);
43         a=multi(a,a);
44         x>>=1;
45     }
46     return res;
47 }
48
49 Matrix add(Matrix a,Matrix b)
50 {
51     Matrix temp;
52     for(int i=0;i<n;i++)
53         for(int j=0;j<n;j++)
54         temp.mat[i][j]=(a.mat[i][j]+b.mat[i][j])%MOD;
55     return temp;
56 }
57
58 Matrix solve(Matrix base,int x)
59 {
60     if(x==1)return base;
61     Matrix temp=solve(base,x/2);
62     Matrix sum=add(temp,multi(pow(base,x/2),temp));
63     if(x&1)
64         sum=add(pow(base,x),sum);
65     return sum;
66 }
67
68 int main()
69 {
70     //freopen("D:\\input.txt","r",stdin);
71     while(~scanf("%d%d",&n,&k) &&n)
72     {
73         for(int i=0;i<n;i++)
74         for(int j=0;j<n;j++)
75         {
76             scanf("%d",&base.mat[i][j]);
77             base.mat[i][j]%=MOD;
78         }
79         Matrix ans=solve(base,k);
80         for(int i=0;i<n;i++)
81         {
82             for(int j=0;j<n;j++)
83             {
84                 if(j)  printf(" ");
85                 printf("%d",ans.mat[i][j]);
86             }
87             printf("\n");
88         }
89         printf("\n");
90     }
91     return 0;
92 }
时间: 2024-10-05 19:12:27

UVa 11149 矩阵的幂(矩阵倍增法模板题)的相关文章

UVa 11149 Power of Matrix (矩阵快速幂,倍增法或构造矩阵)

题意:求A + A^2 + A^3 + ... + A^m. 析:主要是两种方式,第一种是倍增法,把A + A^2 + A^3 + ... + A^m,拆成两部分,一部分是(E + A^(m/2))(A + A^2 + A^3 + ... + A^(m/2)),然后依次计算下去,就可以分解,logn的复杂度分解,注意要分奇偶. 另一种是直接构造矩阵,,然后就可以用辞阵快速幂计算了,注意要用分块矩阵的乘法. 代码如下: 倍增法: #pragma comment(linker, "/STACK:10

UVA 11149 - Power of Matrix(矩阵倍增)

UVA 11149 - Power of Matrix 题目链接 题意:给定一个n*n的矩阵A和k,求∑kiAi 思路:利用倍增去搞,∑kiAi=(1+Ak/2)∑k/2iAi,不断二分即可 代码: #include <cstdio> #include <cstring> const int N = 45; int n, k; struct mat { int v[N][N]; mat() {memset(v, 0, sizeof(v));} mat operator * (mat

Uva 11149 - Power of Matrix ( 矩阵快速幂 )

Uva 11149 -Power of Matrix ( 矩阵快速幂 ) #include <cstdio> #include <cstring> #define CLR( a, b ) memset( a, b, sizeof(a) ) #define MOD 10 #define MAX_SIZE 40 struct Mat { int r, c; int mat[MAX_SIZE][MAX_SIZE]; Mat( int _r = 0 , int _c = 0 ) { CLR

hdu 4965 矩阵快速幂 矩阵相乘性质

Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 170    Accepted Submission(s): 99 Problem Description One day, Alice and Bob felt bored again, Bob knows Alice is a gir

[POJ 3150] Cellular Automaton (矩阵快速幂 + 矩阵乘法优化)

Cellular Automaton Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 3048   Accepted: 1227 Case Time Limit: 2000MS Description A cellular automaton is a collection of cells on a grid of specified shape that evolves through a number of dis

HDU 4965 Fast Matrix Caculation ( 矩阵乘法 + 矩阵快速幂 + 矩阵乘法的结合律 )

HDU 4965 Fast Matrix Calculation ( 矩阵乘法 + 矩阵快速幂 + 矩阵乘法的结合律 ) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define MAX_SIZE 1001 #define CLR( a, b ) memset( a, b, sizeof(a) ) #define MOD 6 typedef long lo

扩展欧几里得算法模板题 zoj 3609

Modular Inverse Time Limit: 2 Seconds      Memory Limit: 65536 KB The modular modular multiplicative inverse of an integer a modulo m is an integer x such that a-1≡x (mod m). This is equivalent to ax≡1 (mod m). Input There are multiple test cases. Th

模板——矩阵快速幂+矩阵乘法

#include<bits/stdc++.h> using namespace std; const long long P=1e9+7; long long n,m; struct nob{ long long juzhen[105][105]; friend nob operator * (const nob &a,const nob &b){ nob c; for (int i=1; i<=n; i++){ for (int l=1; l<=n; l++){

poj1470 LCA倍增法

倍增法模板题 #include<iostream> #include<cstring> #include<cstdio> #include<queue> using namespace std; #define maxn 1000 #define DEG 20 struct Edge{ int to,next; }edge[maxn*maxn*2]; int head[maxn],tot; void addedge(int u,int v){ edge[to