Binary Search Tree (BST) is a rooted binary tree data structure which has following properties:
If there is a new node to be inserted, the following algorithm will be used:
BST structure depends on its data inserting sequence. Different sequence may yield a different structure though the data set is the same. For example:
Insert sequence: 1 2 3, the BST will be:
If the data is inserted with sequence: 2 1 3, the tree will be:
On the other hand, different data set may have a same BST structure. For example: Insert sequence 2 1 3 will have the same BST structure with 4 6 2, and the tree will be:
Given N nodes BST, calculate how many distinct insert data sequence which result in the same BST structure, assuming that data are taken from range 1..M.
he first line of input contains an integer T (T ≤ 100), the number of test cases. Each case begins with two integers N and M (1 ≤ N ≤ M ≤ 1, 000), the number of nodes in BST and the maximum range respectively. The next line contains N integers Ai (1 ≤ Ai ≤ 1, 000) the insert sequence that construct a BST.
For each case, output an integer denoting the number of distinct insert data sequence which result in the same BST structure, assuming that data are taken from range 1...M.
Modulo this number with 1,000,003.
Note: Explanation for the 1st sample input.
There are 8 insert sequences (data taken from 1..4) which have the same BST:
2 1 3
2 3 1
2 1 4
2 4 1
3 1 4
3 4 1
3 2 4
3 4 2
3 3 4 3 1 4 3 5 1 2 3 4 4 2 1 10 3
8 10 3
Migrated from old NTUJ.
Regional Jakarta 2010
No. | Testdata Range | Score |
---|