HDU 4965 Fast Matrix Calculation 矩阵乘法 乘法结合律
一种奇葩的写法,纪念一下当时的RE。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <cctype>
#include <time.h> using namespace std; const int INF = <<;
const int MAXN = ; struct Matrix {
int a[MAXN][MAXN];
int col, row;
}; Matrix tMu; void multiplication(const Matrix &x, const Matrix &y) {
tMu.row= x.row; tMu.col = y.col;
for (int i = ; i < tMu.row; i++)
for (int j = ; j < tMu.row; j++) {
tMu.a[i][j] = ;
for (int k = ; k < x.col; k++)
tMu.a[i][j] = (tMu.a[i][j]+x.a[i][k]*y.a[k][j])%;
}
} Matrix a, b, ONE;
Matrix t;
Matrix tmp, res; int n, k; void pow(const Matrix &x, int d) {
res = ONE; res.col = res.row = x.col;
tmp = x;
while (d>) {
if (d&) {
multiplication(res, tmp);
res = tMu;
}
multiplication(tmp, tmp);
tmp = tMu;
d >>= ;
}
} void solve() {
multiplication(b, a);
t = tMu;
pow(t, n*n-);
t = res;
multiplication(a, t);
t = tMu;
multiplication(t, b);
t = tMu;
int ans = ;
for (int i = ; i < n; i++) {
for (int j = ; j < n; j++)
ans += t.a[i][j];
}
printf("%d\n", ans);
} int main() {
#ifdef Phantom01
freopen("HDU4965.txt", "r", stdin);
#endif //Phantom01 for (int i = ; i < MAXN; i++) {
for (int j = ; j < MAXN; j++)
ONE.a[i][j] = ;
ONE.a[i][i] = ;
} while (scanf("%d%d", &n, &k)!=EOF && !(n==&&k==)) {
a.row = b.col = n;
a.col = b.row = k;
for (int i = ; i < n; i++)
for (int j = ; j < k; j++)
scanf("%d", &a.a[i][j]);
for (int i = ; i < k; i++)
for (int j = ; j < n; j++)
scanf("%d", &b.a[i][j]);
solve();
} return ;
}
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(矩阵高速幂)
HDU 4965 Fast Matrix Calculation 题目链接 矩阵相乘为AxBxAxB...乘nn次.能够变成Ax(BxAxBxA...)xB,中间乘n n - 1次,这样中间的矩阵一个 ...
- HDU4965 Fast Matrix Calculation —— 矩阵乘法、快速幂
题目链接:https://vjudge.net/problem/HDU-4965 Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Othe ...
- hdu 4965 Fast Matrix Calculation
题目链接:hdu 4965,题目大意:给你一个 n*k 的矩阵 A 和一个 k*n 的矩阵 B,定义矩阵 C= A*B,然后矩阵 M= C^(n*n),矩阵中一切元素皆 mod 6,最后求出 M 中所 ...
- 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 ...
- 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 ...
- hdu4965 Fast Matrix Calculation (矩阵快速幂 结合律
http://acm.hdu.edu.cn/showproblem.php?pid=4965 2014 Multi-University Training Contest 9 1006 Fast Ma ...
随机推荐
- JSP获取json格式的数据报错 Uncaught SyntaxError: Unexpected identifier
后台json字符串是 {"id":"cmdb_ci.`fully_qualified_domain_name`","field":" ...
- 1、Attention_based Group recommendation——基于注意力机制的群组推荐
1.摘要: 本文将Attention-based模型和BPR模型结合对给定的群组进行推荐项目列表. 2.算法思想: 如图: attention-based model:[以下仅计算一个群组的偏好,多个 ...
- 编写高性能的javascript代码(持续更新)
参考资料: Vanilla JS——世界上最轻量的JavaScript框架(没有之一) http://segmentfault.com/a/1190000000355277 探索高效jQuery的奥秘 ...
- hdu5791 TWO
hdu5791 TWO 题意 给你两个数串 问你两个数串有多少子串一致 子串不一定是连续的 解法 我们设 \(dp[i][j]\) 表示A串匹配到 i 位,B串匹配到 j 位,一致的子串数.那么我们有 ...
- java实现组合数_n!_杨辉三角_组合数递推公式_回文数_汉诺塔问题
一,使用计算机计算组合数 1,设计思想 (1)使用组合数公式利用n!来计算Cn^k=n!/k!(n-k)!用递推计算阶乘 (2)使用递推的方法用杨辉三角计算Cn+1^k=Cn^k-1+Cn^k 通过数 ...
- python 多列表对应的位置的值形成一个新的列表
list1 = [1, 2, 3, 4, 5] list2 = ['a','b', 'c', 'd', 'e'] list3 = [1, 2, 3, 4, 5] multi_list = map(li ...
- poj 2288 Islands and Bridges (状压dp+Tsp问题)
这道题千辛万苦啊! 这道题要涉及到当前点和前面两个点,那就设dp[state][i][j]为当前状态为state,当前点为i,前一个点为j 这个状态表示和之前做炮兵那题很像,就是涉及到三个点时,就多设 ...
- 【BZOJ 1221】 [HNOI2001] 软件开发
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] /* 设一个超级源点S和超级汇点T S和2*i-1各连一条容量为ni的边. 花费为0 表示每天都会产生ni条要洗的毛巾 S和2*i各 ...
- C++11新特性应用--介绍几个新增的便利算法(用于分区的几个算法)
今天继续. C++11新增的关于Non-modifying sequence operations和Modifying sequence operations的算法已经写了.具体信息见之前的博客. 以 ...
- Matlab pchiptx
function v = pchiptx(x,y,u) %PCHIPTX Textbook piecewise cubic Hermite interpolation. % v = pchiptx(x ...