HDU 4920 Matrix multiplication(矩阵相乘)

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 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
   
   
1 0 1 2 0 1 2 3 4 5 6 7

 


Sample Output
   
   
0 0 1 2 1

 


Author
Xiaoxu Guo (ftiasch)
 


Source
#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://javaforall.net/115239.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 使用Ubuntu搭建Web服务器

    使用Ubuntu搭建Web服务器Docker是一个开源的应用容器引擎,基于Go语言并遵从Apache2.0协议开源。Docker可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口(类似iPhone的app),更重要的是容器性能开销极低。总而言之,Docker相当于在你的电脑上建了一个虚拟机…

    2022年5月28日
    30
  • 系统增加locale支持

    系统增加locale支持

    2021年4月22日
    157
  • js checkbox复选框实现单选功能[通俗易懂]

    js checkbox复选框实现单选功能[通俗易懂]本文仅供学习交流使用,如侵立删!联系方式及demo下载见文末jscheckbox复选框实现单选功能<scripttype=”text/javascript”> $(“:checkbox”).click(function(){ $(this).attr(“checked”,true);//设置当前选中checkbox的状态为checked $(this).siblings().attr(“checked”,false);//设置当前选中的checkbox同级(兄弟级

    2022年5月7日
    153
  • linux之路由知识之ip route 命令中的疑惑[通俗易懂]

    linux之路由知识之ip route 命令中的疑惑[通俗易懂]1.基础知识1.1路由(Routing)1.1.1路由策略(使用iprule命令操作路由策略数据库)基于策略的路由比传统路由在功能上更强大,使用更灵活,它使网络管理员不仅能够根据目的地址而且能够根据报文大小、应用或IP源地址等属性来选择转发路径。iprule命令:Usage:iprule[list|add|del]SELECTORACTION(ad…

    2022年7月18日
    22
  • java swing视频教程下载_JAVA Swing 教程

    java swing视频教程下载_JAVA Swing 教程JAVASwing教程,包含所有练习源码和讲解教程!初级界面篇练习01分解颜色练习02画板练习03帧练习04画布练习05密码验证界面练习06对话框练习07滚动条练习08边框练习09单选框图片浏览器练习10卡片布局管理器练习11边界布局管理器练习12进程条练习13列表框和组合框练习14选项卡练习15菜单练习16菜单快捷键练习17模式对话框练习18网格布局管理器练习19复选框练习20单选框练习21…

    2025年9月14日
    5
  • rolling在舞蹈里是什么意思_机械舞和街舞有啥区别

    rolling在舞蹈里是什么意思_机械舞和街舞有啥区别原标题:这,就是街舞中的那些“Swag”十足的舞蹈类型,你了解吗?世界越来越小了,人们越靠越近,视野越来越广,局限于逼仄的小空间的时代已经一去不复返了。现在,一推门,迎面就是全世界。自去年开始,嘻哈文化开始通过综艺节目进入公众视野(中国有嘻哈),这种来自大洋彼岸的“陌生文化”随即引起了广泛的关注。尽管作为“第一个吃螃蟹的人”,《中国有嘻哈》的最终结果不尽如人意,但却为后来的综艺制作人提供了全新的视…

    2025年5月28日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号