KatInterview

Matrix Product, Fast Power and Fibonacci Toolkit

Math & Bit ManipulationAuto-graded · 15 test cases
MediumAuto-gradedMath & Bit Manipulationmatrix-multiplicationfast-exponentiationfibonacci

Last updated

Combine matrix multiplication, square-and-multiply exponentiation and 2x2 matrix-power Fibonacci, with optional modular arithmetic, in one function.

Write one function that computes three independent quantities and returns them together. An optional modulus can apply to all of them.

Inputs

  • mat_a: an r × s matrix of integers (list of rows).
  • mat_b: an s × c matrix of integers (list of rows).
  • base: an integer.
  • exp: a non-negative integer exponent.
  • idx: a non-negative integer index into the Fibonacci sequence.
  • modulus: either None (no reduction) or an integer.

What to compute

  1. Matrix product. Compute mat_a × mat_b, an r × c matrix, using the standard definition: entry (i, j) is the sum over t of mat_a[i][t] * mat_b[t][j].

    • If modulus is given, every entry of the result is reduced modulo modulus into the range [0, modulus).
    • If modulus is None, entries are left as exact integers.
    • If mat_a has no rows, the product is []. If mat_b has no rows, treat its column count as 0, so the product has len(mat_a) rows, each an empty list.
  2. Integer power. Compute base ** exp in O(log exp) multiplications.

    • If modulus is given, the result is base ** exp mod modulus, normalized to be non-negative. The one exception is exp = 0, where the answer is always exactly 1, even when modulus = 1.
    • If modulus is None, return the exact integer, keeping its sign (for example (-2)^3 = -8).
    • 0^0 is 1.
  3. Fibonacci number. Compute F(idx), where F(0) = 0, F(1) = 1 and F(m) = F(m-1) + F(m-2).

    • It must run in O(log idx) matrix operations, so idx can be as large as 10^9 when a modulus is supplied.
    • If modulus is given, return F(idx) mod modulus.

Validation

If modulus is not None and is <= 0, raise ValueError before doing any other work.

Output

Return a dictionary with exactly these keys:

  • "matrix": the product from part 1,
  • "power": the value from part 2,
  • "fib": the value from part 3.

Constraints

  • mat_a[i] has length s for every row, and mat_b has s rows of equal length, so the product is well defined.
  • exp >= 0 and idx >= 0.
  • Matrix entries and base are integers that may be negative.
  • When modulus is None, inputs are small enough that exact results are practical.
  • When modulus is given, it may be as large as about 10^9 + 7, and idx and exp may be as large as 10^9.

Examples

Example 1

Input: matrix_pow_fib_toolkit([[1, 2], [3, 4]], [[5, 6], [7, 8]], 2, 10, 10, None)

Output: {'matrix': [[19, 22], [43, 50]], 'power': 1024, 'fib': 55}

Example 2

Input: matrix_pow_fib_toolkit([[1, 2, 3], [4, 5, 6]], [[7, 8], [9, 10], [11, 12]], 3, 0, 0, 1000000007)

Output: {'matrix': [[58, 64], [139, 154]], 'power': 1, 'fib': 0}

Example 3

Input: matrix_pow_fib_toolkit([[2]], [[3]], 0, 5, 1, 7)

Output: {'matrix': [[6]], 'power': 0, 'fib': 1}

Hints

Hint 1

Treat the three tasks separately. The matrix product is three nested loops: for each output cell, sum the products along the shared dimension, reducing modulo `modulus` if one is given.

Hint 2

For the power, look at `exp` in binary. Keep a running square of the base, multiply it into the answer whenever the lowest bit of the exponent is 1, then shift the exponent right.

Hint 3

Fibonacci can be written as repeated multiplication by the matrix [[1,1],[1,0]]. Raise it to the power `idx` with the same square-and-multiply loop, starting from the identity matrix, and read `F(idx)` from the top-right entry.