poj3233(等比矩阵求和)
poj3233
题意
给出一个 \(n \times n\) 的矩阵 \(A\) ,求 \(A + A^2 + A^3 + ... + A^k\) 。
分析
构造矩阵
\]
记为 \(B\) ,其中 \(A\) 为原矩阵,\(E\) 为 \(n \times n\) 的单位矩阵,\(0\) 为 \(n \times n\) 的零矩阵。
那么求 \(B^{k+1}\) ,
有
\]
可以发现右边减去一个单位矩阵就是我们所要求的答案。
code
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 65;
int n, m, k;
struct Matrix {
int mat[N][N];
Matrix() { memset(mat, 0, sizeof mat); }
};
Matrix operator * (Matrix A, Matrix B) {
Matrix C;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
for(int k = 0; k < n; k++) {
(C.mat[i][j] += A.mat[i][k] * B.mat[k][j]) %= m;
}
}
}
return C;
}
Matrix operator ^ (Matrix A, int x) {
Matrix B;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(i == j) B.mat[i][j] = 1;
}
}
while(x) {
if(x & 1) B = B * A;
A = A * A;
x >>= 1;
}
return B;
}
int main() {
scanf("%d%d%d", &n, &k, &m);
Matrix A;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
scanf("%d", &A.mat[i][j]);
}
}
// 右边的单位矩阵
for(int i = 0; i < n; i++) {
for(int j = n; j < 2 * n; j++) {
if(j - i == n) A.mat[i][j] = 1;
}
}
// 右下方的单位矩阵
for(int i = n; i < 2 * n; i++) {
for(int j = n; j < 2 * n; j++) {
if(i == j) A.mat[i][j] = 1;
}
}
// 下方的 0 矩阵,省略
// ...
n <<= 1;
A = A ^ (k + 1);
n >>= 1;
// 将右边的矩阵减去一个单位矩阵
for(int i = 0; i < n; i++) {
for(int j = n; j < 2 * n; j ++) {
if(j - i == n) A.mat[i][j] = (A.mat[i][j] - 1 + m) % m;
printf("%d%c", A.mat[i][j], " \n"[j == 2 * n - 1]);
}
}
return 0;
}
poj3233(等比矩阵求和)的更多相关文章
- poj 1195:Mobile phones(二维树状数组,矩阵求和)
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14489 Accepted: 6735 De ...
- poj 1195:Mobile phones(二维线段树,矩阵求和)
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14391 Accepted: 6685 De ...
- UVA 11149-Power of Matrix(等比矩阵求和)
给定一个矩阵A 要求A + A^2 + A^3 +…. A^k: 对于到n的等比矩阵求和 如果n是偶数: 如果n是奇数: #include<stdio.h> #include<s ...
- BZOJ 2901: 矩阵求和
2901: 矩阵求和 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 411 Solved: 216[Submit][Status][Discuss] ...
- BZOJ_2901_矩阵求和_前缀和
BZOJ_2901_矩阵求和_前缀和 Description 给出两个n*n的矩阵,m次询问它们的积中给定子矩阵的数值和. Input 第一行两个正整数n,m. 接下来n行,每行n个非负整数,表示第一 ...
- YTU 2442: C++习题 矩阵求和--重载运算符
2442: C++习题 矩阵求和--重载运算符 时间限制: 1 Sec 内存限制: 128 MB 提交: 1457 解决: 565 题目描述 有两个矩阵a和b,均为2行3列.求两个矩阵之和.重载运 ...
- YTU 2640: 编程题:运算符重载---矩阵求和
2640: 编程题:运算符重载---矩阵求和 时间限制: 1 Sec 内存限制: 128 MB 提交: 484 解决: 190 题目描述 /* 有两个矩阵a和b,均为2行3列.求两个矩阵之和. 重 ...
- hdu 1588(Fibonacci矩阵求和)
题目的大意就是求等差数列对应的Fibonacci数值的和,容易知道Fibonacci对应的矩阵为[1,1,1,0],因为题目中f[0]=0,f[1]=1,所以推出最后结果f[n]=(A^n-1).a, ...
- HDU 6336.Problem E. Matrix from Arrays-子矩阵求和+规律+二维前缀和 (2018 Multi-University Training Contest 4 1005)
6336.Problem E. Matrix from Arrays 不想解释了,直接官方题解: 队友写了博客,我是水的他的代码 ------>HDU 6336 子矩阵求和 至于为什么是4倍的, ...
随机推荐
- powershell入门教程-v0.3版
powershell入门教程-v0.3版 来源 https://www.itsvse.com/thread-3650-1-1.html 参考 http://www.cnblogs.com/piapia ...
- (补漏)Springboot2.0 集成shiro权限管理
原文Springboot2.0 集成shiro权限管理 一.关于停止使用外键. 原本集成shiro建立用户.角色.权限表的时候使用了外键,系统自动创建其中两个关联表,用@JoinTable.看起来省事 ...
- Brainf**k(一位数求max)
题目大意:给你两个一位数,要你求出其中的较大值(使用$Brainf**k$) ($Brainf**k$简介,相当于有一个数组和一个指针,","为把数组当前位赋值为读入的数,&quo ...
- BZOJ1407 [Noi2002]Savage 【扩展欧几里得】
题目链接 BZOJ1407 题解 枚举\(m\)用扩欧判即可 #include<algorithm> #include<iostream> #include<cstrin ...
- javascript 随机数区间
生成[0,max]之间的随机数 parseInt(Math.random()*(max+1),10);Math.floor(Math.random()*(max+1)); 生成[1,max]之间的随机 ...
- How to disable index in innodb
Q: I read from many places that disabling index before loading a data table can significantly speed ...
- Any gotchas at all with converting from MyISAM to InnoDB?
Q: I'm ready to move from MyISAM to InnoDB but wanted to know if there was a full list of things to ...
- POJ 3179 Corral the Cows
Corral the Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1352 Accepted: 565 De ...
- idea 导入spring 源码注意的问题
问题:idea导入spring 源码的步骤是: 首先从官网下载spring的源码:git clone https://github.com/spring-projects/spring-framewo ...
- ecma 2018, javascript spread syntax behaves like Object.assign
as the subject. It is only supported in Chrome version 60+, so, first check the version, or just use ...