#P9232. [蓝桥杯 2023 省 A] 更小的数

[蓝桥杯 2023 省 A] 更小的数

Problem Description

image

Xiao Lan has a string of length nn consisting only of digit characters 090 \sim 9, with indices from 00 to n1n - 1. You can treat it as an nn-digit decimal number numnum. Xiao Lan can choose a continuous substring of numnum and reverse that substring, at most once. Xiao Lan wants the new number numnewnum_{new} obtained by reversing the chosen substring and putting it back into its original position to satisfy numnew<numnum_{new} < num. Please help him compute how many different substring choices there are in total. As long as the positions of the two substrings in numnum are not exactly the same, we consider them different choices.

Note that leading zeros are allowed, i.e., the most significant digit of the number may be 00, and this is valid.

Input Format

Input one line containing a string of length nn representing numnum (containing only digit characters 090 \sim 9). From left to right, the indices are 0n10 \sim n - 1.

Output Format

Output one line containing an integer representing the answer.

210102
8

Hint

Sample Explanation.

There are 88 different choices in total:

  1. The chosen substring indices are 010 \sim 1, after reversing numnew=120102<210102num_{new} = 120102 < 210102.
  2. The chosen substring indices are 020 \sim 2, after reversing numnew=012102<210102num_{new} = 012102 < 210102.
  3. The chosen substring indices are 030 \sim 3, after reversing numnew=101202<210102num_{new} = 101202 < 210102.
  4. The chosen substring indices are 040 \sim 4, after reversing numnew=010122<210102num_{new} = 010122 < 210102.
  5. The chosen substring indices are 050 \sim 5, after reversing numnew=201012<210102num_{new} = 201012 < 210102.
  6. The chosen substring indices are 121 \sim 2, after reversing numnew=201102<210102num_{new} = 201102 < 210102.
  7. The chosen substring indices are 141 \sim 4, after reversing numnew=201012<210102num_{new} = 201012 < 210102.
  8. The chosen substring indices are 343 \sim 4, after reversing numnew=210012<210102num_{new} = 210012 < 210102.

Constraints.

For 20%20\% of the testdata, 1n1001 \le n \le 100.

For 40%40\% of the testdata, 1n10001 \le n \le 1000.

For all testdata, 1n50001 \le n \le 5000.

Translated by ChatGPT 5