题目链接:

http://acm.hust.edu.cn/vjudge/contest/122094#problem/G

Power of Matrix

Time Limit:3000MS
Memory Limit:0KB
#### 问题描述
> 给你一个矩阵A,求A+A^2+A^3+...+A^k
#### 输入
> Input consists of no more than 20 test cases. The first line for each case contains two positive integers n
> (≤ 40) and k (≤ 1000000). This is followed by n lines, each containing n non-negative integers, giving
> the matrix A.
> Input is terminated by a case where n = 0. This case need NOT be processed.

输出

For each case, your program should compute the matrix A + A2 + A3 + . . . + Ak

. Since the values may

be very large, you only need to print their last digit. Print a blank line after each case.

样例

sample input

3 2

0 2 0

0 0 2

0 0 0

0 0

sample output

0 2 4

0 0 2

0 0 0

题解

A+A2+...Ak=(I+A(n/2))(A+...+A(n/2))=...

一直递推下去,深度只有logn,只要logn次快速幂求A^x。

代码

WA四次的代码:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std; const int maxn = 55;
const int mod = 10;
typedef int LL; struct Matrix {
LL mat[maxn][maxn];
Matrix() { memset(mat, 0, sizeof(mat)); }
friend Matrix operator *(const Matrix& A, const Matrix& B);
friend Matrix operator +(const Matrix &A,const Matrix &B);
friend Matrix operator ^(Matrix A, int n);
}; Matrix I; Matrix operator +(const Matrix& A, const Matrix& B) {
Matrix ret;
for (int i = 0; i < maxn; i++) {
for (int j = 0; j < maxn; j++) {
ret.mat[i][j] = (A.mat[i][j] + B.mat[i][j])%mod;
}
}
return ret;
} Matrix operator *(const Matrix& A, const Matrix& B) {
Matrix ret;
for (int i = 0; i < maxn; i++) {
for (int j = 0; j < maxn; j++) {
for (int k = 0; k < maxn; k++) {
ret.mat[i][j] = (ret.mat[i][j]+A.mat[i][k] * B.mat[k][j]) % mod;
}
}
}
return ret;
} Matrix operator ^(Matrix A, int n) {
Matrix ret=I;
while (n) {
if (n & 1) ret = ret*A;
A = A*A;
n /= 2;
}
return ret;
} Matrix solve(Matrix A, int n) {
if (!n) return I;
if (n == 1) return A;
//这里要加括号!!! ret=I+(A^(n/2))!!!
Matrix ret = I + A ^ (n / 2);
ret = ret*solve(A, n / 2);
//这里也要加!!!! ret=ret+(A^n)!!!
if (n % 2) ret = ret + A^n;
return ret;
} int n, k; int main() {
for (int i = 0; i < maxn; i++) I.mat[i][i] = 1;
while (scanf("%d%d", &n, &k) == 2 && n) {
Matrix A;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &A.mat[i][j]);
A.mat[i][j] %= mod;
}
}
Matrix ans=solve(A, k);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n-1; j++) {
printf("%d ",ans.mat[i][j]);
}
printf("%d\n", ans.mat[i][n - 1]);
}
printf("\n");
}
return 0;
}

保险一些的写法:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std; const int maxn = 55;
const int mod = 10;
typedef int LL; struct Matrix {
LL mat[maxn][maxn];
Matrix() { memset(mat, 0, sizeof(mat)); }
friend Matrix operator *(const Matrix& A, const Matrix& B);
friend Matrix operator +(const Matrix &A,const Matrix &B);
friend Matrix pow(Matrix A, int n);
}; Matrix I; Matrix operator +(const Matrix& A, const Matrix& B) {
Matrix ret;
for (int i = 0; i < maxn; i++) {
for (int j = 0; j < maxn; j++) {
ret.mat[i][j] = (A.mat[i][j] + B.mat[i][j])%mod;
}
}
return ret;
} Matrix operator *(const Matrix& A, const Matrix& B) {
Matrix ret;
for (int i = 0; i < maxn; i++) {
for (int j = 0; j < maxn; j++) {
for (int k = 0; k < maxn; k++) {
ret.mat[i][j] = (ret.mat[i][j]+A.mat[i][k] * B.mat[k][j]) % mod;
}
}
}
return ret;
} Matrix pow(Matrix A, int n) {
Matrix ret=I;
while (n) {
if (n & 1) ret = ret*A;
A = A*A;
n /= 2;
}
return ret;
} Matrix solve(Matrix A, int n) {
if (!n) return I;
if (n == 1) return A;
Matrix ret = I + pow(A,n/2);
ret = ret*solve(A, n / 2);
if (n % 2) ret = ret + pow(A,n);
return ret;
} int n, k; int main() {
for (int i = 0; i < maxn; i++) I.mat[i][i] = 1;
while (scanf("%d%d", &n, &k) == 2 && n) {
Matrix A;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &A.mat[i][j]);
A.mat[i][j] %= mod;
}
}
Matrix ans=solve(A, k);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n-1; j++) {
printf("%d ",ans.mat[i][j]);
}
printf("%d\n", ans.mat[i][n - 1]);
}
printf("\n");
}
return 0;
}

UVA 11149 Power of Matrix 快速幂的更多相关文章

  1. UVA 11149 - Power of Matrix(矩阵乘法)

    UVA 11149 - Power of Matrix 题目链接 题意:给定一个n*n的矩阵A和k,求∑kiAi 思路:利用倍增去搞.∑kiAi=(1+Ak/2)∑k/2iAi,不断二分就可以 代码: ...

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

    题意:求A + A^2 + A^3 + ... + A^m. 析:主要是两种方式,第一种是倍增法,把A + A^2 + A^3 + ... + A^m,拆成两部分,一部分是(E + A^(m/2))( ...

  3. UVa 11149 Power of Matrix(倍增法、矩阵快速幂)

    题目链接: 传送门 Power of Matrix Time Limit: 3000MS      Description 给一个n阶方阵,求A1+A2+A3+......Ak. 思路 A1+A2+. ...

  4. UVA 11149 Power of Matrix

    矩阵快速幂. 读入A矩阵之后,马上对A矩阵每一个元素%10,否则会WA..... #include<cstdio> #include<cstring> #include< ...

  5. UVa 11149 Power of Matrix 矩阵快速幂

    题意: 给出一个\(n \times n\)的矩阵\(A\),求\(A+A^2+A^3+ \cdots + A^k\). 分析: 这题是有\(k=0\)的情况,我们一开始先特判一下,直接输出单位矩阵\ ...

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

    题意:已知N*N的矩阵A,输出矩阵A + A2 + A3 + . . . + Ak,每个元素只输出最后一个数字. 分析: A + A2 + A3 + . . . + An可整理为下式, 从而可以用lo ...

  7. UVA 11149 Power of Matrix 构造矩阵

    题目大意:意思就是让求A(A是矩阵)+A2+A3+A4+A5+A6+······+AK,其中矩阵范围n<=40,k<=1000000. 解题思路:由于k的取值范围很大,所以很自然地想到了二 ...

  8. UVA 11149.Power of Matrix-矩阵快速幂倍增

    Power of Matrix UVA - 11149       代码: #include <cstdio> #include <cstring> #include < ...

  9. POJ 3233:Matrix Power Series 矩阵快速幂 乘积

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 18450   Accepted:  ...

随机推荐

  1. GitHub之上传文件

    github是一个基于git的代码托管平台,付费用户可以建私人仓库,我们一般的免费用户只能使用公共仓库,也就是代码要公开.对于一般人来说公共仓库就已经足够了,而且我们也没多少代码来管理,O(∩_∩)O ...

  2. jquery 获得table 行数

    1.获得Table总行数,分别使用了两种方法,结果不同:: 1).$("#table_Id").children("tr").length; 只能获得静态页面t ...

  3. 项目经理PPT演讲意见

    1.语速 2.互动 3.平常语气,聊天的感觉去讲解 4.脱稿演讲,不要照着PPT读,PPT展示仅仅是一个重点提示,更多在于自己讲解 5.如果是验收等相关的内容,劲量多讲解用户能够得到的利益,如“钱” ...

  4. 几款jQuery右键菜单插件

    1.jQuery Very Simple ContextMenu Plugin 2.ContextJS Project Page:http://lab.jakiestfu.com/contextjs/ ...

  5. HTML特殊字符编码大全

    HTML特殊字符编码大全:往网页中输入特殊字符,需在html代码中加入以&开头的字母组合或以&#开头的数字.下面就是以字母或数字表示的 特殊符号大全 ´ © © > > µ ...

  6. ListView Web 服务器控件概述(MSDN)

    1: "折叠"图像"展开"图像"复制"图像"复制悬停"图像 全部折叠全部展开 代码:全部 代码:多个 代码:Visual ...

  7. javascript对象与实例

    var person=new Object(); var person2={}; 一切都是对象 person是Object的实例, 也是对象. 第二个person2也是如此,只不过它是采用字面量的方式 ...

  8. [WinForm]TextBox只能输入数字或者正浮点型数字

    关键代码: /// <summary> /// 只能输入数字[KeyPress事件] /// </summary> /// <param name="textB ...

  9. 利用VSCode进行.Net Core初尝试

    1.下载.Net Core,https://www.microsoft.com/net/core#windows,按照页面提示进行SDK的安装. 2.下载VsCode,https://www.visu ...

  10. Git命令收集【不断更新中】

    git stash 可以用来保存暂时不想提交但又被修改过的文件. git stash pop 用来取出被保存在stash栈中的修改过的所有文件. git stash show 查询哪些文件被存放在了s ...