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 ...
随机推荐
- (五)我的JavaScript系列:JavaScript的糟粕
泪眼问花花不语,乱红飞过秋千去. JavaScript的糟粕 JavaScript语言是一门集精华与糟粕于一体的语言.在JavaScript: the good parts中,便集中讨论了关于精华与糟 ...
- 简单案列完美搞定Mvc设计模式
一个小列子搞定Mvc模式,包括数据库以及如何提高用户体验度 1.首先来web.xml配置servlet的访问路径: <?xml version="1.0" encoding= ...
- asp页面无法访问,可尝试开始SQL Server等服务
存在问题 asp页面的英文提示,翻译后为: "一个错误发生在服务器在处理URL.请联系系统管理员(管理人).如果您是系统管理员,请点击这里了解更多关于这个错误." 解决方案 请 ...
- 微信 Android版隐藏功能代码
- SAP标准培训课程C4C10学习笔记(二)第二单元
第二单元目录: SAP Cloud for Customer的项目实施分为4个阶段: 这四个阶段的详细介绍在SAP社区上这篇博文里: SAP Hybrid Project implementation ...
- UVA 10891 Game of Sum (决策优化)
这是一个零和博弈,最高得分只和序列以及谁先手有关. d[i][j],表示i到j的序列当前取的这个人的最高得分,转移以后状态是新的区间和另一个人取,从中取最小值. 决策的最小值也可递推. #includ ...
- UVA 11491 Erasing and Winning 奖品的价值 (贪心)
题意:给你一个n位整数,让你删掉d个数字,剩下的数字要尽量大. 题解:因为最后数字位数是确定的,而且低位数字对答案的贡献是一定不及高位数字的,所以优先选择选最大且最靠左边的数字,但是有一个限制,选完这 ...
- 苹果市值破万亿,iPhone 会涨价吗?
今日导读 苹果教父乔布斯曾经说过:“活着就是为了改变世界.”虽然他在 56 岁时就遗憾离世,但他极具创新和变革的精神早已深埋进苹果公司的企业文化里,影响着一代又一代的人.就在最近,这家一直努力“改变世 ...
- WINDOWS-API:关于线程CreateThread,_beginthead(_beginthreadex),AfxBeginThread
[转]windows多线程编程CreateThread,_beginthead(_beginthreadex)和AfxBeginThread的区别 在Windows的多线程编程中,创建线程的函数主要有 ...
- 01_5_删除指定id的单个对象
01_5_删除指定id的单个对象 1. 配置相应的映射文件内容 <delete id="deleteStudent" parameterClass="int&quo ...