Hdu 4920 matrix multiplication (matrix multiplication)

Source: Internet
Author: User

Problem Descriptiongiven, matrices A and B of size nxn, find the product of them.
Bobo hates big integers. So is 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 A. 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 AXB in similar format.

Sample Input10120 12 34 56 7

Sample Output00 12 1

Matrix multiplication is just a three for loop, nothing to say. But this problem good pit, to be careful, how to pit, please look at the code.

1#include <cstdio>2#include <cstring>3 using namespacestd;4 intn,a[804][804],b[804][804],c[804][804];5 intMain ()6 {7     inti,j,k;8      while(~SCANF ("%d",&N))9     {Ten          for(i=1; i<=n;i++) One          for(j=1; j<=n;j++) c[i][j]=0; A          for(i=1; i<=n;i++) -          for(j=1; j<=n;j++) -         { thescanf"%d",&a[i][j]); -a[i][j]%=3;//each input to the% 3, so that the data is smaller, the following can not be% 3.  -         } -          for(i=1; i<=n;i++) +          for(j=1; j<=n;j++) -         { +scanf"%d",&b[i][j]); Ab[i][j]%=3; at         } -          for(k=1; k<=n;k++)//This loop cannot be swapped under the J loop, or it will time out. What reason is not clear at the moment.  -          for(i=1; i<=n;i++) -          for(j=1; j<=n;j++) -C[I][J]+=A[I][K]*B[K][J];//This can not be% 3, or it will time out, because more than a step.  -          for(i=1; i<=n;i++) in         { -              for(j=1; j<n;j++) toprintf"%d", c[i][j]%3); +printf"%d\n", c[i][j]%3); -         } the     } *     return 0; $}

Hdu 4920 matrix multiplication (matrix multiplication)

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.