Unit conversion ratio queries with modular answers

0
Remix

You have n units, numbered 0 to n - 1, split into separate families: units in one family convert into each other, units from different families never do. You are given conversion facts between pairs of units (one unit equals some multiple of another). Answer a list of queries, each asking how many of unit b make one unit a, or that the conversion is impossible because the two units sit in different families. Ratios can grow huge, so report them modulo a given number (division then becomes multiplication by a modular inverse). Build the families once, then answer each query fast.

AppleTechnical phone screenLast asked Sep 2026
No answer yet. Write the first one; the best answer gets endorsed.