In the Hall of Patterns, the Mathematician of Prime City constructs Pascal's Triangle ÔÇö a triangular array where each number is the sum of the two above it. The Mathematician needs the entire triangle modulo a prime, computed efficiently using the recurrence C(n,k) = C(n-1,k-1) + C(n-1,k). "Pascal's Triangle gives all binomial coefficients," the Mathematician says. "Build it row by row using the additive recurrence, taking mod p at each step. This avoids needing modular inverses." Given n and a prime p, output the nth row (0-indexed) of Pascal's Triangle modulo p. The row has n+1 elements, space-separated. Constraints: 0 <= n <= 5000, 2 <= p <= 10^9 (prime) Input: 4 1000000007 Output: 1 4 6 4 1 Input: 5 7 Output: 1 5 3 1 5 3
Constraints:
0 <= n <= 5000, 2 <= p <= 10^9 (prime)
Tags:
