UVA - 11149 Power of Matrix(矩阵倍增)
题意:已知N*N的矩阵A,输出矩阵A + A2 + A3 + . . . + Ak,每个元素只输出最后一个数字。
分析:
A + A2 + A3 + . . . + An可整理为下式,

从而可以用log2(n)的复杂度算出结果。
注意:输入时把矩阵A的每个元素对10取余,因为若不处理,会导致k为1的时候结果出错。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 10;
const double pi = acos(-1.0);
const int MAXN = 40 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int n;
struct Matrix{
int r, c, matrix[MAXN][MAXN];
Matrix(int rr, int cc):r(rr), c(cc){
memset(matrix, 0, sizeof matrix);
}
};
Matrix add(Matrix a, Matrix b){
Matrix ans(n, n);
for(int i = 0; i < a.r; ++i){
for(int j = 0; j < a.c; ++j){
ans.matrix[i][j] = ((a.matrix[i][j] % MOD) + (b.matrix[i][j] % MOD)) % MOD;
}
}
return ans;
}
Matrix mul(Matrix a, Matrix b){
Matrix ans(a.r, b.c);
for(int i = 0; i < a.r; ++i){
for(int j = 0; j < b.c; ++j){
for(int k = 0; k < a.c; ++k){
(ans.matrix[i][j] += ((a.matrix[i][k] % MOD) * (b.matrix[k][j] % MOD)) % MOD) %= MOD;
}
}
}
return ans;
}
Matrix QPOW(Matrix a, int k){
Matrix ans(n, n);
for(int i = 0; i < n; ++i){
ans.matrix[i][i] = 1;
}
while(k){
if(k & 1) ans = mul(ans, a);
a = mul(a, a);
k >>= 1;
}
return ans;
}
Matrix solve(Matrix tmp, int k){
if(k == 1) return tmp;
Matrix t = solve(tmp, k >> 1);
Matrix ans = add(t, mul(QPOW(tmp, k >> 1), t));
if(k & 1) ans = add(ans, QPOW(tmp, k));
return ans;
}
int main(){
int k;
while(scanf("%d%d", &n, &k) == 2){
if(n == 0) return 0;
Matrix tmp(n, n);
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
scanf("%d", &tmp.matrix[i][j]);
tmp.matrix[i][j] %= MOD;
}
}
Matrix ans = solve(tmp, k);
for(int i = 0; i < ans.r; ++i){
for(int j = 0; j < ans.c; ++j){
if(j) printf(" ");
printf("%d", ans.matrix[i][j]);
}
printf("\n");
}
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(倍增法、矩阵快速幂)
题目链接: 传送门 Power of Matrix Time Limit: 3000MS Description 给一个n阶方阵,求A1+A2+A3+......Ak. 思路 A1+A2+. ...
- 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 矩阵快速幂
题意: 给出一个\(n \times n\)的矩阵\(A\),求\(A+A^2+A^3+ \cdots + A^k\). 分析: 这题是有\(k=0\)的情况,我们一开始先特判一下,直接输出单位矩阵\ ...
- UVA 11149 Power of Matrix 快速幂
题目链接: http://acm.hust.edu.cn/vjudge/contest/122094#problem/G Power of Matrix Time Limit:3000MSMemory ...
- UVA 11149 Power of Matrix 构造矩阵
题目大意:意思就是让求A(A是矩阵)+A2+A3+A4+A5+A6+······+AK,其中矩阵范围n<=40,k<=1000000. 解题思路:由于k的取值范围很大,所以很自然地想到了二 ...
- UVA 11149 Power of Matrix
矩阵快速幂. 读入A矩阵之后,马上对A矩阵每一个元素%10,否则会WA..... #include<cstdio> #include<cstring> #include< ...
- UVA11149 Power of Matrix —— 矩阵倍增、矩阵快速幂
题目链接:https://vjudge.net/problem/UVA-11149 题意: 给出矩阵A,求出A^1 + A^2 …… + A^k . 题解: 1.可知:A^1 + A^2 …… + A ...
- UVA 11149.Power of Matrix-矩阵快速幂倍增
Power of Matrix UVA - 11149 代码: #include <cstdio> #include <cstring> #include < ...
随机推荐
- SpringCloud实战——(4)基于Eureka、Zuul
- eclipse搜索类快捷键
习惯的编辑器可以提高编程效率,熟悉的快捷键可以提高工作效率,本文更新eclipse中常用的搜索快捷键 打开资源快捷键:Ctrl+Shift+R 通过在搜索框中输入名字可以很方便的在项目或工作空间中找某 ...
- 引用类型--Object类型、Array类型
引用类型的值(对象)是引用类型的一个实例.在ECMAScript中,引用类型是一种数据结构,它描述的是一类对象具有的属性和方法. 对象是某个特定引用类型的实例,新对象是使用new操作符后跟一个构造函数 ...
- 「JSOI2008」Blue Mary的旅行
传送门 Luogu 解题思路 分层图加网络流,有点像这题 可以证明最多不超过100天,所以才可以分层,不然图的规模会很大. 首先连源点汇点: \((s,1,INF), (n, t, INF)\) 以时 ...
- 安装oracle客户端后,怎样设置电脑的环境变量?
安装配置参考: http://www.haodaima.net/art/2854001 设置环境变量(修改PATH和TNS_ADMIN环境变量): 对于NLS_LANG环境变量, 最好设置成和数据库端 ...
- SQL Server DATEADD() 函数 一步步使用教程
SQL Server DATEADD() 函数 DATEADD() 函数在日期中添加或减去指定的时间间隔. DATEADD(datepart,number,date)date 参数是合法的日期表达式. ...
- Python format语法
a = {"name" : "alex","age":16} v = "my name is {name}, my age is ...
- 二十 Filter&自动登录功能
Filter过滤器 过滤器,其实就是对客户端发出来的请求进行过滤,浏览器发出,然后服务器用Servelt处理.在中间就可以过滤,起到的是拦截的作用. 不仅仅作用于客户端请求,而且过滤服务器响应 作用: ...
- P1092 最好吃的月饼
1092 最好吃的月饼 (20分) 月饼是久负盛名的中国传统糕点之一,自唐朝以来,已经发展出几百品种. 若想评比出一种“最好吃”的月饼,那势必在吃货界引发一场腥风血雨…… 在这里我们用数字说话,给 ...
- GNS3 模拟icmp路由跟踪
R1 : conf t int f0/0 no shutdown ip add 192.168.1.1 255.255.255.0 no ip routing end R2 f0/0: conf t ...