Gross LCS

아직 제출이 없습니다시간 제한10초메모리 제한16 MB

문제

Note that the memory limit is unusually low.

Let LCS(A,B)\operatorname{LCS}(A, B) denote the length of the longest common subsequence of integer sequences A=a_1,a_2,,a_nA = \langle a\_1, a\_2, \ldots, a\_n \rangle and B=b_1,b_2,,b_mB = \langle b\_1, b\_2, \ldots, b\_m \rangle.

For an integer xx, let A+xA + x denote the sequence a_1+x,a_2+x,,a_n+x\langle a\_1 + x, a\_2 + x, \ldots, a\_n + x \rangle.

You are given two integer sequences AA and BB. Find the sum of LCS(A+x,B)\operatorname{LCS}(A + x, B) over all integers xx from 10100-10^{100} to 1010010^{100}.

입력

The first line contains two integers nn and mm (1n,m40001 \le n, m \le 4000).

The second line contains nn integers a_1,a_2,,a_na\_1, a\_2, \ldots, a\_n (108a_i108-10^8 \le a\_i \le 10^8).

The third line contains mm integers b_1,b_2,,b_mb\_1, b\_2, \ldots, b\_m (108b_i108-10^8 \le b\_i \le 10^8).

출력

Print the sum of LCS(A+x,B)\operatorname{LCS}(A + x, B) over all integers xx from 10100-10^{100} to 1010010^{100}.

힌트

An integer sequence PP is a subsequence of an integer sequence QQ if PP can be obtained from QQ by deletion of several (possibly zero or all) elements. The longest common subsequence of sequences AA and BB is the longest sequence CC that is a subsequence of both AA and BB.

In the example test:

  • LCS(A5,B)=LCS(0,0,3,3,6,3,6)\operatorname{LCS}(A - 5, B) = \operatorname{LCS}(\langle 0, 0, 3 \rangle, \langle 3, 6, 3, 6 \rangle) = 1;
  • LCS(A2,B)=LCS(3,3,6,3,6,3,6)\operatorname{LCS}(A - 2, B) = \operatorname{LCS}(\langle 3, 3, 6 \rangle, \langle 3, 6, 3, 6 \rangle) = 3;
  • LCS(A+1,B)=LCS(6,6,9,3,6,3,6)\operatorname{LCS}(A + 1, B) = \operatorname{LCS}(\langle 6, 6, 9 \rangle, \langle 3, 6, 3, 6 \rangle) = 2;
  • LCS(A+x,B)=0\operatorname{LCS}(A + x, B) = 0 for any x5,2,1x \notin \\{ -5, -2, 1 \\}.

Therefore the answer is 1+3+2=61 + 3 + 2 = 6.