HDU 4965 Fast Matrix Calculation(矩阵高速幂)
HDU 4965 Fast Matrix Calculation
矩阵相乘为AxBxAxB...乘nn次。能够变成Ax(BxAxBxA...)xB,中间乘n
n - 1次,这样中间的矩阵一个仅仅有6x6。就能够用矩阵高速幂搞了
代码:
#include <cstdio>
#include <cstring> const int N = 1005;
const int M = 10;
int n, m; int A[N][M], B[M][N], C[M][M], CC[N][N];
int ans[M][M]; void tra() {
memset(CC, 0, sizeof(CC));
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
CC[i][j] = 0;
for (int k = 0; k < m; k++) {
CC[i][j] = (CC[i][j] + C[i][k] * C[k][j]) % 6;
}
}
}
for (int i = 0; i < m; i++)
for (int j = 0; j < m; j++)
C[i][j] = CC[i][j];
} void mul() {
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
CC[i][j] = 0;
for (int k = 0; k < m; k++) {
CC[i][j] = (CC[i][j] + ans[i][k] * C[k][j]) % 6;
}
}
}
for (int i = 0; i < m; i++)
for (int j = 0; j < m; j++)
ans[i][j] = CC[i][j];
} void pow_mod(int k) {
memset(ans, 0, sizeof(ans));
for (int i = 0; i < m; i++)
ans[i][i] = 1;
while (k) {
if (k&1) mul();
tra();
k >>= 1;
}
} void init() {
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
scanf("%d", &A[i][j]);
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
scanf("%d", &B[i][j]);
} int solve() {
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
C[i][j] = 0;
for (int k = 0; k < n; k++) {
C[i][j] = (C[i][j] + B[i][k] * A[k][j]) % 6;
}
}
} pow_mod(n * n - 1); for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
C[i][j] = ans[i][j];
}
} for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
CC[i][j] = 0;
for (int k = 0; k < m; k++) {
CC[i][j] = (CC[i][j] + A[i][k] * C[k][j]) % 6;
}
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
A[i][j] = CC[i][j];
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int sum = 0;
for (int k = 0; k < m; k++) {
sum = (sum + A[i][k] * B[k][j]) % 6;
}
ans += sum;
}
}
return ans;
} int main() {
while (~scanf("%d%d", &n, &m) && n || m) {
init();
printf("%d\n", solve());
}
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,得到 ...
- HDU 4965 Fast Matrix Calculation 矩阵快速幂
题意: 给出一个\(n \times k\)的矩阵\(A\)和一个\(k \times n\)的矩阵\(B\),其中\(4 \leq N \leq 1000, \, 2 \leq K \leq 6\) ...
- HDU 4965 Fast Matrix Calculation 矩阵乘法 乘法结合律
一种奇葩的写法,纪念一下当时的RE. #include <iostream> #include <cstdio> #include <cstring> #inclu ...
- 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 中所 ...
- 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 ...
- hdu 5015 233 Matrix (矩阵高速幂)
233 Matrix Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tota ...
- 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 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和)
HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 1588 Gauss Fibonacci 题意: g(i)=k*i+b;i为变量. 给出 ...
随机推荐
- [luogu] P4155 [SCOI2015]国旗计划(贪心)
P4155 [SCOI2015]国旗计划 题目描述 A 国正在开展一项伟大的计划 -- 国旗计划.这项计划的内容是边防战士手举国旗环绕边境线奔袭一圈.这项计划需要多名边防战士以接力的形式共同完成,为此 ...
- js实现导航固定定位
js实现导航固定定位 <!DOCTY ...
- tp框架报错 Namespace declaration statement has to be the very first statement in the script
Namespace declaration statement has to be the very first statement in the script tp框架报这个错误,错误行数就是nam ...
- Sublime Text 3 Package Control安装
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50618314 安装好Sublime T ...
- POJ——T2186 Popular Cows || 洛谷——P2341 [HAOI2006]受欢迎的牛
http://poj.org/problem?id=2186 || https://www.luogu.org/problem/show?pid=2341 Time Limit: 2000MS M ...
- Leetcode--easy系列4
#58 Length of Last Word Given a string s consists of upper/lower-case alphabets and empty space char ...
- CCFlow的excel数据源导入Dtl明细表的操作方法以及模版demo
CCBPM支持通过excel向Dtl明细表(从表)导入数据. 以下,我们以cc的財务报销单demo流程解说详细的操作步骤和模版设计. 导入的操纵步骤: 1.流程发起后,在開始节点导入数据源,点击明细表 ...
- Android 多分辨率自适应总结
这周的工作对Android项目多分辨率自适应进行调整.故对这方面知识进行不断的尝试学习.Android项目刚開始做的时候一定养成编程习惯,全部资源调用放在value中.统一命名以及管理.总结了下面内容 ...
- node--19 moogose demo1
db.js /** * Created by Danny on 2015/9/28 16:44. */ //引包 var mongoose = require('mongoose'); //创建数据库 ...
- jar 包的认识与处理、jar 文件 war 文件以及 ear 文件
1. jar 包 将 jar 包解压,其实是该类(.java)编译好的(.class)文件. 包路径 package 多层嵌套的 packages META-INF 文件夹 2. 常用 jar 包及其 ...