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 ...
随机推荐
- 基础——(5)D Flip-Flop(D触发器)
之前搞了一个 D-Latch,看一下下图是怎么变化的 In D-latch anytime its enabled the input D is going to be output at Q 使用c ...
- UVALive-7198 Tall orders 微积分 二分
题目链接:https://cn.vjudge.net/problem/UVALive-7198 题意 有悬链线方程$ f(x)=a \cdot cosh(\frac{s}{a}) $, 现有两个电线杆 ...
- 小学生都能学会的python(运算符 和 while循环)
---恢复内容开始--- 小学生都能学会的python(运算符和编码) 一.格式化输出 #占位:"%s"占位,占得是字符串,"%d"占位,占的是数字. # 让用 ...
- poj 3420 Quad Tiling (状压dp+多米诺骨牌问题+矩阵快速幂)
还有这种操作?????? 直接用pre到now转移的方式构造一个矩阵就好了. 二进制长度为m,就构造一个长度为1 << m的矩阵 最后输出ans[(1 << m) - 1][( ...
- ubuntu/wireshark --Lua: Error during loading: [string "/usr/share/wireshark/init.lua"]:45问题解决
错误如下: 解决方案:修改init.lua 直接运行wireshark的话会报错: Lua: Error during loading:[string "/usr/share/wiresha ...
- static和extern的作用域--题目
#include <stdio.h> ; int main(void) { , sum = , count = ; ,count++) // count = 2 { ; count++; ...
- Linux 0.11中write实现
看了一下Linux 0.11版本号write的实现,首先它在标准头文件unistd.h中有定义 int write(int fildes, const char * buf, off_t count) ...
- Java多线程之~~~线程安全容器的非堵塞容器
在并发编程中,会常常遇到使用容器.可是假设一个容器不是线程安全的.那么他在多线程的插入或者删除的过程 中就会出现各种问题.就是不同步的问题.所以JDK提供了线程安全的容器,他能保证容器在多线程的情况下 ...
- hdu-3401-Trade-单调队列优化的DP
单调队列入门题... dp[i][j]:第i天.手中拥有j个股票时,获得的最大利润. 若第i天不买不卖:dp[i][j]=max(dp[i][j],dp[i-1][j]); 若第i天买 ...
- [SICP] 求值规则
在Java语言学习中,通常不太关注求值规则. (2+4*6)*(3+5+7)这样的组合式的求值规则.通常归结为优先级问题: if.for等的求值规则通常归结为语义. 函数式编程语言的Scheme,将这 ...