D. Token Removing
Problem Statement
Define a sequence of integers valid if and only if .
Define the weight of a valid sequence of length :
- Initially, a token is placed at each integer point in the closed segment on the number axis.
- Perform operations in sequence. During the -th operation, if , remove a token in the closed segment that has not been removed; otherwise, do nothing.
- is the number of ways to remove tokens. Two ways are considered different if there exists a such that the positions of the tokens removed by the two ways are different at the -th operation.
- For example, , because we can remove tokens on or in sequence.
JT gives you two integers and asks you to find the sum of the weights over all valid sequences of length . Since the answer may be too large, print it modulo .
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The only line of each test case contains two integers and (, ) — the length of valid sequences, and the modulus.
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output an integer — the sum of the weights over all valid sequences of length , modulo .
Solution
The goal is to calculate the sum of weights f(a) over all possible valid sequences a, modulo m. The weight f(a) is the number of ways to remove tokens based on the sequence a.
1. The Core Idea: Changing Perspective
Instead of iterating through every possible sequence a and calculating its contribution, the solution flips the problem on its head. We ask a different question:
For a specific set of token positions P that are removed, what is its total contribution to the final answer?
If we can calculate this for any given set P, we can then sum these contributions over all possible sets P to get the final answer.
2. Contribution of a Single Set of Tokens P
Let’s assume we decide to remove a specific set of tokens located at positions . For convenience, let’s sort them in descending order: .
For this set P to be removed, we need to find a sequence and a corresponding removal plan. This involves two main parts:
2.1 Choosing values
For each token that is removed at some step , the condition is . The value can be any integer from 1 to . This gives choices for . For all tokens, the total number of choices for the values is the product .
2.2 Assigning removal steps
We need to assign a unique removal step to each token such that .
- For the largest token , it can be removed at any step from to . There are available steps.
- For the second largest, , it can be removed at any step from to , except for the step already taken by . Since , the step for is always a valid (but unavailable) step for . This leaves choices.
- Generalizing, for the token (which is the -th largest, with starting from 0), there are potential steps. However, of these steps have already been assigned to the tokens larger than . This leaves choices for .
2.3 Final Contribution Formula
Combining these two parts, the total contribution for a single, fixed set P is:
Our new goal is to sum this value over all possible non-empty subsets of .
3. The Dynamic Programming Solution
Enumerating all subsets P is too slow. We can use dynamic programming to calculate this sum efficiently. The key is to build the sets P one element at a time.
The crucial insight is to consider elements in descending order, from down to .
3.1 DP State Definition
Let’s define our DP state:
= The sum of contributions for all sets of size where all chosen tokens are from the range
3.2 DP Transition
We compute this by iterating from down to . For each , we make a decision:
Don’t include in our set P: If we don’t choose , we must still choose tokens, but now they must all come from the range . The sum of contributions for this case is, by definition, .
Include in our set P: If we choose , we need to select other tokens from the range .
- The sum of contributions for all such sets of tokens from is .
- Now, we add to each of these sets. Since is smaller than any number in , it will always be the smallest element in the new -element set. This means its rank (from largest, 0-indexed) will always be .
- The multiplicative factor for is therefore .
- The total contribution for this case is .
3.3 The DP Recurrence
Combining these cases, we get the recurrence:
Base Cases:
- (representing one way to choose an empty set)
- for
Final Answer: The final answer is the sum of contributions for all possible set sizes , considering all numbers from 1 to . This is .