Skills101
← All coding problems

Matrix Multiplication

mediumMatrix+20 XPTime limit 2s

Multiply an r × m matrix A by an m × c matrix B. The result C is r × c, and each cell is a "row times column" sum:

C[i][j] = A[i][0]·B[0][j] + A[i][1]·B[1][j] + … + A[i][m−1]·B[m−1][j]

Input:

  • line 1: r m c
  • next r lines: m integers each (matrix A)
  • next m lines: c integers each (matrix B)

Output: the r × c product, one row per line.

Constraints: 1 ≤ r, m, c ≤ 150, −1000 ≤ value ≤ 1000 (cells of C can reach 1.5·10⁸ — fits in 32 bits, but 64-bit is safer).

Think about it: three nested loops — over i, j, and the shared index k. The number of columns of A must equal the number of rows of B; that shared size is m.

Sample input 1

2 3 2
1 2 3
4 5 6
7 8
9 10
11 12

Sample output 1

58 64
139 154

Sample input 2

1 1 1
3
-4

Sample output 2

-12

Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).

Loading editor…

Log in to run and submit your code — it's free.