Numbers
InterviewTime limit1sMemory limit512 MB
Given x and a mood flag, find the smallest y at least x whose digits are all equal, or all equal except one digit when the flag is 1.
- Level
Medium5 of 10
- Topics
- Brute force, Implementation, Greedy
- Solved
- No attempts yet
Problem
Anya likes numbers made of identical digits. She likes 777 or 5555, but she does not like 1234 at all.
When Anya is in a good mood, she still likes all numbers made of identical digits, and she also likes numbers in which all digits except one are the same, such as 77727.
Anya has an integer . She wants to find the smallest integer that she will like.
Given an integer and whether Anya is in a good mood, write a program that finds the smallest integer that Anya likes.
Input
The first line contains an integer (). This number does not fit in a 32-bit type, so use a 64-bit type such as long long in C++ or int64 in Pascal.
The second line contains , which is 0 or 1. means Anya is in a good mood, and means she is not.
Output
Print one integer .
It must satisfy:
- if , all digits of are the same
- if , all digits of except possibly one are the same