#P8431. 「WHOI-2」彗星蜜月

    ID: 9446 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>模拟贪心二分洛谷原创O2优化枚举

「WHOI-2」彗星蜜月

Background

After watching the intro of this MV, you should know what the heck ff is (just kidding).

Problem Description

Define f(x)f(x) as the number formed by reversing the digits of xx.

For example:

  • f(12323)=32321f(12323)=32321.
  • f(114514)=415411f(114514)=415411.
  • f(250)=52f(250)=52.

Given an integer nn, find the largest kk such that for every positive integer mm in the interval [1,k][1,k], we have f(m)nf(m)\leq n.

Input Format

This problem has multiple test cases.

The first line contains a positive integer TT, the number of test cases.

Each of the next TT lines contains a positive integer nn.

Output Format

Output TT lines, one for each test case, containing the answer.

3
12
991
114514
11
298
100001
2
99999
99998
100000
99998

Hint

For sample test 11:

$f(1)=1,f(2)=2,f(3)=3,f(4)=4,f(5)=5,f(6)=6,f(7)=7,f(8)=8,f(9)=9,f(10)=1,f(11)=11,f(12)=21$. Therefore, the maximum kk is 1111.


This problem uses bundled testdata.

  • subtask1(10pts):1T,n103\text{subtask1(10pts)}:1\leq T,n\leq10^3.
  • subtask2(30pts):1n106\text{subtask2(30pts)}:1\leq n\leq10^6.
  • subtask3(40pts):1n109\text{subtask3(40pts)}:1\leq n\leq10^9.
  • subtask4(20pts):\text{subtask4(20pts)}: No special constraints.

For 100%100\% of the data, 1T105,1n10181\leq T\leq10^5,1\leq n\leq10^{18}.

Hint: unsigned long long can store natural numbers from 00 to 18,446,744,073,709,551,615(2641)18,446,744,073,709,551,615(2^{64}-1).

Translated by ChatGPT 5