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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 农林业遥感图像分类研究[通俗易懂]

    农林业遥感图像分类研究[通俗易懂]遥感图像处理是数字图像处理技术中的一个重要组成部分,长期以来被广泛应用于农林业的遥感测绘,防灾减灾等领域。本文旨在通过深度学习技术从遥感影像中分类出农田和林业地块。手工从遥感图像中分类出农田和林业区域分类虽然准确但是效率低下,并且很多采用传统图像分割技术的方法泛化性能差,不适合场景复杂的遥感图像。经实践证明,使用深度学习技术在各种计算机视觉任务中都取得了良好的效果,因此本文首先使用先进的深度学习框…

    2022年9月25日
    6
  • Vim 3 vimrc[通俗易懂]

    Vim 3 vimrc[通俗易懂]文章目录什么是vimrc基本修改UI相关配置编码相关配置文件相关配置编辑器相关配置按键映射“键我的vimrc小结什么是vimrcvimrc是Vim的配置文件,Vim在启动时会加载vimrc文件,你能想到的几乎所有的配置(包括主题,快捷键,插件设置等等),都可以配置在vimrc中,所以,vimrc在Vim使用过程中有着至关重要的地位.Vim是极…

    2022年5月18日
    47
  • Java核心技术卷一学习笔记1

    Java核心技术卷一学习笔记1在搞本科生毕设之余,闲下来看看书,写写博客。由于研究生学习要做一个数据可视化的项目,需要用到Java。所以拿了一本《Java核心技术卷一》正在一步步复习之前所学的Java知识。   3.9大数值   如果基本的整数和浮点数精度不能够满足需求,那么可以使用java.math包中的两个很有用的类:BigInteger和BigDecimal。这两个类可以处理包含任意长度数字序列的数值。

    2025年8月7日
    3
  • linux c 报错 warning: large integer implicitly truncated to unsigned type[-Woverflow]

    linux c 报错 warning: large integer implicitly truncated to unsigned type[-Woverflow]警告的原因是:整数溢出整数溢出:当整数达到它所能表述的最大值时,会重新从起点开始#include<stdio.h>intmain(void){ unsigneda=12345678910; printf(“a=%d\n”,a); return0;}该程序输出以后并不是输出a=12345678910而是:上面的代码还不足以说明清楚下面才是重头戏:#include<stdio.h>intmain(void){ inta=2

    2022年7月25日
    24
  • webview长按复制_android长按菜单

    webview长按复制_android长按菜单TextView长按复制实现方法3种方法实践总结发布时间:2020-09-0514:32:58来源:51CTO阅读:1003作者:huangwenwenlili实现效果使用ContextMenu(1)注册菜单@OverridepublicvoidonCreate(BundlesavedInstanceState){mMoblieTextView=(TextView)findVie…

    2022年9月29日
    4
  • 让Firefox支持ActiveX控件「建议收藏」

    让Firefox支持ActiveX控件「建议收藏」让Firefox支持ActiveX控件  疑难集锦字号  我现在用Firefox作为主力浏览器,Firefox下能否支持ActiveX插件?所为对某些包含ActiveX控件的网站并不兼容,不知道怎么做能让Firefox支持ActiveX呢?      答:两种方案:一种是使用IETab这个插件,在Firefox下直接调用IE浏览器内核进行浏览;      另一种是使

    2022年5月14日
    333

发表回复

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

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