题目链接。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. javascript UniqueID属性

        在Web页中的每一个HTML元素都一个ID属性,ID作为其标示,在我们的普通理解中它应该是unique的.但是HTML元素的ID属性是可写的,这就造成了我们非常可能人为的使ID的反复.按么假设 ...

  2. 百度经纬度和google经纬度互转

    原文:百度经纬度和google经纬度互转 百度地图的坐标转换,由于百度地图在GCJ02协议的基础上又做了一次处理,变为 BD09协议的坐标,以下是坐标的转化方式,可以方便和其他平台转化 private ...

  3. MVC区域 vs2013 mvc 搭建基架项

    http://www.it165.net/pro/html/201404/12822.html

  4. SIP基本呼叫

    我们首先来看下主要的呼叫流程. INVITEsip:69690067@beijing.chinamobile.com;user=phone SIP/2.0 From:"+8610696900 ...

  5. JAVA: httpclient 具体解释——第五章;

    httpclient 具体解释--第一章: httpclient 具体解释--第二章: httpclient 具体解释--第三章: httpclient 具体解释--第四章: httpclient 具 ...

  6. Javadoc的Html文件传输chm

     Javadoc的Html文件转chm 工具下载地址:http://msdn.microsoft.com/en-us/library/ms669985.aspx 两篇相关文章: MyEclipse ...

  7. WPF中对三维模型的控制

    原文:WPF中对三维模型的控制 (以下选自南开大学出版社出版的<WPF和Silverlight教程>) 3Dmax中的建模模型可以导出为obj文件格式,将此文件导入WPF项目中,由WPF完 ...

  8. 关于js中window.location.href,location.href,parent.location.href,top.location.href的使用方法

    关于js中"window.location.href"."location.href"."parent.location.href".&qu ...

  9. Android Splash界面支持用户点击 直接进入主界面

    转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/23613403 现在大部分APP都有Splash界面,下面列一下Splash页面的 ...

  10. 乐在其中设计模式(C#) - 代理模式(Proxy Pattern)

    原文:乐在其中设计模式(C#) - 代理模式(Proxy Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 代理模式(Proxy Pattern) 作者:webabcd 介绍 为 ...