#P7143. [THUPC 2021 初赛] 线段树

[THUPC 2021 初赛] 线段树

Description

The segment tree is Little L’s favorite data structure. It can efficiently solve many practical problems.

Given a positive integer nn, Little L builds a segment tree whose indices belong to the integer interval [1,n][1, n]:

  • Initially, the segment tree has only one node [1,n][1, n].
  • For a node [L,R][L, R], if L<RL < R, let mid=[L+R2]mid = \left[ \frac{L + R}{2} \right] (where [x][x] denotes the greatest integer not exceeding xx). Little L creates two child nodes for this node: [L,mid][L, mid] and [mid+1,R][mid + 1, R].

Little L defines a function cover(a,b)cover(a, b) (1abn1 \le a \le b \le n), which means using several segment tree nodes to cover the interval [a,b][a, b] without overlap and without omission. cover(a,b)cover(a, b) is the minimum possible number of segment tree nodes used.

Little L tries to use this segment tree to solve a complex problem, and wants to roughly evaluate the performance of this segment tree.

Specifically, the interval [1,n][1, n] has n(n+1)2\frac{n (n + 1)}{2} different sub-intervals. If Little L randomly selects one sub-interval from these n(n+1)2\frac{n (n + 1)}{2} sub-intervals with equal probability, denoted as [A,B][A, B], then Little L believes the expected value of cover(A,B)cover(A, B) can be used to evaluate the performance of this segment tree.

Little L wants you to help compute the result of E[cover(A,B)]\mathbb{E}[cover(A, B)] multiplied by n(n+1)2\frac{n (n + 1)}{2} modulo 1,000,000,0071, 000, 000, 007. You may find that this result must be an integer.

Input Format

The first line contains a positive integer TT (1T10001 \le T \le 1000), indicating the number of test cases.
The next TT lines follow. In the ii-th line (1iT1 \le i \le T), there is a positive integer nn (1n10181 \le n \le {10}^{18}), indicating the value of nn for the ii-th test case.

Output Format

Output TT lines. In the ii-th line (1iT1 \le i \le T), output one integer representing the answer for the ii-th test case.

1
3

7

Hint

[Sample Explanation #1]

cover(1,1)=1cover(1, 1) = 1, cover(2,2)=1cover(2, 2) = 1, cover(3,3)=1cover(3, 3) = 1, cover(1,2)=1cover(1, 2) = 1, cover(2,3)=2cover(2, 3) = 2, cover(1,3)=1cover(1, 3) = 1. Therefore, the expectation of cover(A,B)cover(A, B) is =1+1+1+1+2+16=76= \frac{1 + 1 + 1 + 1 + 2 + 1}{6} = \frac{7}{6}.

[Source]

From the preliminary round of the 2021 Tsinghua University Student Algorithm Contest and Collegiate Invitational (THUPC2021).

Resources such as the editorial can be found at https://github.com/THUSAAC/THUPC2021-pre.

Translated by ChatGPT 5