Hdu4920Matrix multiplication(矩阵乘法)

Hdu 4920 Matrix multiplication[矩阵乘法的时间复杂度优化] 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4920  题目的意思就是求两个矩阵的乘法。 阅读详情

Matrix multiplication

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


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的矩阵相乘。

直接相乘将第二个矩阵进行倒置。b[i][j]<->b[j][i]。另一种方法是将a的一个数进行判断,如果等于0则不进行运算,否则则进行运算。还有,奇怪的是用G++超时,C++和C都AC。至今不懂为什么会这样。

矩阵相乘优化策略:
矩阵转置:矩阵相乘是矩阵A的一行数据元素与矩阵B的一列的数据元素对应相乘,然后再对其积求和,由于C与C++的二维数组是以行为主序存储的,因此矩阵A的行数据元素是连续存储的,而矩阵B的列数据元素是不连续存储的(N*1的矩阵除外),为了在矩阵相乘时对矩阵B也连续读取数据,根据局部性原理对矩阵B进行转置,矩阵转置是数据元素B[i][j]与B[j][i]进行交换,矩阵转置后,矩阵相乘是矩阵A的一行数据元素与转置后的矩阵B的一行的数据元素对应相乘,然后再对其积求和,这个和就是矩阵C的一个数据元素。

参考资料http://wenku.baidu.com/linkurl=261XeEzHAZkFGPiN63t1nnojoQF50yiuMoviHroGjVXjjRlxFcvWLcws0jgQcmZo4oA9BJcjnPxVreWRu-XXa9zb6r5gUUTxmBXn_qWSsu&qq-pf-to=pcqq.group

#include <stdio.h>
#define LL __int64
int a[900][900],b[900][900],c[900][900];
int main()
{
    int n,i,j,k;
    while (~scanf("%d",&n))
    {
    	int x;
        for (i=0;i<n;i++)
            for (j=0;j<n;j++)
            {
                scanf("%d",&x);
                a[i][j]=x%3;
            }
        for (i=0;i<n;i++)
            for (j=0;j<n;j++)
            {
                scanf("%d",&x);
                b[j][i]=x%3;
            }
        for (i=0;i<n;i++)
        {
        	for (j=0;j<n;j++)
        	{
        		c[i][j]=0;
           		for (k=0;k<n;k++)
                	c[i][j]=c[i][j]+a[i][k]*b[j][k];
                printf(j==0?"%d":" %d",c[i][j]%3);
            }
            printf("\n");
        }
    }
    return 0;
}
#include <stdio.h>
#define LL __int64
int a[810][810],b[810][810],c[810][810],n,i,j,k,x;
int main()
{
	while (~scanf("%d",&n))
	{
		for (i=0;i<n;i++)
			for (j=0;j<n;j++)
			{
				scanf("%d",&x);
				a[i][j]=x % 3;
			}
		for (i=0;i<n;i++)
			for (j=0;j<n;j++)
			{
				scanf("%d",&x);
				b[j][i]=x % 3;
			}
		for (i=0;i<n;i++)
		{
			for (j=0;j<n;j++)
			{
				c[i][j]=0;
				for (k=0;k<n;k++)
					c[i][j]=c[i][j]+a[i][k]*b[j][k];
				printf(j==0?"%d":" %d",c[i][j]%3);
			}
			printf("\n");
		}
	}
	return 0;
}


关于矩阵乘法的一个改进算法的时间复杂度 关于矩阵乘法的一个改进算法的时间复杂度 两个 n 阶非负整数方阵相乘 常规算法的时间复杂度为 On3 立即下载

相关推荐

Sharding-JDBC 系列专题 - 第九篇:高可用性与集群管理

高可用性是指系统在面对故障(如数据库宕机、网络中断)时,能够快速恢复并继续提供服务。Sharding-JDBC 和 Sharding-Proxy 提供了多种机制支持高可用性,同时需要结合底层数据库的高可用方案(如 MySQL 主从复制、MHA)来实现。高可用性和集群管理是 Sharding-JDBC 和 Sharding-Proxy 在生产环境中成功应用的关键。本篇详细介绍了数据库高可用方案、Sharding-JDBC 的故障切换机制、Sharding-Proxy 的集群部署,以及分布式治理功能。

资深架构师分享:深入剖析软件架构,探讨云计算、微服务、大数据等前沿技术。分享实践经验,促进技术交流,共同构建卓越系统。 1399

hdu1588之经典矩阵乘法

经典矩阵乘法

Stephen 1212

拆开华为BBU5900,看看5G基站的大脑里到底装了啥?

本文深度拆解华为BBU5900,揭示5G基站核心处理单元的硬件架构与设计哲学。通过详细解析模块化设计、智能电源系统和动态散热方案,展现其支撑千兆速率、毫秒级时延的技术奥秘,为5G网络部署提供实用参考。

weixin_31456563的博客 586

HDU 4382 矩阵乘法

题意:给出ADD SET MUL 操作,问你循环N次(N为大数),c2的结果,对1e9+7取模。 把每个操作转化成矩阵,相乘后根据费马小定理矩阵高次幂取模就行了。 转化的过程我的答案真在后面所以把字符串先存起来倒着乘的,如果答案阵在前那么就正这乘就行了。 #include #include #include #include using namespace std; const int M

韩小侠的专栏 已迁移至lssea.com 1024

【线粒体基因组数据提交实战】从注释校验到GenBank批量入库的避坑指南

本文详细介绍了线粒体基因组数据从注释校验到GenBank批量上传的全流程避坑指南。重点解析了GB2sequin工具的正确使用方法、TBL注释文件的精修技巧,以及BankIt平台提交的隐藏要点,帮助科研人员高效完成NCBI数据提交,特别适合需要批量上传线粒体基因组数据的研究者。

weixin_28726671的博客 452

避坑指南:ANSYS流固耦合计算中System Coupling数据传递设置与常见报错解决

本文深入解析ANSYS Workbench中System Coupling模块在双向流固耦合(FSI)计算中的应用,详细讲解数据传递机制、典型报错解决方案及高阶调试技巧。通过实际案例展示如何优化耦合步长、处理大变形问题,并提升并行计算效率,帮助工程师有效规避常见技术陷阱。

weixin_29213525的博客 739

HDU 1005(矩阵乘法)

题意:如题。 可以使用找规律: #include #include using namespace std; int f[1000001]; int main() { int a,b,n; while(cin>>a>>b>>n) { if(a==0&&b==0&&n==0) break;

ACM Dream 967

【芯片验证随心记】1.SystemVerilog中,rand和randc的区别?如何用constraint实现randc?

在SystemVerilog里,randc 的“不重复循环”特性是用一个 周期性排列器(permutation engine) 实现的,而这个引擎在 LRM 里并没有公开。我们只能在 “用户空间” 用 rand + constraint 去模仿它的行为。每次随机化只取 queue.pop_front(),用完再洗。在4次随机化内,y会依次出现0,1,2,3各一次(顺序随机)每次随机化,x可能是0-3中的任意值,可能连续多次相同。每次随机化时,在未使用的值里随机挑一个;第5次随机化时,重新生成新的排列周期。

qq_43172512的博客 346

OpenBLAS大数据集处理:分块矩阵乘法的内存优化策略

你是否曾在处理大规模数据集时遭遇过性能瓶颈?当矩阵维度超过10,000时,即使是最先进的硬件也可能陷入停滞。本文将深入探讨OpenBLAS(开源基础线性代数子程序库)如何通过分块矩阵乘法(Block Matrix Multiplication)突破内存限制,实现TB级数据高效运算。读完本文,你将掌握: - 内存层次结构对矩阵运算的影响机制 - OpenBLAS分块策略的底层实现原理 - 分块大小...

gitblog_00193的博客 357

SKF SimPro Quick 4.8.1发布

发布说明可以在SimPro手册的SKF SimPro Quick应用程序的一般部分下找到(帮助→SKF SimPro快速手册→一般→发布说明)。请删除旧版本,请备份您存储在C:\SKF SimPro xxx\files文件夹中的文件以及所有自定义报告模板和表格。我们很高兴地通知您,我们的轴承应用工程软件SKF SimPro Quick 4.8.1可供下载。将新的安装文件下载到本地计算机后,双击它安装向导将打开(按照说明操作)3.安装后,在本地机器上启动SKF SimPro Expert软件。

小韩的博客 1823

MicroCap声学仿真介绍(二)-创建声学仿真元件

声学库里包含孔、管、障板辐射器等器件,我们只需要按照仿真的需求,将声学库里的器件排列好,输入参数,用线连接起来即可。这里我们用D指代孔的直径,L指代孔的长度,N代表孔的数量。26.在弹出的界面中做如下设置:需要先在右面的选框里找到对应的项,然后右键Add Part,再之后就如下设置。3.在弹出的界面中,选择“Macro file(.mac)”,并点击右上角的“OK”下面就以声学仿真中常见的“孔”来做示例,跟大家一块演示一下宏程序的新建。10.在上图中“Value”中填入“R”,然后点击左边的“OK”按钮;

山无忧的博客 2209

分量乘法与分布式计算:提升性能的关键技术

1.背景介绍 分量乘法(Block Multiplication)是一种高效的矩阵乘法方法,它主要应用于分布式计算系统中,以提升性能。在大数据时代,处理大型矩阵计算是一个常见的问题,例如机器学习、图像处理、信号处理等领域。传统的矩阵乘法方法,如稠密矩阵乘法(Dense Matrix Multiplication),在处理大规模矩阵时效率较低,因此需要更高效的算法来解决这个问题。 分量乘法与分布...

AI天才研究院 1260

H2db console 未授权访问RCE 漏洞复现+利用(CVE-2022-23221)

H2 数据库控制台中的另一个未经身份验证的 RCE 漏洞,在 v2.1.210+ 中修复。2.1.210 之前的 H2 控制台允许远程攻击者通过包含 IGNORE_UNKNOWN_SETTINGS=TRUE;INIT=RUNSCRIPT 子字符串的 jdbc:h2:mem JDBC URL 执行任意代码。

0xSec 4571

hdu 4920 Matrix multiplication(矩阵乘法)

hdu 4920 Matrix multiplication(矩阵乘法)

1606

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

HDU 4920 Matrix multiplication(矩阵乘法,cin,cout超时呀)

Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 3989    Accepted Submission(s): 1624 Problem Description Gi

AC_Dreameng 1092

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

HDU 4920 Matrix multiplication(矩阵乘法)

传送门:HDU 4920 题意:给出

一个写程序的 1161

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

【HDU - 4920】Matrix multiplication(矩阵乘法优化、bitset)

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

__似水流年__的博客 691

hdu4920 Matrix multiplication 模3矩阵乘法

hdu4920 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 568 Accepted Submission(s): 225 Problem Descript...

weixin_34319111的博客 239

HDU 4920 Matrix multiplication(矩阵乘法)

题意: 矩阵乘法 #include #include #include #include #define MAX 1000 #define mod 3 using namespace std; int A[MAX][MAX],B[MAX][MAX],f[MAX][MAX]; int n; void mul() { int i,j,k; memset(f,0

Twilightuse93的专栏 460

hdu 4920 Matrix multiplication 稀疏矩阵的乘法

#include #include using namespace std; #define maxn 810 #define rep(i,x,y) for(int i=x;i<=y;i++) #define mset(x) memset(x,0,sizeof(x)) int n; int a[maxn][maxn], b[maxn][maxn], res[maxn][maxn]; int m

436

hdu4920 Matrix multiplication [矩阵乘法 压位 ]

2014多校第五场最后一题,

hccz95的专栏 798

hdu 4920 Matrix multiplication (矩阵乘法)

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

weixin_30340745的博客 117

HDU 4920(Matrix multiplication-矩阵乘法优化)

Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 3647    Accepted Submission(s): 1522 Problem Description Given

nike0good |Oier&ACMer | 熟能生巧 875

HDU 4920.Matrix multiplication-矩阵乘法

Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 5236Accepted Submission(s): 2009 Problem Description Given two...

weixin_30390075的博客 147

矩阵乘法 --- hdu 4920 : Matrix multiplication

Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 820    Accepted Submission(s): 328 Problem Description Given two matr...

weixin_33743703的博客 180
上一篇: Hdu 2980Paint Chain(博弈论)
下一篇: Hdu4111Alice and Bob(dp+博弈论)
Modiz
博客等级 码龄14年 77粉丝 297原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值