题意:

给出一个\(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. P4876 近似排列计数50

    时间限制:1s 内存限制:256MB [问题描述] 对于一个1-n的排列,如果满足第i个数|ai-i|<=k,则称该排列为K-近似排列. 现在排列的若干位置已经确定,你需要计算剩下的数有多少种排 ...

  2. MySQL存储过程多条修改语句

    DROP procedure Sel_Function_ActivityPastDueDELIMITER $$DROP procedure IF EXISTS`shouyi`.`Sel_Functio ...

  3. swift3.0 项目引导页

    项目引导页并不难,使用 UICollectionView就可以完成, 1.首先获取应用程序的版本号,并存入本地,每次有新版本号,和存入本地的版本号,相比较 fileprivate func setup ...

  4. css3的animation动画

    animation 设置对象的动画特效   有6个主要的值 animation-name   动画名称 animation-duration   动画持续时间 animation-timing-fun ...

  5. [拾零]C/C++_代码复用的实现_静态链接库_动态链接库_使用.def导出

    1 静态链接库 1.1 创建静态链接库: 1.在VC6中创建项目:Win32 Static Library 2.在项目中创建两个文件:xxx.h 和 xxx.cpp 3.编译 1.2 使用静态链接库 ...

  6. LeetCode Min Stack 最小值栈

    题意:实现栈的四个基本功能.要求:在get最小元素值时,复杂度O(1). 思路:链表直接实现.最快竟然还要61ms,醉了. class MinStack { public: MinStack(){ h ...

  7. Python-OpenCV——亮度和对比度

    亮度与对比度 亮度调整是将图像像素的强度整体变大/变小,对比度调整指的是图像暗处变得更暗,亮出变得更亮,从而拓宽某个区域内的显示精度. OpenCV中亮度和对比度应用这个公式来计算:g(x) = αf ...

  8. Python-OpenCV中的filter2D()函数

    使用自定义内核对图像进行卷积.该功能将任意线性滤波器应用于图像.支持就地操作.当光圈部分位于图像外部时,该功能会根据指定的边框模式插入异常像素值. 语法 函数原型: dst=cv.filter2D(s ...

  9. 简易数据分析 02 | Web Scraper 的下载与安装

    这是简易数据分析系列的第 2 篇文章. 上篇说了数据分析在生活中的重要性,从这篇开始,我们就要进入分析的实战内容了.数据分析数据分析,没有数据怎么分析?所以我们首先要学会采集数据. 我调研了很多采集数 ...

  10. Dojo操作dom元素的样式

    1.使用dom-style的set方法,可以直接设置dom元素的样式属性,这和使用dom元素的style属性效果一样. 2.使用dom-class的replace方法可以替换某个dom元素的样式,ad ...