HDU 4902 Matrix Multiplication

Source: Internet
Author: User
Click the open link matrix multiplication. Time Limit: 4000/2000 MS (Java/others) memory limit: 131072/131072 K (Java/Others)
Total submission (s): 2113 accepted submission (s): 956


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
Directly multiply the matrix, decisive TLE. To solve this problem, you must use the sparse matrix method.
// 1703ms7868k # include <stdio. h> # include <string. h> # define M 3int A [807] [807], B [807] [807], C [807] [807]; int N; void Init () // create a matrix {for (INT I = 0; I <n; I ++) for (Int J = 0; j <n; j ++) {scanf ("% d", & A [I] [J]); A [I] [J] % = m ;}for (INT I = 0; I <n; I ++) for (Int J = 0; j <n; j ++) {scanf ("% d ", & B [I] [J]); B [I] [J] % = m ;}} int main () {While (scanf ("% d ", & N )! = EOF) {Init (); memset (C, 0, sizeof (c); For (INT I = 0; I <n; I ++) for (int K = 0; k <n; k ++) if (a [I] [k]) for (Int J = 0; j <n; j ++) c [I] [J] = (C [I] [J] + A [I] [k] * B [k] [J]) % 3; for (INT I = 0; I <n; I ++) {for (Int J = 0; j <n-1; j ++) printf ("% d ", c [I] [J]); printf ("% d \ n", C [I] [n-1]) ;}} 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.