#P5343. 【XR-1】分块

    ID: 4271 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>动态规划,dp动态规划优化矩阵加速,矩阵优化矩阵乘法

【XR-1】分块

Description

There is a sequence of length nn, and xht37 now wants to maintain it using block decomposition.

PinkRabbit requires that he may only divide the sequence into blocks whose lengths come from PRPR allowed lengths.

NaCly_Fish requires that he may only divide the sequence into blocks whose lengths come from NFNF allowed lengths.

The same person may request the same block length multiple times.

xht37 wants to satisfy both PinkRabbit and NaCly_Fish, so he has to use block lengths that are allowed by both of them.

xht37 wants to know how many different block decomposition schemes there are. Output the answer modulo 109+710 ^ 9 + 7.

Input Format

The first line contains a positive integer nn, which is the length of the sequence.

The second line contains a positive integer PRPR, which is the number of allowed block lengths requested by PinkRabbit.

The third line contains PRPR positive integers, representing the PRPR block lengths requested by PinkRabbit.

The fourth line contains a positive integer NFNF, which is the number of allowed block lengths requested by NaCly_Fish.

The fifth line contains NFNF positive integers, representing the NFNF block lengths requested by NaCly_Fish.

Output Format

Output one line with one integer, which is the number of different block decomposition schemes modulo 109+710 ^ 9 + 7.

4
3
1 2 3
3
1 2 4
5
19260817
7
8 9 6 3 7 2 1
7
4 5 2 9 7 8 3
859254329

Hint

[Sample 11 Explanation]

The block lengths allowed by both PinkRabbit and NaCly_Fish are {1,2}\{1,2\}.

For a sequence of length 44, the schemes that divide it into blocks with lengths in {1,2}\{1,2\} are:

  • 1 1 1 11\ 1\ 1\ 1
  • 1 1 21\ 1\ 2
  • 1 2 11\ 2\ 1
  • 2 1 12\ 1\ 1
  • 2 22\ 2

There are 55 schemes in total.

[Constraints]

Let the maximum block length be xx.

For 60%60 \% of the testdata, 1n1061 \le n \le 10 ^ 6, 1PR,NF,x101 \le PR,NF,x \le 10, and it is guaranteed that the same person will not request the same block length multiple times.

For 100%100 \% of the testdata, 1n10181 \le n \le 10 ^ {18}, 1PR,NF,x1001 \le PR,NF,x \le 100.

Translated by ChatGPT 5