HDU 4965 Fast Matrix Calculation 矩阵快速幂
题意:
给出一个\(n \times k\)的矩阵\(A\)和一个\(k \times n\)的矩阵\(B\),其中\(4 \leq N \leq 1000, \, 2 \leq K \leq 6\)。
矩阵\(C=A \cdot B\),求矩阵\(C^{N^2}\)的各个元素之和,以上矩阵运算均是在模\(6\)的情况下计算的。
分析:
如果我们直接计算\(A \cdot B\)的话,这个矩阵非常大,不可能进行快速幂计算。
所以要变形一下,
\((A \cdot B)^{N^2}=A \cdot (B \cdot A)^{N^2-1} \cdot B\)
而矩阵\(B \cdot A\)非常小,问题就解决了。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1000 + 10;
const int MOD = 6;
inline int mul_mod(int a, int b) {
return a * b % MOD;
}
inline void add_mod(int& a, int b) {
a += b; if(a >= 6) a -= 6;
}
int N, K, A[maxn][MOD], B[MOD][maxn];
int tmp[maxn][MOD], res[maxn][maxn];
struct Matrix
{
int a[MOD][MOD];
Matrix() { memset(a, 0, sizeof(a)); }
Matrix operator * (const Matrix& t) const {
Matrix ans;
for(int i = 0; i < K; i++)
for(int j = 0; j < K; j++) if(a[i][j])
for(int k = 0; k < K; k++)
add_mod(ans.a[i][k], mul_mod(a[i][j], t.a[j][k]));
return ans;
}
};
Matrix pow_mod(Matrix a, int n) {
Matrix ans;
for(int i = 0; i < K; i++) ans.a[i][i] = 1;
while(n) {
if(n & 1) ans = ans * a;
a = a * a;
n >>= 1;
}
return ans;
}
int main()
{
while(scanf("%d%d", &N, &K) == 2 && N) {
for(int i = 0; i < N; i++)
for(int j = 0; j < K; j++)
scanf("%d", &A[i][j]);
for(int i = 0; i < K; i++)
for(int j = 0; j < N; j++)
scanf("%d", &B[i][j]);
Matrix M;
for(int i = 0; i < K; i++)
for(int j = 0; j < N; j++) if(B[i][j])
for(int k = 0; k < K; k++)
add_mod(M.a[i][k], mul_mod(B[i][j], A[j][k]));
M = pow_mod(M, N * N - 1);
memset(tmp, 0, sizeof(tmp));
memset(res, 0, sizeof(res));
for(int i = 0; i < N; i++)
for(int j = 0; j < K; j++) if(A[i][j])
for(int k = 0; k < K; k++)
add_mod(tmp[i][k], mul_mod(A[i][j], M.a[j][k]));
for(int i = 0; i < N; i++)
for(int j = 0; j < K; j++) if(tmp[i][j])
for(int k = 0; k < N; k++)
add_mod(res[i][k], mul_mod(tmp[i][j], B[j][k]));
int ans = 0;
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++)
ans += res[i][j];
printf("%d\n", ans);
}
return 0;
}
HDU 4965 Fast Matrix Calculation 矩阵快速幂的更多相关文章
- hdu 4965 Fast Matrix Calculation(矩阵高速幂)
题目链接.hdu 4965 Fast Matrix Calculation 题目大意:给定两个矩阵A,B,分别为N*K和K*N. 矩阵C = A*B 矩阵M=CN∗N 将矩阵M中的全部元素取模6,得到 ...
- hdu4965 Fast Matrix Calculation 矩阵快速幂
One day, Alice and Bob felt bored again, Bob knows Alice is a girl who loves math and is just learni ...
- Fast Matrix Calculation 矩阵快速幂
One day, Alice and Bob felt bored again, Bob knows Alice is a girl who loves math and is just learni ...
- HDU 4965 Fast Matrix Calculation 矩阵乘法 乘法结合律
一种奇葩的写法,纪念一下当时的RE. #include <iostream> #include <cstdio> #include <cstring> #inclu ...
- HDU 4965 Fast Matrix Calculation(矩阵高速幂)
HDU 4965 Fast Matrix Calculation 题目链接 矩阵相乘为AxBxAxB...乘nn次.能够变成Ax(BxAxBxA...)xB,中间乘n n - 1次,这样中间的矩阵一个 ...
- HDU - 4965 Fast Matrix Calculation 【矩阵快速幂】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4965 题意 给出两个矩阵 一个A: n * k 一个B: k * n C = A * B M = (A ...
- hdu 4965 Fast Matrix Calculation
题目链接:hdu 4965,题目大意:给你一个 n*k 的矩阵 A 和一个 k*n 的矩阵 B,定义矩阵 C= A*B,然后矩阵 M= C^(n*n),矩阵中一切元素皆 mod 6,最后求出 M 中所 ...
- hdu 5667 BestCoder Round #80 矩阵快速幂
Sequence Accepts: 59 Submissions: 650 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- HDU4965 Fast Matrix Calculation —— 矩阵乘法、快速幂
题目链接:https://vjudge.net/problem/HDU-4965 Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Othe ...
随机推荐
- event——事件对象详解
PS:转自https://www.cnblogs.com/songyaqi/p/5204143.html 1. 事件对象 Event 对象代表事件的状态,比如事件在其中发生的元素.键盘按键的状态.鼠标 ...
- Appium基础五:appium相关API
1.获取信息类: 1.1 获取当前界面的组件: driver.currentActivity(); //获取当前界面的activity,可用于断言是否跳转到预期的activity 1.2 获取当前页面 ...
- Android 适配底部返回键等虚拟键盘的完美解决方案
这个问题来来回回困扰了我很久,一直没能妥善解决. 场景1:华为手机遮挡了屏幕底部. 场景2:进入应用时,虚拟键自动缩回,留下空白区域. 需求: 需要安卓能自适应底部虚拟按键,用户隐藏虚拟按键时应用要占 ...
- 【extjs6学习笔记】1.8 初始: ExtJS命名约定
Convention for Description Example Class 类名应该在CamelCase中 MyCustomClass 类名应包含字母数字字符. 如果属于技术术语,则允许使用数字 ...
- <转>Spring 知识点提炼
Spring 知识点提炼 1. Spring框架的作用 轻量:Spring是轻量级的,基本的版本大小为2MB 控制反转:Spring通过控制反转实现了松散耦合,对象们给出它们的依赖,而不是创建或查找依 ...
- Windows Azure 配置Active Directory 主机(4)
步骤 6:设置在启动时加入域的虚拟机 若要创建其他在首次启动时加入域的虚拟机,请打开 Windows Azure PowerShell ISE,粘贴以下脚本,将占位符替换为您自己的值并运行该脚本. 若 ...
- Python+selenium之selenium Grid2
利用selenium grid2 keyi可以在不同的主机上建立主节点(hub)和分支节点(node),可以使主节点上的测试用例在不同的分支节点上运行.对不同的节点来说,可以搭建不同的测试环境(操作系 ...
- SPOJ SORTBIT Sorted bit squence (数位DP,入门)
题意: 给出一个范围[m,n],按照二进制表示中的1的个数从小到大排序,若1的个数相同,则按照十进制大小排序.求排序后的第k个数.注意:m*n>=0. 思路: 也是看论文的.一开始也能想到是这种 ...
- java 访问docker的环境
1. 配置环境 新增 ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock root@ros ...
- VR/AR软件—Mirra测试(截至2017/11/13),使AR/VR创作更加便捷
Mirra(截至2017/11/13)https://www.mirra.co/ 1.主要特点: 目前仅支持VR,不支持AR 在浏览器(仅支持chrome,firefox)上进行创作,但目前不能直接在 ...