[ICPC 2022 Xi'an R] Find Maximum

Luogu
IDLGP9362
Time1000ms
Memory512MB
DifficultyP4
数学贪心2022O2优化数位 DPICPC西安
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 $T$ queries independently. ## Input The first line contains a single integer $T$ ($1\leq T\leq 10 ^ 4$). Each of the next $T$ lines contains two integers $l$ and $r$ ($1\leq l\leq r\leq 10 ^ {18}$), representing a query. ## Output Output $T$ lines. The $i$-th line contains a single integer, representing the answer to the $i$-th query. [samples] ## Note **Source**: The 2022 ICPC Asia Xi'an Regional Contest Problem E. **Author**: MagicSpark.
Samples
Input #1
10
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5
Output #1
3
3
4
5
3
4
5
4
5
5
API Response (JSON)
{
  "problem": {
    "name": "[ICPC 2022 Xi'an R] Find Maximum",
    "description": {
      "content": "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\\n",
      "description_type": "Markdown"
    },
    "platform": "Luogu",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 524288
    },
    "difficulty": {
      "LuoguStyle": "P4"
    },
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "LGP9362"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "We define a function $f(x)$ over all non-negative integer $x$ as follows:\n$$\nf(x) =\n\\begin{cases}\n1 & (x = 0) \\\\\nf(\\frac{x}{3}) + 1 & (x > 0\\land x\\bmod3 = 0) \\\\\nf(x - 1) + 1  & (x > 0\\land x\\bmod 3\\n...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments