electicode
АсосӣКурсҳоМанбаъҳоМасъалаҳоОлимпиадаи миллӣМусобиқаҳоҶадвали роҳбарон
...

Almost Palindrome

Маҳдудияти вақт: 1000msМаҳдудияти ҳофиза: 1024MB
Ҳамаи ҳалҳо

Тавсифи масъала

A palindrome is a number whose digits read the same from left to right and from right to left. Examples: 777, 121121121, 400440044004.

Palindrome checking is done as follows. Let the number have length kkk. Compare the first ⌊k/2⌋\lfloor k/2 \rfloor⌊k/2⌋ digits with the last ⌊k/2⌋\lfloor k/2 \rfloor⌊k/2⌋ digits in pairs:

  • 1st digit ↔\leftrightarrow↔ last digit,
  • 2nd digit ↔\leftrightarrow↔ second last digit,
  • 3rd digit ↔\leftrightarrow↔ third last digit,
  • and so on.

Each such comparison is called a pair. If all pairs are equal, the number is a palindrome.

Almost palindrome

A number is called almost palindrome if, among all these pairs:

  • exactly one pair has different digits (exactly 1 mismatching pair),
  • all other pairs match.

So, palindromes themselves (with 000 mismatching pairs) are not almost palindromes.

Given an integer NNN, count how many almost palindrome numbers are in the range from 111 to NNN.

Input Format

One integer NNN (1≤N≤10181 \le N \le 10^{18}1≤N≤1018).

Output Format

Print one integer --- the number of almost palindrome integers in [1, N][1,\, N][1,N].

Scoring

SubtaskConstraintsPoints
1N≤2⋅105N \le 2 \cdot 10^{5}N≤2⋅10517
2N≤1010N \le 10^{10}N

Мисолҳо

Мисол 1
Вуруд
15
Баромад
5
Шарҳ

In [1,15][1, 15][1,15], the almost palindromes are: 101010, 121212, 131313, 141414, 15. There are such numbers.

© 2026 Electicode. All rights reserved.

≤
1010
28
3No additional constraints55
15
15
555
Мисол 2
Вуруд
87929634991234692
Баромад
69409337210
Шарҳ

For this NNN, the number of almost palindromes in [1, N][1,\, N][1,N] is 694093372106940933721069409337210.