#P9149. 串串题

    ID: 9140 远端评测题 2000ms 256MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>字符串数学洛谷原创O2优化组合数学KMP 算法洛谷月赛

串串题

Problem Description

Given two integer sequences AA and BB of lengths nn and mm, and constants WW and dd, the sequences are indexed starting from 11. It is guaranteed that Ai,Bi[1,W]A_i, B_i \in [1,W].

It is easy to see that there are (Wd)\binom{W}{d} ways to choose dd pairwise distinct integers from [1,W][1,W].

For each choice, we delete from AA all occurrences of the corresponding dd integers. Let the number of times sequence BB occurs in sequence AA at this time be the weight of this choice.

You need to compute the sum of the weights over all choices, modulo 109+7{10}^9+7.

If you are unsure about the statement, please read the samples and the sample explanation.

Note: (ab)\binom{a}{b} denotes a binomial coefficient, meaning the number of ways to choose bb items from aa items without order.

Please note: we will not delete the corresponding integers that appear in sequence B\bm{B}.

Input Format

This problem contains multiple test cases.

The first line contains a positive integer TT, the number of test cases. For each test case:

The first line contains four positive integers n,m,W,dn, m, W, d, with dWd \le W.

The second line contains nn positive integers A1,A2,,AnA_1, A_2, \ldots, A_n, representing sequence AA.

The third line contains mm positive integers B1,B2,,BmB_1, B_2, \ldots, B_m, representing sequence BB.

Output Format

For each test case, output one integer: the answer modulo 109+7{10}^9+7.

2
4 2 3 1
1 1 2 1
1 1
8 3 4 1
1 2 3 1 2 3 1 2
1 2 1

3
2

Hint

Sample Explanation

In the first test case of the sample:

  1. If we choose to delete the number 11 from AA, then AA becomes {2}\{2\}, and the number of occurrences of BB in AA is 00.
  2. If we choose to delete the number 22 from AA, then AA becomes {1,1,1}\{1,1,1\}, and the number of occurrences of BB in AA is 22.
  3. If we choose to delete the number 33 from AA, then AA becomes {1,1,2,1}\{1,1,2,1\}, and the number of occurrences of BB in AA is 11.

Therefore, the answer for the first test case is 0+2+1=30+2+1=3.

Reminder again: we will not delete the corresponding integers that appear in sequence B\bm{B}.


Constraints

For 100%100\% of the data, 1n,m,W1061 \le n,m,W \le {10}^6, 1d,Ai,BjW1 \le d, A_i, B_j \le W, and 1T51 \le T \le 5.

This problem uses bundled testdata and enables subtask dependencies.

Subtask nn \le mm \le WW \le Special Property Score Dependency
1 1010 55 1010 \
2 10001000 2020 Subtask 1
3 A 1515 \
4 B 2525
5 3030 Subtask 1, 2, 3, 4

Special Property A: guaranteed that d=1d=1.

Special Property B: let cc be the total number of values that appear only in sequence AA but do not appear in sequence BB. It is guaranteed that c5c \le 5.

Translated by ChatGPT 5