hdu 4920 Matrix multiplication(矩阵乘法)2014多培训学校5现场

简介:

Matrix multiplication

                                                                          Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

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
 
题意:给出两个n*n的矩阵,求这两个矩阵的乘积。结果对3取余。
分析:拿到题先用了经典的矩阵相乘的方法,提交以后果断超时了。后来在网上搜了一下矩阵相乘优化,找到了一个优化方法,仅仅可惜如今我还没有理解是怎么优化的。

哭

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 805;
int a[N][N], b[N][N], ans[N][N];
void  Multi(int n)
{
    int  i, j, k, L, *p2;
    int  tmp[N], con;
    for(i = 0; i < n; ++i)
    {
        memset(tmp, 0, sizeof(tmp));
        for(k = 0, L = (n & ~15); k < L; ++k)
        {
            con = a[i][k];
            for(j = 0, p2 = b[k]; j < n; ++j, ++p2)
                tmp[j] += con * (*p2);
            if((k & 15) == 15)
            {
                for(j = 0; j < n; ++j) tmp[j] %= 3;
            }
        }

        for( ; k < n; ++k)
        {
            con = a[i][k];
            for(j = 0, p2 = b[k]; j < n; ++j, ++p2)
                tmp[j] += con * (*p2);
        }
        for(j = 0; j < n; ++j)
            ans[i][j] = tmp[j] % 3;
    }
}
int main()
{
    int n, i, j, k;
    while(~scanf("%d",&n))
    {
        for(i = 0; i < n; i++)
            for(j = 0; j < n; j++)
            {
                scanf("%d",&a[i][j]);
                a[i][j] %= 3;
            }
        for(i = 0; i < n; i++)
            for(j = 0; j < n; j++)
            {
                scanf("%d",&b[i][j]);
                b[i][j] %= 3;
            }
        Multi(n);
        for(i = 0; i < n; i++)
        {
            for(j = 0; j < n-1; j++)
                printf("%d ", ans[i][j]);
            printf("%d\n", ans[i][n-1]);
        }
    }
    return 0;
}

http://blog.csdn.net/gogdizzy/article/details/9003369这里面解说了矩阵相乘的优化方法。

以下这样的方法也能够过:
#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;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。





本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/4756900.html,如需转载请自行联系原作者


相关文章
|
开发框架 搜索推荐 安全
到底什么是伪静态?为什么要做伪静态?
到底什么是伪静态?为什么要做伪静态?
970 0
|
JavaScript 前端开发 索引
js 数组 reduce() 方法
js 数组 reduce() 方法
567 0
js 数组 reduce() 方法
|
存储
剑指offer 67. 滑动窗口的最大值
剑指offer 67. 滑动窗口的最大值
212 0
|
8天前
|
域名解析 人工智能 Linux
|
18天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
17125 62
|
16天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
2天前
|
人工智能 安全 网络安全
|
6天前
|
云安全 数据采集 人工智能
|
7天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1287 3
|
13天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2357 7