#P4104. Balanced Seesaw Eraser Removal
Balanced Seesaw Eraser Removal
Balanced Seesaw Eraser Removal
There is a seesaw built with a regular triangular prism support and a ruler resting on it. The ruler has 2n+1 equally spaced markings, with the n+1-th marking exactly at its center. Initially, an eraser of identical mass is placed on each marking so that the seesaw is balanced. However, one day Flower removes exactly k erasers arbitrarily from the ruler, and surprisingly the seesaw remains balanced.
Assuming each eraser is a point mass, the seesaw remains balanced if and only if the net torque about the fulcrum is zero. Taking the center marking as the origin, number the markings from -n to n (so that the n+1-th marking corresponds to 0). Originally, the total moment is
After removing a subset S of erasers (of size k), the remaining erasers still produce a zero net moment if and only if the sum of the positions (i.e. the markings relative to the center) of the removed erasers is equal to 0:
Your task is to compute the number of ways to remove exactly k erasers (i.e. choose a subset of k markings from the set \(\{-n, -n+1, \ldots, -1, 0, 1, \ldots, n\}\)) such that the removed indices sum to 0, modulo p.
Note: Two ways are considered different if the sets of removed indices are different.
inputFormat
The input consists of a single line containing three space‐separated integers: n, k, and p.
- n: a positive integer, indicating that the ruler has 2n+1 markings (numbered from -n to n).
- k: the number of erasers to remove.
- p: the modulus.
You may assume that the values of n and k are such that a solution exists within time limits.
outputFormat
Output a single integer representing the number of ways to remove exactly k erasers such that the seesaw remains balanced, modulo p.
sample
1 2 1000
1