Hdu4920Matrix multiplication(矩阵乘法)
来源:互联网 发布:纵横软件好用吗 编辑:程序博客网 时间:2024/05/16 17:05
Matrix multiplication
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1839 Accepted Submission(s): 818
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的矩阵相乘。
直接相乘将第二个矩阵进行倒置。b[i][j]<->b[j][i]。另一种方法是将a的一个数进行判断,如果等于0则不进行运算,否则则进行运算。还有,奇怪的是用G++超时,C++和C都AC。至今不懂为什么会这样。
矩阵相乘优化策略:
矩阵转置:矩阵相乘是矩阵A的一行数据元素与矩阵B的一列的数据元素对应相乘,然后再对其积求和,由于C与C++的二维数组是以行为主序存储的,因此矩阵A的行数据元素是连续存储的,而矩阵B的列数据元素是不连续存储的(N*1的矩阵除外),为了在矩阵相乘时对矩阵B也连续读取数据,根据局部性原理对矩阵B进行转置,矩阵转置是数据元素B[i][j]与B[j][i]进行交换,矩阵转置后,矩阵相乘是矩阵A的一行数据元素与转置后的矩阵B的一行的数据元素对应相乘,然后再对其积求和,这个和就是矩阵C的一个数据元素。
参考资料http://wenku.baidu.com/linkurl=261XeEzHAZkFGPiN63t1nnojoQF50yiuMoviHroGjVXjjRlxFcvWLcws0jgQcmZo4oA9BJcjnPxVreWRu-XXa9zb6r5gUUTxmBXn_qWSsu&qq-pf-to=pcqq.group
#include <stdio.h>#define LL __int64int a[900][900],b[900][900],c[900][900];int main(){ int n,i,j,k; while (~scanf("%d",&n)) { int x; for (i=0;i<n;i++) for (j=0;j<n;j++) { scanf("%d",&x); a[i][j]=x%3; } for (i=0;i<n;i++) for (j=0;j<n;j++) { scanf("%d",&x); b[j][i]=x%3; } for (i=0;i<n;i++) { for (j=0;j<n;j++) { c[i][j]=0; for (k=0;k<n;k++) c[i][j]=c[i][j]+a[i][k]*b[j][k]; printf(j==0?"%d":" %d",c[i][j]%3); } printf("\n"); } } return 0;}
#include <stdio.h>#define LL __int64int a[810][810],b[810][810],c[810][810],n,i,j,k,x;int main(){while (~scanf("%d",&n)){for (i=0;i<n;i++)for (j=0;j<n;j++){scanf("%d",&x);a[i][j]=x % 3;}for (i=0;i<n;i++)for (j=0;j<n;j++){scanf("%d",&x);b[j][i]=x % 3;}for (i=0;i<n;i++){for (j=0;j<n;j++){c[i][j]=0;for (k=0;k<n;k++)c[i][j]=c[i][j]+a[i][k]*b[j][k];printf(j==0?"%d":" %d",c[i][j]%3);}printf("\n");}}return 0;}
0 0
- Hdu4920Matrix multiplication(矩阵乘法)
- hdu4920Matrix multiplication (矩阵,bitset)
- hdu4920Matrix multiplication(模3矩阵相乘)
- HDU 4920 Matrix multiplication(矩阵乘法)
- hdu 4920 Matrix multiplication(矩阵乘法)
- hdu4920 Matrix multiplication [矩阵乘法 压位 ]
- HDU 4920 Matrix multiplication(矩阵乘法)
- sgu196:Matrix Multiplication(矩阵乘法-->图)
- SGU - 196 - Matrix Multiplication (矩阵乘法)
- ACdream 1213 Matrix Multiplication(矩阵乘法)
- HDU 4920(Matrix multiplication-矩阵乘法优化)
- POJ3318 Matrix Multiplication ( 随机化 + 矩阵乘法)
- ZOJ 1276 Optimal Array Multiplication Sequence(矩阵链乘法)
- 【4920Matrix multiplication】矩阵乘法优化+输入挂
- Hdu 4920 Matrix multiplication[矩阵乘法的时间复杂度优化]
- poj 1651 Multiplication Puzzle(区间dp 矩阵链乘法)
- 矩阵链乘法问题描述(Matrix-chain multiplication)
- 向量、矩阵乘法的几何意义(二) 矩阵乘法(Matrix Multiplication)
- The user specified as a definer ('XXXXX'@'%') does not exist
- 撒的发生的广东省房的所发生的
- 学习小结
- 解决LR无法启动IE以及WIN7/WIN8无法运行场景
- Cocos2d-x 屏幕适配新解
- Hdu4920Matrix multiplication(矩阵乘法)
- selenium常用命令
- 首届云端KUMO音乐节,这个夏末天津倾情开唱!
- Android数据库安全解决方案,使用SQLCipher进行加解密
- 玩转Bash脚本:变量基本概念及其初始化
- ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务 的解决方法
- 发的是公司的份的所发生的范德萨
- MyMVC框架的使用
- mysql函数大全