Design a Tree
Time limit3sMemory limit256 MB
Count binary tree shapes with exactly N left edges and M right edges modulo 9999991 for up to 10000 queries.
- Level
Medium7 of 10
- Topics
- Combinatorics, Dynamic programming, Math
- Solved
- No attempts yet
Problem
You are a garden designer. You want to grow a new style of tree called the Left-Right Tree. A Left-Right Tree satisfies all of the following.
- A Left-Right Tree is a binary tree.
- A Left-Right Tree has exactly one root node.
- A Left-Right Tree has exactly left sticks and right sticks.
A left stick is the edge joining a node to its left child, and a right stick is the edge joining a node to its right child. A Left-Right Tree therefore has nodes. Two Left-Right Trees whose shapes differ count as different trees.
Drawing every Left-Right Tree is impossible because there are too many of them, so count them instead. Print the number of possible Left-Right Trees modulo .
Input
The first line has the number of queries . ()
Each of the next lines has the number of left sticks and the number of right sticks , separated by a space. ()
Output
Print lines. On line , print the answer to query modulo .
Hint
When and there are three Left-Right Trees: the root with one left child and one right child, the root with a left child that has a right child, and the root with a right child that has a left child.
The Left-Right Trees for and are shown below.
