SQRBR - Square Brackets

You are given:

  • a positive integer n,
  • an integer k, 1<=k<=n,
  • an increasing sequence of k integers 0 < s1 < s2 < ... < sk <= 2n.

What is the number of proper bracket expressions of length 2n with opening brackets appearing in positions s1, s2 ... sk?

Illustration

Several proper bracket expressions:

[[]][[[]][]] 
[[[][]]][][[]]

An improper bracket expression:

[[[][]]][]][[]]

There is exactly one proper expression of length 8 with opening brackets in positions 2, 5 and 7.

Task

Write a program which for each data set from a sequence of several data sets:

  • reads integers n, k and an increasing sequence of k integers from input,
  • computes the number of proper bracket expressions of length 2n with opening brackets appearing at positions s1,s2, ... sk,
  • writes the result to output.

Input

The first line of the input file contains one integer d, 1 <= d <= 10, which is the number of data sets. The data sets follow. Each data set occupies two lines of the input file. The first line contains two integers n and k separated by single space, 1 <= n <= 19, 1 <= k <= n. The second line contains an increasing sequence of k integers from the interval [1;2n] separated by single spaces.

Output

The i-th line of output should contain one integer - the number of proper bracket expressions of length 2n with opening brackets appearing at positions s1, s2 ... sk.

Example

Sample input:
5 
1 1 
1 
1 1 
2 
2 1 
1 
3 1 
2 
4 2 
5 7 

Sample output:
1 
0 
2 
3 
2 

Added by:adrian
Date:2004-06-22
Time limit:3s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All
Resource:III Polish Collegiate Team Programming Contest (AMPPZ), 1998

hide comments
2014-08-04 16:07:23 AmirShams
nice one
2014-07-02 07:38:42 Prakhar Gupta
can someone explain 4th test case??
2014-02-02 22:37:21 Vipul Pandey
easy 2d dp.
2014-01-18 14:14:27 Aradhya Makkar
@ xqj , maybe easy for chinese but not for the rest of us.
2013-09-13 16:09:32 siddhant bhatt
dont give up!
2012-07-19 13:18:29 npsabari
Easy one! though not beginners DP..
2012-06-13 16:04:52 Romal Thoppilan
ya i also thought it was easy enough!


Last edit: 2012-08-23 15:31:55
2012-06-07 04:36:30 Rahul Bansal
beginners DP problem.
2012-04-10 00:05:27 xqj
It is a easy dynamic programming.

Last edit: 2012-04-10 00:22:24
2012-04-03 18:24:04 Hussain Kara Fallah
Nice DP Problem
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.