#P9362. [ICPC2022 Xi'an R] Find Maximum

[ICPC2022 Xi'an R] Find Maximum

题目描述

We define a function f(x)f(x) over all non-negative integer xx as follows:

$$f(x) = \begin{cases} 1 & (x = 0) \\ f(\frac{x}{3}) + 1 & (x > 0\land x\bmod3 = 0) \\ f(x - 1) + 1 & (x > 0\land x\bmod 3\neq 0) \end{cases} $$

Calculate maxx=lrf(x)\max_{x = l} ^ r f(x).

You need to answer TT queries independently.

输入格式

The first line contains a single integer TT (1T1041\leq T\leq 10 ^ 4).

Each of the next TT lines contains two integers ll and rr (1lr10181\leq l\leq r\leq 10 ^ {18}), representing a query.

输出格式

Output TT lines. The ii-th line contains a single integer, representing the answer to the ii-th query.

题目大意

题目描述

定义在所有非负整数 xx 上的函数 f(x)f(x) 如下:

$$f(x) = \begin{cases} 1 & (x = 0) \\ f(\frac{x}{3}) + 1 & (x > 0\land x\bmod3 = 0) \\ f(x - 1) + 1 & (x > 0\land x\bmod 3\neq 0) \end{cases} $$

计算 maxx=lrf(x)\max_{x = l} ^ r f(x)

共有 TT 组数据。

1T1041\leq T\leq 10 ^ 41lr10181\leq l\leq r\leq 10 ^ {18}

输入格式

第一行一个整数 TT

接下来 TT 行,每行两个整数 l,rl, r 表示一组询问。

输出格式

对于每组询问,输出一行一个整数表示答案。

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

3
3
4
5
3
4
5
4
5
5

提示

Source: The 2022 ICPC Asia Xi'an Regional Contest Problem E.

Author: MagicSpark.