#P3301. [SDOI2013] 方程

    ID: 2350 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2013山东中国剩余定理,CRT组合数学容斥

[SDOI2013] 方程

Description

Given the equation x1+x2++xn=mx_1+x_2+\dots +x_{n}=m.

We impose restrictions on variables 11 through n1n_1: $x_{1} \le a_{1},x_{2} \le a_{2},\dots, x_{n_1} \le a_{n_1}$.

We impose restrictions on variables n1+1n_1+1 through n1+n2n_1+n_2: $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 of the equation under these restrictions. The answer can be large; please output it modulo pp.

Input Format

Multiple test cases.

The first line contains two positive integers T,pT,p. TT denotes the number of test cases in this file, and the meaning of pp is as described above.

For each test case, the first line contains four non-negative integers n,n1,n2,mn,n_1,n_2,m.

The second line contains n1+n2n_1+n_2 positive integers, denoting a1,a2,,an1+n2a_{1},a_2,\dots, a_{n_1+n_2}. Note that if n1+n2n_1+n_2 equals 00, this line will be empty.

Output Format

Output TT lines, each containing a single integer representing the answer modulo pp.

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

3 1 1 3
3 3

3
6
0

Hint

【Sample explanation】

For the first test case, the three solutions are (1,3,2)(1,3,2), (1,4,1)(1,4,1), (2,3,1)(2,3,1). For the second test case, the six solutions are (1,1,3)(1,1,3), (1,2,2)(1,2,2), (1,3,1)(1,3,1), (2,1,2)(2,1,2), (2,2,1)(2,2,1), (3,1,1)(3,1,1).

Constraints: For 100%100\% of the testdata, 1T51\le T \le 5, 1m,n1091\le m, n \le 10^9, 1aim1 \le a_i \le m, 0n1,n2min(8,m)0 \le n_1,n_2 \le \min(8, m) and n1+n2nn_1 + n_2 \le n, 1p4373678751\le p \le 437367875.

Translated by ChatGPT 5