What makes 2022 special?
InterviewTime limit1sMemory limit1024 MB
Given N and base d, find the smallest number above N whose base-d digits are each of 0 to d-1 used exactly once.
- Level
Easy3 of 10
- Topics
- Math, Brute force
- Solved
- No attempts yet
Problem
The date of the New Year contest is January 15, 2022. Junggyeom was happy that the contest was being held, and while staring at the title he noticed that 2022 is special in some way.
Yes. When is written in base , it is , and each base- digit , , , , appears exactly once. The next year with this property is 2054.
You are given a number and a base . Find the smallest number greater than such that, when written in base , each base- digit appears exactly once. Do not write leading zeros.
Input
The first line contains the number and the base , separated by a space, both written in base .
Output
On the first line, print the smallest number greater than that, when written in base , contains each base- digit exactly once. Print it in base .
If no such number exists, print on the first line.
Constraints
- All input numbers are integers written in base .
Hints
- A positive integer written in base as means , where .
- For example, .
- Base- digits are integers from up to but not including .
- The base- representation of has each digit exactly once when each appear exactly once among .