UVA 11149 Power of Matrix 快速幂
题目链接:
http://acm.hust.edu.cn/vjudge/contest/122094#problem/G
Power of Matrix
Time Limit:3000MSMemory 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 0sample 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 快速幂的更多相关文章
- UVA 11149 - Power of Matrix(矩阵乘法)
UVA 11149 - Power of Matrix 题目链接 题意:给定一个n*n的矩阵A和k,求∑kiAi 思路:利用倍增去搞.∑kiAi=(1+Ak/2)∑k/2iAi,不断二分就可以 代码: ...
- UVa 11149 Power of Matrix (矩阵快速幂,倍增法或构造矩阵)
题意:求A + A^2 + A^3 + ... + A^m. 析:主要是两种方式,第一种是倍增法,把A + A^2 + A^3 + ... + A^m,拆成两部分,一部分是(E + A^(m/2))( ...
- UVa 11149 Power of Matrix(倍增法、矩阵快速幂)
题目链接: 传送门 Power of Matrix Time Limit: 3000MS Description 给一个n阶方阵,求A1+A2+A3+......Ak. 思路 A1+A2+. ...
- UVA 11149 Power of Matrix
矩阵快速幂. 读入A矩阵之后,马上对A矩阵每一个元素%10,否则会WA..... #include<cstdio> #include<cstring> #include< ...
- UVa 11149 Power of Matrix 矩阵快速幂
题意: 给出一个\(n \times n\)的矩阵\(A\),求\(A+A^2+A^3+ \cdots + A^k\). 分析: 这题是有\(k=0\)的情况,我们一开始先特判一下,直接输出单位矩阵\ ...
- UVA - 11149 Power of Matrix(矩阵倍增)
题意:已知N*N的矩阵A,输出矩阵A + A2 + A3 + . . . + Ak,每个元素只输出最后一个数字. 分析: A + A2 + A3 + . . . + An可整理为下式, 从而可以用lo ...
- UVA 11149 Power of Matrix 构造矩阵
题目大意:意思就是让求A(A是矩阵)+A2+A3+A4+A5+A6+······+AK,其中矩阵范围n<=40,k<=1000000. 解题思路:由于k的取值范围很大,所以很自然地想到了二 ...
- UVA 11149.Power of Matrix-矩阵快速幂倍增
Power of Matrix UVA - 11149 代码: #include <cstdio> #include <cstring> #include < ...
- POJ 3233:Matrix Power Series 矩阵快速幂 乘积
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 18450 Accepted: ...
随机推荐
- ef codeFirst 修改表结构 增加字段等 EF code first需要重新生成库导致数据丢失的问题.
需要在库程序包管理器里依次执行以下命令: 1.启用迁移功能:Enable-Migrations -ContextTypeName MvcMovie.Models.MovieDbContext 2.建立 ...
- php数据过滤函数与方法示例【转载】
1.php提交数据过滤的基本原则 1)提交变量进数据库时,我们必须使用addslashes()进行过滤,像我们的注入问题,一个addslashes()也就搞定了.其实在涉及到变量取值时,intval( ...
- Access时间日期比较查询的方法总结
Access日期时间比较查询语句困扰过很多网友,种豆网整理了一下Access日期比较查询的几种方法,假定数据表明为TblName,日期/时间字段名为FDate(这里不能讲FDate设置为字符串,否则比 ...
- VxWorks 6.9 内核编程指导之读书笔记 -- POSIX
POSIX能力 VxWorks扩展了POSIX,为了移植,VxWorks提供了额外的POSIX接口作为可选组件.VxWorks实现了POSIX 1003.1(POSIX .1)一些传统接口以及POSI ...
- 多文件上传artDialog+plupload
一.效果展示 包括文件上传面板以及文件上传列表 二.介绍 长话短说,采用spring springMVC mybatis maven mysql,实现多文件上传功能,下载使用的是流的形式. 其中涉及的 ...
- ThinkPHP中的视图二
ThinkPHP中的视图 1.模板注释 在实际项目开发中,经常要使用注释功能,如果是ThinkPHP框架,则可以在模板文件中使用如下方式进行注释: {// 注释内容 } :单行注释 {/* 注释内容 ...
- POJ 3624
背包问题,在定容量的背包中放入物体求装满价值最大.因为每种物体数量只有1,故只有放与不放. #include<iostream> #include<cstring> #incl ...
- C# Winform中DataGridView的DataGridViewCheckBoxColumn使用方法
下面介绍Winform中DataGridView的DataGridViewCheckBoxColumn使用方法: DataGridViewCheckBoxColumn CheckBox是否选中 在判断 ...
- Sql 临时表
一个#是只能在当前打开滴查询窗体查询,两个#是能够在其他打开滴查询窗体查询 SELECT 'VR001839003YP' 列名1,'RO512498726DE' 列名2 INTO #临时表 UNION ...
- 使用 PHP 验证表单数据
使用 PHP 验证表单数据 首先我们对用户所有提交的数据都通过 PHP 的 htmlspecialchars() 函数处理. 当我们使用 htmlspecialchars() 函数时,在用户尝试提交以 ...