Problems > Counting a Digit

Counting a Digit

Difficulty: 1600 2000 ms 128 MB

math

How many times does the digit D appear when all integers 1..N are written out?

Input: one line with two integers N and D.

Output: the number of occurrences of digit D in 1..N.

Constraints:

  • 1 ≤ N ≤ 1018
  • 0 ≤ D ≤ 9

Sample Tests

Input 1
13 1
Output 1
6
Input 2
25 2
Output 2
9

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.