Problem Statement :

You are situated in an N dimensional grid at position (x1,x2,...,xN). The dimensions of the grid are (D1,D2,...DN). In one step, you can walk one step ahead or behind in any one of the N dimensions. (So there are always 2×N possible different moves). In how many ways can you take M steps such that you do not leave the grid at any point? You leave the grid if at any point xi, either xi≤0 or xi>Di.

Input Format

The first line contains the number of test cases T. T test cases follow. For each test case, the first line contains N and M, the second line contains x1,x2,…,xN and the 3rd line contains D1,D2,…,DN.

Output Format

Output T lines, one corresponding to each test case. Since the answer can be really huge, output it modulo 1000000007.







Sample Input


2 3

1 1

2 3

Sample Output


If this was in 1D the solution can be like this : solve(i+1)+solve(i-1);

in 2D : solve(i+1,j)+solve(i-1,j)+solve(i,j+1)+solve(i,j-1); How can i program it for N Dimensions? Is their some general steps for making recursion statements like above which could help in making recursive statements

Most solutions which i saw are in bottom up or top down manner i am not able to understand them ? is their any way to understand them ,as i have always practiced dp using recursion + memoization i find it hard to understand them

  • $\begingroup$ Not sure but seems more suited for stackoverflow. $\endgroup$ Nov 17, 2015 at 5:27
  • $\begingroup$ ok i will copy it there . but i am not able to get which type of question are for stackoverflow and which for cs , if i post there they say post it here $\endgroup$
    – JSONParser
    Nov 17, 2015 at 5:31
  • $\begingroup$ @sesy1, I recommend that you do not copy your post onto both Stack Overflow and CS.SE. Site rules state that cross-posting on multiple Stack Exchange sites is not allowed. It looks like you've already gotten an answer here at the duplicate. As far as what is on-topic here, questions about algorithms are on-topic here, but coding questions (how do I implement it?) are off-topic here. Sasha might have been referring to your question "How can I program it?". Note that we discourage problem dumps: we want you to gain understanding, not do your exercise/contest problem for you. $\endgroup$
    – D.W.
    Nov 17, 2015 at 17:25


Browse other questions tagged or ask your own question.