PracticeDynamic Programming

Ranked Panel Selection Report

Easy/MediumDynamic Programmingaccumulationstate trackinginvariants Time not estimated Not started

Context

A reporting team analyzes a pool of ranked nominees and needs to summarize how many distinct panels of a chosen size could be formed. The report displays this count modulo a configured positive base so large values remain manageable. Given the pool size, panel size, and modulus, calculate the requested modular count.

Problem

For a pool of n nominees, a panel of r nominees can be formed in C(n, r) distinct ways. Return C(n, r) modulo modulus. Compute the value using Pascal's recurrence. Begin with the constant coefficient C(0, 0) = 1, then process rows through n. Store only coefficients up to r. When extending each row, update positions from r downward so that every addition uses values from the preceding row rather than values already changed in the current row. Reduce each stored value modulo modulus after adding. The inputs guarantee 0 ≤ r ≤ n and modulus ≥ 1. If modulus is 1, the returned residue is 0.

Examples

Example 1
Input: modulus = 100n = 5r = 2
Output: 10
Explanation: For n=5, r=2, and modulus=100, the final state is [1, 5, 10]; returning position 2 produces 10.
Example 2
Input: modulus = 7n = 6r = 3
Output: 6
Explanation: For n=6, r=3, and modulus=7, the final state is [1, 6, 1, 6]; returning position 3 produces 6.
Example 3
Input: modulus = 13n = 4r = 0
Output: 1
Explanation: For n=4, r=0, and modulus=13, the final state is [1]; returning position 0 produces 1.

Constraints

  • 0 <= n <= 1000
  • 0 <= r <= n
  • 1 <= modulus <= 1000000000
  • Types: n is int, r is int, modulus is int; result is int

Function signature

def panel_selection_residue(n: int, r: int, modulus: int) -> int
n int
The total number of nominees.
r int
The number of nominees selected for the panel.
modulus int
The positive base used to reduce the panel count.
returns int
The number of distinct r-nominee panels from n nominees, reduced modulo modulus.

Adapted from MBPP problem task_402 (CC BY 4.0). Rewritten, extended and verified by Iksha.

Approved · pf1-abs_856_v214 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1