QOJ.ac

QOJ

実行時間制限: 2 s メモリ制限: 256 MB 満点: 100

#4127. Equation

統計

Given the equation:

$$X_1+X_2+\dots+X_n=m$$

We have some constraints on the first $1..n_1$ variables: $X_1 \le A_1$ $X_2 \le A_2$ $\dots$ $X_{n_1} \le A_{n_1}$

We have some constraints on the $n_1+1..n_1+n_2$ variables: $X_{n_1+1} \ge A_{n_1+1}$ $X_{n_1+2} \ge A_{n_1+2}$ $\dots$ $X_{n_1+n_2} \ge A_{n_1+n_2}$

Find the number of positive integer solutions to this equation under these constraints. The answer may be very large, so please output the answer modulo $p$, i.e., the remainder of the answer divided by $p$.

Input

The input contains multiple test cases. The first line contains two positive integers $T$ and $p$. $T$ represents the number of data sets in this test point, and the meaning of $p$ is as described in the problem statement.

For each data set, the first line contains four non-negative integers $n, n_1, n_2, m$. The second line contains $n_1+n_2$ positive integers, representing $A_1..A_{n_1+n_2}$. Please note that if $n_1+n_2$ equals $0$, this line will be an empty line.

Output

There are $T$ lines in total, each containing a positive integer representing the answer after modulo $p$.

Examples

Input 1

3 10007
3 1 1 6
3 3
3 0 0 5
3 1 1 3
3 3

Output 1

3
6
0

Note

For the first data set, the three solutions are $(1,3,2), (1,4,1), (2,3,1)$. For the second data set, the six solutions are $(1,1,3), (1,2,2), (1,3,1), (2,1,2), (2,2,1), (3,1,1)$.

Constraints

Test Point ID $n$ $n_1$ $n_2$ $m$ $p$
1 $n \le 5$ $n_1 \le 8$ $n_2 \le 8$ $m \le 10$ $p=10007$
2 $n \le 50$ $n_1=0$ $n_2=0$ $m \le 50$ $p=262203414$
3 $n \le 50$ $n_1 \le 8$ $n_2 \le 8$ $m \le 50$ $p=437367875$
4 $n \le 10^4$ $n_1=0$ $n_2=0$ $m \le 10^4$ $p=10007$
5 $n \le 10^4$ $n_1 \le 8$ $n_2 \le 8$ $m \le 10^4$ $p=10007$
6 $n \le 10^9$ $n_1 \le 8$ $n_2 \le 8$ $m \le 10^9$ $p=10007$
7 $n \le 10^9$ $n_1=0$ $n_2 \le 8$ $m \le 10^9$ $p=262203414$
8 $n \le 10^9$ $n_1 \le 8$ $n_2 \le 8$ $m \le 10^9$ $p=262203414$
9 $n \le 10^9$ $n_1=0$ $n_2=0$ $m \le 10^9$ $p=437367875$
10 $n \le 10^9$ $n_1 \le 8$ $n_2 \le 8$ $m \le 10^9$ $p=437367875$

For 100% of the test data: $T \le 5$, $1 \le A_{1..n_1+n_2} \le m$, $n_1+n_2 \le n$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.