博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 4920 Matrix multiplication(简单矩阵相乘+技巧减少Mod次数)
阅读量:4139 次
发布时间:2019-05-25

本文共 2240 字,大约阅读时间需要 7 分钟。

Matrix multiplication

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 3866 Accepted Submission(s): 1590
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.
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 A
ij. The next n lines describe the matrix B in similar format (0≤A
ij,B
ij≤10
9).
Output
For each tests:
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
Author
Xiaoxu Guo (ftiasch)
Source
//mod3为0的话这个可以优化 跳过循环   //别人的代码  对如果存在很多0的矩阵可以变快/* #include 
#include
#include
using namespace std;int a[808][808];int b[808][808];int ans[808][808];int main(){ int n; while (~scanf("%d",&n)) { for (int i=1;i<=n;i++) for (int t=1;t<=n;t++) { scanf("%d",&a[i][t]); a[i][t]%=3; } for (int i=1;i<=n;i++) for (int t=1;t<=n;t++) { scanf("%d",&b[i][t]); b[i][t]%=3; } for (int i=1;i<=n;i++) for (int t=1;t<=n;t++) ans[i][t]=0; for (int i=1;i<=n;i++) for (int k=1;k<=n;k++) { if (a[i][k]==0) continue ; for (int t=1;t<=n;t++) ans[i][t]+=a[i][k]*b[k][t]; } for (int i=1;i<=n;i++) { printf("%d",ans[i][1]%3); for (int t=2;t<=n;t++) printf(" %d",ans[i][t]%3); printf("\n"); } }}*/ //但是本代码是使用减少Mod求余的次数 也可以过 1750MS #include
#include
#include
using namespace std;const int N=800+10;int A[N][N],B[N][N],re[N][N];int n;inline void handle(){ int i,j,z; for(i=0;i
#include
#include
using namespace std;const int N=800+10;int A[N][N],B[N][N],re[N][N];int n;inline void handle(){ int i,j,z; for(i=0;i

转载地址:http://vfmvi.baihongyu.com/

你可能感兴趣的文章
你准备写代码到多少岁?程序员们是这么回答的!
查看>>
码农:和产品对一天需求,产品经理的需求是对完了,可我代码呢?
查看>>
程序员过年回家该怎么给亲戚朋友解释自己的职业?
查看>>
技术架构师的日常工作是什么?网友:搭框架,写公共方法?
查看>>
第四章 微信飞机大战
查看>>
九度:题目1008:最短路径问题
查看>>
九度Online Judge
查看>>
九度:题目1027:欧拉回路
查看>>
九度:题目1012:畅通工程
查看>>
九度:题目1017:还是畅通工程
查看>>
九度:题目1034:寻找大富翁
查看>>
第六章 背包问题——01背包
查看>>
第七章 背包问题——完全背包
查看>>
51nod 分类
查看>>
1136 . 欧拉函数
查看>>
面试题:强制类型转换
查看>>
Decorator模式
查看>>
Template模式
查看>>
Observer模式
查看>>
高性能服务器设计
查看>>