P28407 [洛谷] [CERC2017] Cumulative Code
提高+
题目描述

Problem Description

As you probably know, a tree is a graph consisting of $n$ nodes and $n - 1$ undirected edges in which any two nodes are connected by exactly one path. In a labeled tree each node is labeled with a different integer between $1$ and $n$.

The Prüfer code of a labeled tree is a unique sequence associated with the tree, generated by repeatedly removing nodes from the tree until only two nodes remain. More precisely, in each step we remove the leaf with the smallest label and append the label of its neighbour to the end of the code. Recall, a leaf is a node with exactly one neighbour. Therefore, the Prüfer code of a labeled tree is an integer sequence of length $n - 2$. It can be shown that the original tree can be easily reconstructed from its Prüfer code.

The complete binary tree of depth $k$, denoted with $C_k$, is a labeled tree with $2^k - 1$ nodes where node $j$ is connected to nodes $2j$ and $2j + 1$ for all $j < 2^{k-1}$. Denote the Prüfer code of $C_k$ with $p_1, p_2, ..., p_{2^k-3}$.

Since the Prüfer code of $C_k$ can be quite long, you do not have to print it out. Instead, you need to answer $q$ questions about the sums of certain elements in the code. Each question consists of three integers: $a, d$ and $m$. The answer is the sum of the elements $p_a, p_{a+d}, p_{a+2d}, ..., p_{a+(m-1)d}$ in the Prüfer code of $C_k$.

Input Format

The first line contains two integers $k$ and $q$ ($2 \le k \le 30, 1 \le q \le 300$) — the depth of the complete binary tree and the number of questions. The $j$-th of the following $q$ lines contains the $j$-th question: three positive integers $a_j, d_j$ and $m_j$ such that $a_j, d_j$ and $a_j + (m_j - 1)d_j$ are all at most $2^k - 3$.

Output Format

Output $q$ lines. The $j$-th line should contain a single integer — the answer to the $j$-th question.

Sample Input and Output

Input Sample #1

3 5
1 1 1
2 1 1
3 1 1
4 1 1
5 1 1

Output Sample #1

2
2
1
3
3

Input Sample #2

4 4
2 1 5
4 4 3
4 8 1
10 3 2

Output Sample #2

18
15
5
13

Input Sample #3

7 1
1 1 125

Output Sample #3

4031

Notes

In the first example above, when constructing the Prüfer code for $C_3$ the nodes are removed in the following order: $4, 5, 2, 1, 6$. Therefore, the Prüfer code of $C_3$ is $2, 2, 1, 3, 3$.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
3 5 1 1 1 2 1 1 3 1 1 4 1 1 5 1 1
样例输出
2 2 1 3 3