Crazy Tree 2
Practice
3.7 (162 votes)
Medium
Implementation
Recursion
Tree
Mathematics
Approved
Problem
84% Success 1071 Attempts 30 Points 1s Time Limit 256MB Memory 100 KB Max Code

Abhimanyu makes a complete binary tree of level L, called Crazy Tree. The levels are numbered 1, 2, ..., L from top to bottom. The root of the tree is at level 1. He numbers all the leaf nodes 1, 2, 3, ... from left to right. The value of every parent node is the product of values of its child nodes.

Below are Level 2 (left) and Level 3 (right) Crazy Trees.

Level 2 Crazy Tree Level 3 Crazy Tree

Abhimanyu has a magical function S:

  • S(Z): This function gives sum of all the nodes at level Z

For two magical integers X and Y, Abhimanyu wants to find the value of W % M, where:

  • W = S(X) + S(X + 1) + ... + S(Y)
  • M = 1299709

Input

First line of input contains two space separated integers L and Q, where L is the number of levels in Crazy Tree and Q is number of queries. Each of next Q lines contains two space separated integers X and Y.

Output

Output W % M, for each test case in single line.

Constraints

  • 1 <= L <= 21
  • 1 <= Q <= L (L + 1) / 2
  • 1 <= X <= L
  • X <= Y <= L
  • M = 1299709

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
Tags:
Easy
Points:50
17 votes
Tags:
Data StructuresTreeApprovedMedium-HardSegment tree
Points:50
37 votes
Tags:
Data StructuresDisjoint setGraphsMapsMediumReadyTrees