hdu 4920 Matrix multiplication(矩阵相乘)2014多校训练第5场
来源:互联网 发布:python form import 编辑:程序博客网 时间:2024/04/27 20:50
Matrix multiplication
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Problem Description
Given two matrices A and B of size n×n, find the product of them.
bobo hates big integers. So you are only asked to find the result modulo 3.
bobo hates big integers. So you are only asked to find the result modulo 3.
Input
The input consists of several tests. For each tests:
The first line contains n (1≤n≤800). Each of the following n lines contain n integers -- the description of the matrix A. The j-th integer in the i-th line equals Aij. The next n lines describe the matrix B in similar format (0≤Aij,Bij≤109).
The first line contains n (1≤n≤800). Each of the following n lines contain n integers -- the description of the matrix A. The j-th integer in the i-th line equals Aij. The next n lines describe the matrix B in similar format (0≤Aij,Bij≤109).
Output
For each tests:
Print n lines. Each of them contain n integers -- the matrix A×B in similar format.
Print n lines. Each of them contain n integers -- the matrix A×B in similar format.
Sample Input
10120 12 34 56 7
Sample Output
00 12 1
题意:给出两个n*n的矩阵,求这两个矩阵的乘积,结果对3取余。
分析:拿到题先用了经典的矩阵相乘的方法,提交以后果断超时了。后来在网上搜了一下矩阵相乘优化,找到了一个优化方法,只可惜现在我还没有理解是怎么优化的。
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N = 805;int a[N][N], b[N][N], ans[N][N];void Multi(int n){ int i, j, k, L, *p2; int tmp[N], con; for(i = 0; i < n; ++i) { memset(tmp, 0, sizeof(tmp)); for(k = 0, L = (n & ~15); k < L; ++k) { con = a[i][k]; for(j = 0, p2 = b[k]; j < n; ++j, ++p2) tmp[j] += con * (*p2); if((k & 15) == 15) { for(j = 0; j < n; ++j) tmp[j] %= 3; } } for( ; k < n; ++k) { con = a[i][k]; for(j = 0, p2 = b[k]; j < n; ++j, ++p2) tmp[j] += con * (*p2); } for(j = 0; j < n; ++j) ans[i][j] = tmp[j] % 3; }}int main(){ int n, i, j, k; while(~scanf("%d",&n)) { for(i = 0; i < n; i++) for(j = 0; j < n; j++) { scanf("%d",&a[i][j]); a[i][j] %= 3; } for(i = 0; i < n; i++) for(j = 0; j < n; j++) { scanf("%d",&b[i][j]); b[i][j] %= 3; } Multi(n); for(i = 0; i < n; i++) { for(j = 0; j < n-1; j++) printf("%d ", ans[i][j]); printf("%d\n", ans[i][n-1]); } } return 0;}
http://blog.csdn.net/gogdizzy/article/details/9003369这里面讲解了矩阵相乘的优化方法。
下面这种方法也可以过:
#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;const int N = 805;int a[N][N], b[N][N], ans[N][N];int main(){ int n, i, j, k; while(~scanf("%d",&n)) { for(i = 1; i <= n; i++) for(j = 1; j <= n; j++) { scanf("%d",&a[i][j]); a[i][j] %= 3; } for(i = 1; i <= n; i++) for(j = 1; j <= n; j++) { scanf("%d",&b[i][j]); b[i][j] %= 3; } memset(ans, 0, sizeof(ans)); for(k = 1; k <= n; k++) //经典算法中这层循环在最内层,放最内层会超时,但是放在最外层或者中间都不会超时,不知道为什么 for(i = 1; i <= n; i++) for(j = 1; j <= n; j++) { ans[i][j] += a[i][k] * b[k][j]; //ans[i][j] %= 3; //如果在这里对3取余,就超时了 } for(i = 1; i <= n; i++) { for(j = 1; j < n; j++) printf("%d ", ans[i][j] % 3); printf("%d\n", ans[i][n] % 3); } } return 0;}
1 0
- hdu 4920 Matrix multiplication(矩阵相乘)2014多校训练第5场
- hdu 4920 Matrix multiplication(多校第5场,矩阵相乘)
- hdu 4965 Fast Matrix Calculation(矩阵快速幂)2014多校训练第9场
- hdu 4920 Matrix multiplication (矩阵相乘)
- 【HDU】4920 - Matrix multiplication(矩阵相乘)
- HDU 4920 Matrix multiplication(矩阵相乘)
- hdu 4911 Inversion(归并排序求逆序对数)2014多校训练第5场
- hdu 4915 Parenthese sequence(模拟)2014多校训练第5场
- hdoj Matrix multiplication 4920 (矩阵相乘)
- HDU 4920(杭电多校训练#5 1010 题) Matrix multiplication(不知道该挂个什么帽子。。。)
- 2014多校训练第5场1005题 ||hdu 4915 模拟
- hdu 4902 Nice boat(2014多校训练第4场1006)(暴力写法)
- hdu 4901 The Romantic Hero(计数dp)2014多校训练第4场1005
- hdu 4901 The Romantic Hero(计数dp)2014多校训练第4场1005
- hdu 4927 Series 1(高精度) 2014多校训练第6场
- hdu 4923 Room and Moor(数学题)2014多校训练第6场
- hdu 4939 Stupid Tower Defense(DP)2014多校训练第7场
- hdu 4937 Lucky Number(数学题 进制转换)2014多校训练第7场
- 备份及恢复EAS测试服务器数据库
- 浏览器和netbean调试详解
- C++四种强制类型转换运算符
- 百度定位
- dom.style.height, offsetWidth, scrollWidth, clientWidth区别
- hdu 4920 Matrix multiplication(矩阵相乘)2014多校训练第5场
- HDU 1686 Oulipo (KMP)
- The House Of Santa Claus(dfs)
- Linux中tar的用法总结
- hdu 1106 排序 20140805.cpp
- 计算CRC16
- TCP与UDP在socket编程中的区别
- [Linux]Makefile入门
- Transfer Learning 资料