Neil and his Binary Tree CodeChef Solution – Queslers

Problem: Neil and his Binary Tree CodeChef Solution

Neil has a perfect binary tree with NN nodes, and an integer MM. He can assign values to nodes. He calls the tree good if the following conditions are met:

  • Nodes’ values are positive integers no more than MM.
  • Nodes at even levels have values strictly more than their parents’ values.
  • Nodes at odd levels have values strictly less than their parents’s values.

How many ways can Neil assign values to all nodes to get a good perfect binary tree? Since the answer can be large, please output it under modulo 109+7109+7.


  • The root of the tree is at layer 11.
  • Two assignments are different if there is at least one node with different values in both assignments.
  • You may need to use 64-bit data types in your programming language to take input.

Input Format

  • The first line of each input contains TT – the number of test cases. The test cases then follow.
  • The only line of each test case contains two space-separated integers NN and MM – the number of nodes on the tree and the maximum value of any node.

Output Format

For each test case, output on a single line the number of different assignment modulo 109+7109+7.


  • 1≤T≤1001≤T≤100
  • 1≤M≤10001≤M≤1000
  • 1≤N<2591≤N<259
  • It is guaranteed you can make a perfect binary tree with exactly NN nodes.

Sample Input 1 

3 3

Sample Output 1 



  • Test case 11: Here are all the possible assignments.
Neil and his Binary Tree CodeChef Solution Review:

In our experience, we suggest you solve this Neil and his Binary Tree CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

Neil and his Binary Tree Problem is available on Hacker Rank for Free, if you are stuck anywhere between a compilation, just visit Queslers to get (Neil and his Binary Tree) CodeChef Solution.


I hope this Neil and his Binary Tree CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Hacker Rank, Leetcode, Codechef, Codeforce Solution.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More on Queslers >>

CodeChef Solution

Cognitive Class Answers

Leetcode Solution

Coursera Quiz Answers

Leave a Reply

Your email address will not be published. Required fields are marked *