#P9766. Find the (k+1)-th Good Number

    ID: 22912 Type: Default 1000ms 256MiB

Find the (k+1)-th Good Number

Find the (k+1)-th Good Number

A positive integer is called a good number. There are two types of good numbers defined as follows:

Type 1 Good Number: A number whose every digit is the same. For example, 1, 22, 333, etc.

Type 2 Good Number: A number that satisfies at least one of the following conditions:

  • It is a Type 1 good number.
  • Let t be the number of digits. There exists a choice of t-1 digits (all in consecutive positions after removing exactly one digit) that are identical, and the resulting t-1-digit number does not have a leading zero.

Given a positive integer x and an integer k (where k is either 0 or 1), the task is to find the smallest number y such that y ≥ x and y is a good number of type k+1 (i.e. if k is 0, then y must be a Type 1 good number; if k is 1, then y must be a Type 2 good number).

Note: For numbers with two digits (t = 2), every number qualifies as a Type 2 good number because after removing one digit, the remaining one-digit number is trivially composed of one repeated digit (and no leading zero issue arises when the remaining digit is nonzero). For numbers with t ≥ 3, the second condition is non‐trivial.

The problem may be solved by enumerating possible candidates by digit length and checking the appropriate conditions.

The necessary latex formulas:

  • For the second condition: if t is the number of digits then the condition is that there exists an index i (with 0 ≤ i < t) such that if we remove the ith digit, the remaining string is of the form $d d \cdots d$ (i.e. all digits equal) and its first digit is not 0.

inputFormat

The input consists of a single line containing two integers x and k separated by a space. Here, x (1 ≤ x) is the lower bound and k is either 0 or 1, which indicates that you must find the smallest number y ≥ x that is a Type k+1 good number.

outputFormat

Output a single integer y — the smallest good number of type k+1 such that y ≥ x.

sample

5 0
5