The problem in brief
Read two matrices, of size and of size , with all dimensions up to 300 and real entries, and print their product , an matrix. That is the whole task: a direct exercise on the operation at the heart of every neural network, where each layer computes for a whole batch at once. The only real difficulty is speed, with a 1 second time limit.
How you are scored
Hidden tests compare your matrix with the exact product (the tolerance is not stated; we print floats with full precision via repr). The README gives no sample, so we test against numpy on 200 random shapes and on a hand-computed case.
The key idea
Definition. Entry of the product is the dot product of row of with column of :
The inner dimensions must agree ( has columns, has rows), and the result takes the outer ones, . For example
A second reading of the same formula. Row of is a linear combination of the rows of , with weights taken from row of :
This is the view behind "a layer mixes its inputs": every output row is a weighted blend of the input rows.
Cost. Each of the entries needs multiply-adds, so the product costs operations: up to . A modern CPU does that in a few milliseconds, but a Python interpreter executes each s += A[i][k] * B[k][j] as several bytecode instructions, roughly 40 nanoseconds each round. That puts a pure Python triple loop right at the time limit.

On our machine, the triple loop takes 1.08 s for , and the row-combination version 0.96 s, both on the edge of the limit before even counting the time to read and print 90 000 numbers. numpy's @, which calls an optimised BLAS routine (compiled, vectorised, cache-friendly), takes 7 milliseconds, 150 times faster. All three curves have slope 3 on the log-log plot: same algorithm, very different constant factors.
The full solution
import sys, numpy as np
t = sys.stdin.read().split()
a, b, c = map(int, t[:3])
v = np.array(t[3:3 + a*b + b*c], dtype=float)
A, B = v[:a*b].reshape(a, b), v[a*b:].reshape(b, c)
for row in A @ B:
print(" ".join(repr(float(x)) for x in row))
Reading every token at once with split() does not depend on line breaks, and it is much faster than calling input() 600 times. solution.py also contains the two pure Python versions, used for testing and as a fallback if numpy were unavailable on the judge.
Results
| check | result |
|---|---|
| 200 random shapes up to : triple loop, row combination, numpy | all agree (np.allclose) |
| hand-computed product | 19 22 / 43 50 |
| : triple loop / row combination / numpy | 1.08 s / 0.96 s / 0.007 s |
What to remember
- is row of dotted with column of ; equivalently, each row of is a combination of the rows of . Inner dimensions must match.
- The cost is multiply-adds: doubling every dimension multiplies the time by 8.
- Vectorise: in Python, push the inner loops into numpy (BLAS). The algorithm is the same, the constant factor is 100 times smaller. This is why deep learning frameworks phrase everything as matrix products.
- Read input in bulk (
sys.stdin.read().split()) when it is large.