CF2106C.Cherry Bomb

传统题 时间 2000 ms 内存 256 MiB 5 尝试 1 已通过 1 标签

Cherry Bomb

Two integer arrays aa and bb of size nn are complementary if there exists an integer xx such that ai+bi=xa_i + b_i = x over all 1in1 \le i \le n. For example, the arrays a=[2,1,4]a = [2, 1, 4] and b=[3,4,1]b = [3, 4, 1] are complementary, since ai+bi=5a_i + b_i = 5 over all 1i31 \le i \le 3. However, the arrays a=[1,3]a = [1, 3] and b=[2,1]b = [2, 1] are not complementary.

Cow the Nerd thinks everybody is interested in math, so he gave Cherry Bomb two integer arrays aa and bb. It is known that aa and bb both contain nn non-negative integers not greater than kk.

Unfortunately, Cherry Bomb has lost some elements in bb. Lost elements in bb are denoted with 1-1. Help Cherry Bomb count the number of possible arrays bb such that:

  • aa and bb are complementary.
  • All lost elements are replaced with non-negative integers no more than kk.

Input

The first line of the input contains a single integer tt (1t1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains two integers nn and kk (1n21051 \le n \le 2 \cdot 10^5, 0k1090 \le k \le 10^9) — the size of the arrays and the maximum possible value of their elements.

The second line contains nn integers a1,a2,...,ana_1, a_2, ..., a_n (0aik0 \le a_i \le k).

The third line contains nn integers b1,b2,...,bnb_1, b_2, ..., b_n (1bik-1 \le b_i \le k). If bi=1b_i = -1, then this element is missing.

It is guaranteed that the sum of nn over all test cases does not exceed 21052 \cdot 10^5.

Output

Output exactly one integer, the number of ways Cherry Bomb can fill in the missing elements from bb such that aa and bb are complementary.

Note

In the first example, the only way to fill in the missing elements in bb such that aa and bb are complementary is if b=[2,0,1]b = [2, 0, 1].

In the second example, there is no way to fill in the missing elements of bb such that aa and bb are complementary.

In the fourth example, some bb arrays that are complementary to aa are: [4,2,3,0,1],[7,5,6,3,4],[4, 2, 3, 0, 1], [7, 5, 6, 3, 4], and [9,7,8,5,6][9, 7, 8, 5, 6].

Samples

7
3 10
1 3 2
-1 -1 1
5 1
0 1 0 0 1
-1 0 1 0 -1
5 1
0 1 0 0 1
-1 1 -1 1 -1
5 10
1 3 2 5 4
-1 -1 -1 -1 -1
5 4
1 3 2 1 3
1 -1 -1 1 -1
5 4
1 3 2 1 3
2 -1 -1 2 0
5 5
5 0 5 4 3
5 -1 -1 -1 -1
1
0
0
7
0
1
0

在线编程 IDE

建议全屏模式获得最佳体验