题意:

给出一个\(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 矩阵快速幂的更多相关文章

  1. hdu 4965 Fast Matrix Calculation(矩阵高速幂)

    题目链接.hdu 4965 Fast Matrix Calculation 题目大意:给定两个矩阵A,B,分别为N*K和K*N. 矩阵C = A*B 矩阵M=CN∗N 将矩阵M中的全部元素取模6,得到 ...

  2. 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 ...

  3. Fast Matrix Calculation 矩阵快速幂

    One day, Alice and Bob felt bored again, Bob knows Alice is a girl who loves math and is just learni ...

  4. HDU 4965 Fast Matrix Calculation 矩阵乘法 乘法结合律

    一种奇葩的写法,纪念一下当时的RE. #include <iostream> #include <cstdio> #include <cstring> #inclu ...

  5. HDU 4965 Fast Matrix Calculation(矩阵高速幂)

    HDU 4965 Fast Matrix Calculation 题目链接 矩阵相乘为AxBxAxB...乘nn次.能够变成Ax(BxAxBxA...)xB,中间乘n n - 1次,这样中间的矩阵一个 ...

  6. 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 ...

  7. hdu 4965 Fast Matrix Calculation

    题目链接:hdu 4965,题目大意:给你一个 n*k 的矩阵 A 和一个 k*n 的矩阵 B,定义矩阵 C= A*B,然后矩阵 M= C^(n*n),矩阵中一切元素皆 mod 6,最后求出 M 中所 ...

  8. hdu 5667 BestCoder Round #80 矩阵快速幂

    Sequence  Accepts: 59  Submissions: 650  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536 ...

  9. HDU4965 Fast Matrix Calculation —— 矩阵乘法、快速幂

    题目链接:https://vjudge.net/problem/HDU-4965 Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Othe ...

随机推荐

  1. 在MasterPage中检验session是否存在~

    在母板頁中檢查user是否登入過,這樣就不用在每個頁中去作檢驗.在其Init事件中寫入如下代碼:     protected void ContentPlaceHolder1_Init(object  ...

  2. ORACLE行转列通用过程(转)

    1.使用视图 SQL code? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 create or r ...

  3. codevs 4888 零件分组

    4888 零件分组  时间限制: 1 s  空间限制: 16000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 现有一些棍状零件,每个零件都有 ...

  4. linux配置tomcat已service方式启动

    1. 在/etc/init.d目录下新建文件,命名为tomcat2. 对tomcat文件进行编辑,执行 # cd /etc/init.d/ # vi tomcat 将下面代码粘上去 注意:下面代码ja ...

  5. xcode在代码中查找中文

    总是忘记xcode中查找中文,这次记下来,以后就不会忘记了,哈哈 请看下图: 切换到查找,点击find后面的text,选择Regular Expression,然后输入 1. 查找非ascii的字符 ...

  6. hdu 3861 The King’s Problem

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  7. Netbackup驱动器常用命令vmoprcmd

    1.vmoprcmd vmoprcmd – 对驱动器执行操作员功能 大纲 vmoprcmd -devmon [pr | ds | hs] [-h device_host] default_operat ...

  8. 四、filter和find函数的区别

    filter(): filter函数会返回data中为true那项的数组(即查询符合条件的数据) eg:data.filter((f)=>{ if(f[name]===item[name]){ ...

  9. Windows环境下使用Apache+mod

    1.安装Python和Apache. 2.安装mod_wsgi后获得wsgi.so,并将wsgi.so放到Apache的modules文件夹下. 3.安装webpy. 4.打开httpd.conf(在 ...

  10. 在DataGridView控件中显示下拉列表

    实现效果: 知识运用: DataGridViewComboBoxColumn类 //通过该类可以创建下拉列表样式的列 实现代码: private void Form1_Load(object send ...