给定一个矩阵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(等比矩阵求和)的更多相关文章

  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(倍增法、矩阵快速幂)

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

  3. UVA 11149 Power of Matrix 快速幂

    题目链接: http://acm.hust.edu.cn/vjudge/contest/122094#problem/G Power of Matrix Time Limit:3000MSMemory ...

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

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

  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

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

  9. Power of Matrix(uva11149+矩阵快速幂)

    Power of Matrix Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit St ...

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

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

随机推荐

  1. Linux 典型应用之WebServer 安装和配置

    Apache的基本操作 安装 yum install httpd 启动 service httpd start  在浏览器中输入以下Ip 发现无法访问 http://192.168.1.109/ 输入 ...

  2. CentOS查看和修改PATH环境变量的方法 profile

    https://blog.csdn.net/dongheli/article/details/83987092

  3. Android下的软件合集

    在平常使用Android手机的时候,选择一个好的软件可以做到事半功倍的效果,所以在此总结一下,加速我们的工作与生活效率 1) ConnectBot ConnectBot是一个Android操作系统上的 ...

  4. [转帖]Windows平台卸载Oracle的办法

    1.首先打开服务:选中此电脑->点击右键->选择管理->选择服务和应用程序->服务 在右边查看并停止以 oracle开头的服务(选中正在运行的以oracle开头的服务-> ...

  5. 1228.Poor Pigs 可怜的猪

    转自[LeetCode] Poor Pigs 可怜的猪 There are 1000 buckets, one and only one of them contains poison, the re ...

  6. 虚拟机安装CentOS7之后没有ip的问题

    CentOS 7 默认是不启动网卡的(ONBOOT=no),主要是修改一下网上配置,然后重起便可,看这篇博客操作: https://blog.csdn.net/dancheren/article/de ...

  7. 本地数据访问时出现跨域问题Cross origin requests are only supported for protocol schemes: ……

    从桌面找到Chrome图标,右键属性,快捷方式,起始位置(安装路径) 注:在cmd中访问Program Files文件的方法 %ProgramFiles%=C:\Program Files %Prog ...

  8. node 模块化思想中index.js的重要性

    目录结构如上图 module1和modlue2.main在同一级 module1下文件: index.js var test2=require('./test2'); var sayHi=functi ...

  9. python之路--内置函数, 匿名函数

    一 . 内置函数 什么是内置函数? 就是python给你提供的. 拿来直接⽤的函数, 比如print., input等等. 字符串类型代码的执⾏ eval() 执⾏字符串类型的代码. 并返回最终结果( ...

  10. postman+jenkins+newman自动化api接口测试

    一.下载nodejs https://nodejs.org/zh-cn/download/ 二.linux下解压 xz -d node-v8.11.3-linux-x64.tar.xz tar xf ...