Crazy Tree 2

3.7

162 votes
Medium, Implementation, Recursion, Tree, Mathematics, Approved
Problem

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
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

The tree is Level 3 tree. So, S(1) = 24 S(2) = 14 S(3) = 10

Editor Image

?