New AVL Tree
Time limit2sMemory limit1024 MB
Given a set S of allowed balance values and a height h, count the tree shapes of height h where every non-leaf node has a balance in S, modulo 1e9+7.
- Level
Medium5 of 10
- Topics
- Tree, Dynamic programming
- Solved
- No attempts yet
Problem
The AVL tree is named after Adelson-Velsky and Landis, who created it.
To define an AVL tree, first define the "balance value" of each node. The balance value of a node is , where is the height of its left subtree and is the height of its right subtree. If the node has no left or right child, the height of that subtree is taken as . In a regular AVL tree, every node has a balance value of , , or .
Minjae, who attended the MatKor data structures seminar, wants to develop an AVLM tree named after himself. He allows only some of the values , , and as balance values. He picks a nonempty subset of and defines an AVLM tree as a tree in which every node except the leaves has a balance value that is an element of . Leaves are exempt because their balance value is always .
Given the set and a height of an AVLM tree, count the number of possible tree shapes of height .
A tree with a single node has height .
Input
The first line contains the number of test cases . ()
Each test case consists of two lines. The first line contains the height and the number of elements , separated by a space. (, )
The second line contains the distinct elements of , which are the allowed balance values of the nodes, in ascending order.
The set is one of , , , , , , .
Each test case appears only once.
Output
For each test case, print the answer modulo on its own line.
Hint
Reading input with fast I/O is necessary when there are many test cases, or the program may exceed the time limit.