HDU 4920 Matrix multiplication(矩阵相乘)

各种TEL,233啊。没想到是处理掉0的情况就能够过啊。一直以为会有极端数据。没想到居然是这种啊、、在网上看到了一个AC的奇妙的代码,经典的矩阵乘法,仅仅只是把最内层的枚举,移到外面就过了啊、、、有点不理解啊,复杂度不是一样的吗、、

Matrix multiplication

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 640    Accepted Submission(s): 250


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

Sample Input
1 0 1 2 0 1 2 3 4 5 6 7

Sample Output
0 0 1 2 1

Author
Xiaoxu Guo (ftiasch)

Source
2014 Multi-University Training Contest 5
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-12
///#define M 1000100
#define LL __int64
///#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?

0:x) using namespace std; const int maxn = 810; int a[maxn][maxn]; int b[maxn][maxn]; int c[maxn][maxn]; int aa[maxn][maxn]; int bb[maxn][maxn]; int main() { int n; while(cin >>n) { memset(c, 0, sizeof(c)); memset(aa, 0, sizeof(aa)); memset(bb, 0, sizeof(bb)); for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { scanf("%d",&a[i][j]); a[i][j] %= 3; } } for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { scanf("%d",&b[i][j]); b[i][j] %= 3; } } for(int i = 1; i <= n; i++) { int x = -1; for(int j = n; j >= 0; j--) { aa[i][j] = x; if(a[i][j]) x = j; } } for(int i = 1; i <= n; i++) { int x = -1; for(int j = n; j >= 0; j--) { bb[i][j] = x; if(b[i][j]) x = j; } } for (int i = 1; i <= n; i++) { for(int j = aa[i][0]; j != -1; j = aa[i][j]) { for(int k = bb[j][0]; k != -1; k = bb[j][k]) c[i][k] += a[i][j]*b[j][k]; } } for(int i = 1; i <= n; i++) { for(int j = 1; j <= n-1; j++) printf("%d ",c[i][j]%3); printf("%d\n",c[i][n]%3); } } return 0; }


这是看到有人交的AC的代码:

#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;
}


转载于:https://www.cnblogs.com/yxwkf/p/5418535.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/418626.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

前端学习(1888)vue之电商管理系统电商系统之在sessionStrorage中保存按钮的激活状态

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vue import Welcome from ./components/Welcome.vue Vue.use(Router)const router new Router({routes: [{path: /,re…

java parseint()

static int parseInt(String s)static int parseInt(String s, int radix) parseInt(String s)表示将 “数字” 转换成10进制整数 parseInt(String s&#xff0c;int radix)表示将radix 进制的字符串“s” 转换为10进制整数 radix为可选参数&#xff0c;如果没有默认是十进制&a…

前端学习(1889)vue之电商管理系统电商系统之绘制用户列表组件的基本布局

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vue import Welcome from ./components/Welcome.vue Vue.use(Router)const router new Router({routes: [{path: /,re…

MFC - PreTranslateMessage()响应自定义消息

1 // 自定义消息2 #define MY_THREAD_MSG WM_USER10003 4 5 //6 // PreTranslateMessage()如果要响应【自定义消息】必须用::PostMessage()发送自定义消息 7 // PreTranslateMessage()不响应::SendMessage()发送的【自定义消息】 10 BOOL CMyThreadDlg::PreTran…

前端学习(1890)vue之电商管理系统电商系统之绘获取用户列表数据

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vue import Welcome from ./components/Welcome.vue Vue.use(Router)const router new Router({routes: [{path: /,re…

详解spring boot mybatis全注解化

https://www.cnblogs.com/goloving/p/9125948.html https://www.jianshu.com/p/8cffcc105b3a https://blog.csdn.net/abc5232033/article/details/79060439

java初学者笔记总结day6

static关键字作用&#xff1a;没有static在类中定义的【数据和功能】&#xff08;成员变量和成员方法&#xff09;&#xff0c;都是属于对象的&#xff0c;如果【数据和功能】只是跟类有关系&#xff0c;那么我们可以通过staic关键字修饰&#xff0c;变为&#xff08;静态变量和…

前端学习(1891)vue之电商管理系统电商系统之el-table渲染表格

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vue import Welcome from ./components/Welcome.vue Vue.use(Router)const router new Router({routes: [{path: /,re…

使用spring ResponseEntity处理http响应

https://blog.csdn.net/neweastsun/article/details/81142870

20145302张薇《Java程序设计》实验三报告

20145302张薇《Java程序设计》实验三&#xff1a;敏捷开发与XP实践 实验内容 使用git上传代码使用git实现代码开发实践实现代码的重载 使用git上传代码 git initgit add .git commit -m "注释"git push origin master出现error&#xff1a;fetch-first解决办法&#…

前端学习(1892)vue之电商管理系统电商系统之为表格添加索引列

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vue import Welcome from ./components/Welcome.vue Vue.use(Router)const router new Router({routes: [{path: /,re…

install kinect driver for ARM---38

原创博客:转载请标明出处:http://www.cnblogs.com/zxouxuewei/ The video describes connecting a Microsoft Kinect to the NVIDIA Jetson TK1 Development Kit. The Kinect provides 3D depth information about a scene. This is commonly referred to as a RGBD device bec…

前端学习(1893)vue之电商管理系统电商系统之自定义状态列的效果

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vue import Welcome from ./components/Welcome.vue Vue.use(Router)const router new Router({routes: [{path: /,re…

tomcat学习笔记1

tomcat是一个开源软件&#xff0c;是由java语言编写的&#xff0c;它工作的话需要运行在jvm虚拟机中&#xff0c;说到jvm不得不说下java这个大名鼎鼎的编程 语言了 java这个编程语言最优秀的特点要数write once , run anywhere 额&#xff01;怎么听起来有点流氓的感觉 没错&…

前端学习(1894)vue之电商管理系统电商系统之通过作用域插槽操作列

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vue import Welcome from ./components/Welcome.vue Vue.use(Router)const router new Router({routes: [{path: /,re…

前端学习(1895)vue之电商管理系统电商系统之组件的分页显示

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vue import Welcome from ./components/Welcome.vue Vue.use(Router)const router new Router({routes: [{path: /,re…

工作总结08

昨天做了数据库的创建。 今天准备做组件传数据&#xff0c;在Activity中使用数据库。 遇到的问题&#xff1a;数据创建成功&#xff0c;但是只能通过adb语言查看&#xff0c;相当于不可视&#xff0c;有时候发现不了&#xff0c;意味着数据库创建失败。转载于:https://www.cnbl…

前端学习(1896)vue之电商管理系统电商系统之修改用户状态

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vue import Welcome from ./components/Welcome.vue Vue.use(Router)const router new Router({routes: [{path: /,re…

html5笔记

出处:http://www.cnblogs.com/xiaowei0705/archive/2011/04/19/2021372.html HTML5 LocalStorage 本地存储 HTML5 LocalStorage 本地存储 说到本地存储&#xff0c;这玩意真是历尽千辛万苦才走到HTML5这一步&#xff0c;之前的历史大概如下图所示&#xff1a; 最早的Cookies自然…