KatInterview

Fast Power Residue Plus Nearest-Source Grid Map

Trees & GraphsAuto-graded · 18 test cases
MediumAuto-gradedTrees & Graphsbfsmodular-exponentiationgrid

Last updated

Combine square-and-multiply modular exponentiation with a multi-source BFS that labels every grid cell with its distance to the nearest source cell.

Write a single function that answers two unrelated questions and returns both answers together.

Task A: Power residue

Given integers base, exp and mod, compute (base ^ exp) mod mod.

  • exp may be as large as 10^18, so repeated multiplication exp times is far too slow. Your approach should take time proportional to the number of bits in exp.
  • mod is a positive integer, at most 10^9 + 7.
  • When exp is 0, the answer is 1 mod mod. In particular, when mod is 1 the answer is always 0.
  • A negative base should be treated as its non-negative residue modulo mod.

Task B: Distance to the nearest source

You are given a rectangular grid grid of 0s and 1s. A cell containing 1 is a source; a cell containing 0 is empty. Movement is allowed one step at a time to the four orthogonal neighbours (up, down, left, right), and there are no obstacles.

Produce a grid of the same shape in which each cell holds the minimum number of steps needed to reach any source starting from that cell.

  • A source cell has distance 0.
  • If the grid contains no source at all, every cell gets the value -1.
  • If the grid is empty (no rows), the distance grid is an empty list.

Return value

Return a dictionary with exactly two keys:

  • "power": the integer result of Task A.
  • "distances": the list of lists produced by Task B.

Constraints

  • 0 <= exp <= 10^18
  • 1 <= mod <= 10^9 + 7
  • base is an integer (it may be negative or larger than mod).
  • The grid has at most 500 rows and 500 columns.
  • Every grid entry is 0 or 1.
  • The grid may contain no sources, or consist only of sources.

Examples

Example 1

Input: power_and_nearest_source(2, 10, 1000, [[0, 0, 0], [0, 1, 0], [0, 0, 0]])

Output: {'power': 24, 'distances': [[2, 1, 2], [1, 0, 1], [2, 1, 2]]}

Example 2

Input: power_and_nearest_source(7, 0, 13, [[1, 0], [0, 1]])

Output: {'power': 1, 'distances': [[0, 1], [1, 0]]}

Example 3

Input: power_and_nearest_source(123, 1000000000000000000, 1, [[0, 0], [0, 0]])

Output: {'power': 0, 'distances': [[-1, -1], [-1, -1]]}

Hints

Hint 1

For the power part, inspect the bits of the exponent: square the running base at every step and multiply it into the answer only when the current bit is 1, reducing modulo `mod` each time.

Hint 2

Start the answer as `1 % mod` rather than `1` so that `mod = 1` and `exp = 0` need no special handling.

Hint 3

For the grid, do not search from each source separately. Put all sources into one queue at distance 0 and expand outward layer by layer; the first time a cell is reached gives its minimum distance.

Hint 4

Use `-1` in the distance grid as the marker for 'not reached yet', and set a cell's value when you enqueue it so it is never queued twice.