Nebius Round 2 (Codeforces Round 1088, Div. 1 + Div. 2)
F
Learning Binary Search
Codeforces
2400
原题
← EG →
翻译中…

You step into your first data structures class, where you are learning about binary search. You heard the professor yapping about why binary search works in $O(\log{n})$. But you want to see if you can find a better bound.

翻译中…

Given a sorted array $a$ of size $n$ and an integer $k$, define $f(a, k, l, r)$ as the result of the following code:

翻译中…

function f(a, k, l, r):
   if a does not contain k:
      return 0
   mid = floor((l+r) / 2)
   if a[mid]==k:
      return 1
   else if a[mid]<k:
      return 1+f(a, k, mid+1, r)
   else:
      return 1+f(a, k, l, mid-1)

翻译中…

You are given two integers $n$ and $m$. Define an array $a$ good if:

翻译中…
  • $|a|=n$ (there are $n$ elements in $a$).
  • $1 \leq a_1 \leq a_2 \leq \ldots \leq a_n \leq m$ (the array is nondecreasing, bounded by $1$ below and bounded by $m$ above).
翻译中…

You are interested in finding the sum of $f(a,1,1,n)+f(a,2,1,n)+\ldots+f(a,m,1,n)$ over all good arrays $a$. Since this answer may be huge, output the answer modulo $676\,767\,677$. Note that $676\,767\,677$ is a prime number.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains two integers $n$ and $m$ ($3 \leq n,m \leq 10^6$).

翻译中…

It is guaranteed that the sum of $n$ does not exceed $10^6$ over all test cases, and the sum of $m$ does not exceed $10^6$ over all test cases.

翻译中…

Output

翻译中…

For each test case, output the requested sum modulo $676\,767\,677$ on a new line.

翻译中…

Note

翻译中…

In the first test case, one good array $a$ is $[2,2,3]$. Here, $f(a,1,1,n)=0$ (as $1$ is not present in $a$), $f(a,2,1,n)=1$, $f(a,3,1,n)=2$. Therefore, the contribution of this good array is $0+1+2=3$.

翻译中…

样例

Input #1
7
3 3
3 4
3 5
4 3
4 5
999967 99967
15 876543
Output #1
26
60
115
50
315
93903683
322710644
InputCopy OutputCopy
题目信息
题号
2211F
时间限制
2 s
内存限制
256 MB
标签
组合数学
分治
动态规划
数学

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。