#P4109. [HEOI2015] 定价

    ID: 3045 远端评测题 1000ms 125MiB 尝试: 2 已通过: 1 难度: 5 上传者: 标签>2015各省省选河北枚举,暴力概率论,统计块状链表,块状数组,分块

[HEOI2015] 定价

Description

In the market, many goods are priced like 999 yuan, 4999 yuan, 8999 yuan. They are essentially no different from 1000 yuan, 5000 yuan, and 9000 yuan, but psychologically they make people feel much cheaper, so this is a common pricing strategy used by merchants. However, in your view, such prices are absurd. Therefore, you define the absurdity of a price pp (pp is a positive integer) as follows:

  1. First, treat pp as a string of digits (without leading 00).
  2. Then, if the last character of pp is 00, remove it. Repeat this process until the last character of pp is not 00.
  3. Let the length of pp be aa. If the last digit is 55, the absurdity is 2a12a - 1; otherwise, it is 2a2a.

For example, the absurdity of 850 is 3, for 880 it is 4, and the absurdity of 9999 is 8.

Now, you want to sell an idle item. The acceptable price is within the range [L,R][L, R]. You want to choose a price with the lowest absurdity.

Input Format

The first line of input contains a positive integer TT, indicating the number of testdata.

Each test case is on a separate line and contains two space-separated positive integers L,RL, R, indicating the pricing interval.

Output Format

For each test case, output the result on a separate line. If the price with the lowest absurdity is not unique, output the smallest one.

3
998 1002
998 2002
4000 6000
1000
1000
5000

Hint

  • For 20%20\% of the testdata, L,R2000L, R \leq 2000.
  • For 100%100\% of the testdata, T100T \leq 100, 1LR1091 \leq L \leq R \leq 10^9.

Translated by ChatGPT 5