ProgrammingVery hard

Which method is correct and fast enough for n choose r modulo p = 10^9 + 7 for 10^6 queries with n up to 10^6?

  1. A. Precompute factorials and inverse factorials in O(N), then each query is O(1) using modular inverses
  2. B. Build the full Pascal triangle up to n = 10^6, which needs about 5 x 10^11 cells
  3. C. Compute n! from scratch with a loop for every query, about 10^12 steps
  4. D. Recurse on Pascal's rule without memoisation for each query

Correct answer: A. Precompute factorials and inverse factorials in O(N), then each query is O(1) using modular inverses

Factorials cost O(N) once and each query is a few multiplications plus one modular power. The Pascal table would need on the order of 5 x 10^11 cells.

More C Programming questions

Exams that ask this topic