HDU 4920 Matrix multiplication (Matrix multiplication), hdumultiplication

Source: Internet
Author: User
Tags cmath

HDU 4920 Matrix multiplication (Matrix multiplication), hdumultiplication

Various TEL, 233 ah. I didn't expect it would be enough to deal with 0. I always thought there would be extreme data. I did not expect this. I saw an amazing AC code on the Internet. The classic matrix multiplication only enumerated the innermost layer, it's time to move outside, and it's a bit difficult to understand. Isn't the complexity the same ,,

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 DescriptionGiven 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.
InputThe 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. the j-th integer in the I-th line equals Aij. the next n lines describe the matrix B in similar format (0 ≤aij, Bij ≤109 ).
OutputFor each tests:

Print n lines. Each of them contain n integers -- the matrix A × B in similar format.
Sample Input
10120 12 34 56 7
 
Sample Output
00 12 1
 
AuthorXiaoxu Guo (ftiasch)
Source2014 Multi-University Training Contest 5
#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;}

Here is the code of the AC that someone handed in:

# 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 ++) // in a classic algorithm, this layer of loop exists in the innermost layer, which times out, but not in the outermost layer or in the middle, I do not know why 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; // if the remainder of 3 is obtained here, it will time out} 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 ;}





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.