题目链接。hdu 4965 Fast Matrix Calculation

题目大意:给定两个矩阵A,B,分别为N*K和K*N。

  1. 矩阵C = A*B
  2. 矩阵M=CN∗N
  3. 将矩阵M中的全部元素取模6,得到新矩阵M‘
  4. 计算矩阵M’中全部元素的和

解题思路:由于矩阵C为N*N的矩阵,N最大为1000。就算用高速幂也超时,可是由于C = A*B, 所以CN∗N=ABAB…AB=AC′N∗N−1B,C‘
= B*A, 为K*K的矩阵,K最大为6。全然能够接受。

#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
const int maxn = 1005;
const int MOD = 6;
typedef int Mat[maxn][maxn]; int N, K;
Mat A, B, X, Y, tmp; void put (Mat x, int r, int c) {
for (int i = 0; i < K; i++) {
for (int j = 0; j < K; j++)
printf("%d ", x[i][j]);
printf("\n");
}
} void mul_mat (Mat ret, Mat a, Mat b, int r, int t, int c) {
memset(tmp, 0, sizeof(tmp)); for (int k = 0; k < t; k++) {
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
tmp[i][j] = (tmp[i][j] + a[i][k] * b[k][j]) % MOD;
}
memcpy(ret, tmp, sizeof(tmp));
} void pow_mat (Mat ret, Mat x, int n) {
memset(Y, 0, sizeof(Y));
for (int i = 0; i < K; i++)
Y[i][i] = 1; while (n) {
if (n&1)
mul_mat(Y, Y, x, K, K, K);
mul_mat(x, x, x, K, K, K);
n >>= 1;
}
memcpy(ret, Y, sizeof(Y));
} void init () {
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]);
} int main () {
while (scanf("%d%d", &N, &K) == 2 && N + K) {
init(); mul_mat(X, B, A, K, N, K); pow_mat(X, X, N*N-1); mul_mat(X, A, X, N, K, K);
mul_mat(X, X, B, N, K, N); int ans = 0;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
ans += X[i][j];
printf("%d\n", ans);
}
return 0;
}

hdu 4965 Fast Matrix Calculation(矩阵高速幂)的更多相关文章

  1. HDU 4965 Fast Matrix Calculation 矩阵快速幂

    题意: 给出一个\(n \times k\)的矩阵\(A\)和一个\(k \times n\)的矩阵\(B\),其中\(4 \leq N \leq 1000, \, 2 \leq K \leq 6\) ...

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

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

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

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

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

  5. hdu 4965 Fast Matrix Calculation

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

  7. hdu 5015 233 Matrix (矩阵高速幂)

    233 Matrix Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tota ...

  8. Fast Matrix Calculation 矩阵快速幂

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

  9. HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和)

    HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 1588 Gauss Fibonacci 题意:  g(i)=k*i+b;i为变量.  给出 ...

随机推荐

  1. 黄聪:Microsoft Enterprise Library 5.0 系列教程(一) : Caching Application Block (初级)

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(一) : Caching Application Block (初级) 本篇文章具体官方解释请参照以下链接: h ...

  2. 树形dp专辑

    hdu 2196 http://acm.hdu.edu.cn/showproblem.php?pid=2196 input 5//5个结点 1 1//表示结点2到结点1有一条权值为1的边 2 1//表 ...

  3. Iphone CodeSign error: no provisioning profile at path 错

    今天在退房手续,变化了profile,当再次进行整理编译项目时.就出现了这个问题,在xCode 6 开发环境下出现了, 出现这个错误提示Code Sign error: Provisioning pr ...

  4. Autofac 入门

    Autofac 入门文档 原文链接:http://docs.autofac.org/en/latest/getting-started/index.html 在程序中使用Autofac的基本模式是: ...

  5. 打开 chm 帮助文件显示空白及解决方法

    有个很奇葩的解决方法:把 chm 文件用压缩软件压缩,然后用压缩软打开此压缩包,直接双击压缩包里面的 chm 文件 这虽然解决了问题,但是这不科学…… 分析:直接打开压缩包里面的文件,压缩包的文件是临 ...

  6. [WebGL入门]二十四,补色着色

    注:文章译自http://wgld.org/,原作者杉本雅広(doxas),文章中假设有我的额外说明,我会加上[lufy:],另外,鄙人webgl研究还不够深入,一些专业词语,假设翻译有误,欢迎大家指 ...

  7. JavaEE SSH集成框架(两) struts2 本地加载dtd文件,action组态

    1. 载入中struts2的dtd文件.使struts.xml网络无法验证,和eclipse有技巧 在src在创建struts.xml: <? xmlversion="1.0" ...

  8. org.apache.jasper.JasperException: An exception occurred processing JSP page /admin/jiaoshi/daochuEx

    org.apache.jasper.JasperException: An exception occurred processing JSP page /admin/jiaoshi/daochuEx ...

  9. Asp.NET MVC3 使用 SignalR 实现推(持续)

    一,Persistent Connection 演示示例教程 1.实现server端代码 1),编写server PersistentConnection 代码 项目中 SignalR 文件夹下创建 ...

  10. Directx11学习笔记【五】 基本的数学知识----向量篇

    本文参考dx11龙书 Chapter1 vector algebra(向量代数) 要想学好游戏编程,扎实的数学知识是尤为重要的,下面将对dx11龙书中有关向量的数学知识做一下总结. 在数学中,几何向量 ...