All problems

Problem 867

Numbers With Repeated Digits

Hard · 30 pts · Math, Dynamic Programming, Combinatorics

Problem

Count the positive integers from 1 to N that have at least one repeated digit (like 11, 121 or 1001). It is easier to count the numbers with all digits distinct and subtract from N. Count them digit by digit: all distinct-digit numbers shorter than N, plus those with N's length that share a prefix with N and then have a smaller digit, using permutation counts for the remaining positions.

Input

A single integer N.

Output

Print how many integers in [1, N] have a repeated digit.

Constraints

1 <= N <= 10^9

Example input

100

Example output

10

Explanation

Up to 100, the numbers with a repeated digit are 11, 22, 33, 44, 55, 66, 77, 88, 99 and 100. So the answer is 10.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Preimage Size of Factorial Zeroes Function Next: Nim Game