UVA 11149-Power of Matrix(等比矩阵求和)
给定一个矩阵A 要求A + A^2 + A^3 +…. A^k;
对于到n的等比矩阵求和
如果n是偶数:
如果n是奇数:
#include<stdio.h>
#include<string.h>
#include<algorithm> using namespace std; const int maxn = ;
int mod = ;
int n, k; struct matrix {
int mat[maxn][maxn];
}; matrix mat_add(matrix A, matrix B) {
matrix ans;
for(int i=; i<n; i++) {
for(int j=; j<n; j++) {
ans.mat[i][j] = A.mat[i][j] + B.mat[i][j];
ans.mat[i][j] %= mod;
}
}
return ans;
} matrix mat_mul(matrix A, matrix B) {
matrix ans;
memset(ans.mat, , sizeof(ans.mat));
for(int i=; i<n; i++) {
for(int j=; j<n; j++) {
for(int k=; k<n; k++) {
ans.mat[i][j] += A.mat[i][k] * B.mat[k][j];
ans.mat[i][j] %= mod;
}
}
}
return ans;
} matrix mat_pow(matrix A, int b) {
matrix ans;
matrix p = A;
for(int i=; i<n; i++) {
for(int j=; j<n; j++) {
ans.mat[i][j] = (i == j);
}
}
while(b) {
if(b & )
ans = mat_mul(ans, p);
p = mat_mul(p, p);
b >>= ;
}
return ans;
} matrix work(matrix A, int m) {
if(m == )
return A;
matrix t = work(A, m/);
if(m & ) {
matrix cur = mat_pow(A, m/+);
t = mat_add(t, mat_mul(t, cur));
t = mat_add(t, cur);
} else {
matrix cur = mat_pow(A, m/);
t = mat_add(t, mat_mul(t, cur));
}
return t;
} int main() {
while(scanf("%d%d", &n, &k), n) {
matrix A;
for(int i=; i<n; i++) {
for(int j=; j<n; j++) {
int x;
scanf("%d", &x);
A.mat[i][j] = x % ;
}
}
matrix ans = work(A, k);
for(int i=; i<n; i++) {
for(int j=; j<n; j++) {
printf("%d%c", ans.mat[i][j], j==n- ? '\n' : ' ');
}
}
printf("\n");
}
return ;
}
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 快速幂
题目链接: http://acm.hust.edu.cn/vjudge/contest/122094#problem/G Power of Matrix Time Limit:3000MSMemory ...
- 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(矩阵倍增)
题意:已知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
矩阵快速幂. 读入A矩阵之后,马上对A矩阵每一个元素%10,否则会WA..... #include<cstdio> #include<cstring> #include< ...
- Power of Matrix(uva11149+矩阵快速幂)
Power of Matrix Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit St ...
- UVA 11149.Power of Matrix-矩阵快速幂倍增
Power of Matrix UVA - 11149 代码: #include <cstdio> #include <cstring> #include < ...
随机推荐
- #Leetcode# 633. Sum of Square Numbers
https://leetcode.com/problems/sum-of-square-numbers/ Given a non-negative integer c, your task is to ...
- Drools 规则引擎
Drools - Drools - Business Rules Management System (Java™, Open Source) http://drools.org/ [Drools]J ...
- h5 文件下载
一.a 标签 移动端不支持 onDownFile= (url, filename) => { const downUrl = `http://10.1.109.123:19092/down/to ...
- 调整分区大小 转载--------------http://blog.csdn.net/perfectzq/article/details/73606119
centos7重新调整分区大小 centos 7 调整 root 和 home 的容量大小 查看磁盘的空间大小: df -h 备份/home : cp -r /home/ homebak/ 卸载 ...
- 动态SQL2
set标签 存放修改方法,我们之前写的更新方法是全字段的更新,或者是指定字段的更新,现在我想实现一个新功能,传入的Employee包含什么字段,数据库就更新相对应的列值: 如果我们啥也不做直接上< ...
- Linux基础学习笔记2-文件管理和重定向
本节内容 1)文件系统结构元素 2)创建和查看文件 3)复制.转移和删除文件 4)软和硬链接 5)三种I/O设备 6)把I/O重定向至文件 7)使用管道 文件系统和结构 文件系统 文件和目录被组织成一 ...
- Java 基础类型 默认值
(1)数据库里的列,如果有默认值,不能赋值有业务含义的值. (2)int 默认值 java会分配默认值的额.
- No module named 'ConfigParser'
系统: CentOS-6.4-x86_64 Python : Python 3.4.5 和 Python 3.5.2 安装 MySQL-python ,结果出错: ImportError: No mo ...
- QTP 自动化测试--点滴 等待
1 使用wait()语句:wait(10) 等待10秒后继续执行 Window("驷惠WIN系列[汽车4S连锁管理软件] 6.").Window("应付帐款明细查询&qu ...
- PC平台的SIMD支持检测
如果我们希望在用SIMD来提升程序处理的性能,首先需要做的就是检测程序所运行的平台是否支持相应的SIMD扩展.平台对SIMD扩展分为两部分的支持: CPU对SIMD扩展的支持.SIMD扩展是随着CPU ...