#15572. [ICPC 2022 Xi'an R] Find Maximum

    ID: 15572 传统题 1000ms 512MiB 尝试: 0 已通过: 0 难度: (无) 上传者: 标签>P9362数学贪心2022O2优化数位 DPICPC西安

[ICPC 2022 Xi'an R] Find Maximum

[ICPC 2022 Xi'an R] Find Maximum

题目描述

We define a function $f(x)$ over all non-negative integer $x$ 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 $\max_{x = l} ^ r f(x)$.

You need to answer TT queries independently.

输入格式

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

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

输出格式

Output $T$ lines. The $i$-th line contains a single integer, representing the answer to the $i$-th query.
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. 标签: P9362|数学|贪心|2022|O2优化|数位 DP|ICPC|西安

来源

P9362|[ICPC 2022 Xi'an R] Find Maximum