Inversions in Lexicographical Order
Time limit2sMemory limit512 MB
Given n with up to 250000 digits, count the inversions in the permutation formed by sorting 1 to n as strings.
- Level
Hard9 of 10
- Topics
- Combinatorics, Dynamic programming, String
- Solved
- No attempts yet
Problem
An inversion in permutation is a pair of integers such that and .
Consider a lexicographical order on positive integers. Under this ordering, integers are compared as strings of digits. For example, 628 comes before 7, 239 comes before 271, and 42 comes before 427.
You are given a positive integer . Sort all integers from 1 to , inclusive, in lexicographical order. This gives a permutation of length , where is the smallest integer between 1 and in this order (which is always 1), is the second smallest, and so on.
How many inversions does contain?
Input
The only line of the input contains a single positive integer without leading zeroes. The value of is between 1 and , inclusive, so has at most 250000 decimal digits.
Output
Output a single integer without leading zeroes: the number of inversions in .
Hint
In the first test case, contains 16 inversions.