Matrix multiplication 2 (Codevs no.3147)

Source: Internet
Author: User

2016-06-01 17:33:30

Title Link: Matrix multiplication 2 (Codevs no.3147)

Main topic:

Given two square matrices of the same size, a, B. Multiple queries, each time a matrix of a sub-matrix of all elements of the and.

Solution:

First think of violence.

Preprocessing n^3, asking for a mock sweep, this constant is just an instant explosion.

Of course, it can be optimized.

Listing the element expressions of the sub-matrices, you will find some elements//matrix multiplication 2 (Codevs no.3147)
Matrix multiplication
#include <stdio.h>
#include <algorithm>
using namespace Std;
const int maxn=2010;
int MAP1[MAXN][MAXN];
int MAP2[MAXN][MAXN];
Long long ans;
int n,m;
int a,b,c,d;
int main ()
{
scanf ("%d%d", &n,&m);
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
scanf ("%d", &map1[i][j]);
MAP1[I][J]+=MAP1[I-1][J];
}
}
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
scanf ("%d", &map2[i][j]);
MAP2[I][J]+=MAP2[I][J-1];
}
}
for (int i=1;i<=m;i++)
{
ans=0;
scanf ("%d%d%d%d", &a,&b,&c,&d);
for (int i=1;i<=n;i++)
{
int Sumxmap2=map2[i][max (B,D)]-map2[i][min (b,d)-1];
int Sumymap1=map1[max (A,C)][i]-map1[min (a,c) -1][i];
ans+= (long Long) sumxmap2*sumymap1;
}
printf ("%lld\n", ans);
}
return 0;
The product of the} can be used to multiply the distribution law

So we simply give up the exact value of the sub-matrix and combine the data according to the characteristics of the distributive law.

We prefix the column with a (multiply by line) and, for B (multiplication by the time), prefix the line and

Finally, all the elements can be represented as Sigma ((A[i][dbound]-a[i][ubound-1]) * (b[i][rbound]-b[i][lbound-1]);

(Ubound,dbound,lbound,rbound represents the lower and upper sides of the small matrix)

1 //Matrix multiplication 2 (Codevs no.3147)2 //matrix multiplication3#include <stdio.h>4#include <algorithm>5 using namespacestd;6 Const intmaxn= .;7 intMAP1[MAXN][MAXN];8 intMAP2[MAXN][MAXN];9 Long Longans;Ten intn,m; One inta,b,c,d; A intMain () - { -scanf"%d%d",&n,&M); the      for(intI=1; i<=n;i++) -     { -          for(intj=1; j<=n;j++) -         { +scanf"%d",&map1[i][j]); -map1[i][j]+=map1[i-1][j]; +         } A     } at      for(intI=1; i<=n;i++) -     { -          for(intj=1; j<=n;j++) -         { -scanf"%d",&map2[i][j]); -map2[i][j]+=map2[i][j-1]; in         } -     } to      for(intI=1; i<=m;i++) +     { -ans=0; thescanf"%d %d%d%d",&a,&b,&c,&d); *          for(intI=1; i<=n;i++) $         {Panax Notoginseng             intSumxmap2=map2[i][max (b,d)]-map2[i][min (b,d)-1]; -             intSumymap1=map1[max (a,c)][i]-map1[min (a,c)-1][i]; theans+= (Long Long) sumxmap2*Sumymap1; +         } Aprintf"%lld\n", ans); the     } +     return 0; -}

Matrix multiplication 2 (Codevs no.3147)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.