题目链接:

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. part 4 AngularJS ng src directive

  2. (转)Android之常用功能方法大集合

    这些,都是Andorid中比较常用的方法和功能,在网上搜集整理一下记录之,以备不时之需.由于经过多次转载,源文作者不确凿,在此申明,敬请见谅.不得不赞,非常实用. 1.判断sd卡是否存在 boolea ...

  3. WPF button 如何区分click和doubleclick

    WPF button 同时处理两个事件时候会先触发click事件,触发doubleclick事件  ,那如何区分呢,可以这样设置: private static DispatcherTimer myC ...

  4. Java 字节流实现文件读写操作(InputStream-OutputStream)

    Java 字节流实现文件读写操作(InputStream-OutputStream) 备注:字节流比字符流底层,但是效率底下. 字符流地址:http://pengyan5945.iteye.com/b ...

  5. ArcSDE for Microsoft SQL Server Post Installation图解(转)

    ArcSDE for Microsoft SQL Server Post Installation图解 使用ArcSDE作为空间数据引擎时,经常遇到服务无法启动的情况(启动服务时提示:本地计算机上的a ...

  6. oracle连接和执行流程总结

    参考关于oracle连接及一个事务的完整流程分析的资料,做整理如下 参考资料: http://blog.csdn.net/wyzxg/archive/2010/08/16/5815335.aspx h ...

  7. mysql颠覆实战笔记(五)--商品系统设计(二):定时更新商品总点击量

    继续回到沈老师的MYSQL颠覆实战,首先回顾下上一节课的内容,请大家会看下上节课写的存储过程. 打开prod_clicklog表, 我们只要把日期(不含时分秒)的部分存在数据库中, 如果同一日期有相同 ...

  8. Less 导入命令 @import

    在这个less文件上想导入另一个less文件, 连在同级的文件里直接可用文件名 @import url('css.less')或@import rul(css) 连下级的文件 @import url( ...

  9. Win7下MongoDB安装

    一.下载MongoDB 下载地址:http://www.mongodb.org/downloads 注意:1.从2.2开始,MongoDB不再支持windows xp. 2.32位MongoDB最大支 ...

  10. Linux下iptables拦截Nginx的问题

    环境:CentOS 6.4 X64,Nginx 1.5.3 问题:配置好Nginx后,加入了“iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT” ...