Crazy Dance
Time limit2sMemory limit1024 MB
Given base a and target digit counts b_i, find the second at which the spoken digits first match every b_i exactly, or report -1 if it never happens.
- Level
Medium6 of 10
- Topics
- Math, Binary search
- Solved
- No attempts yet
Statement
Joker is known for his madness. For that reason he uses a positional numeral system with base , in which every number consists of digits from to . Joker also loves to dance. He can dance for a very long time, so he made up a rule to keep himself from dancing forever. The rule is strange too. While dancing, every second starting from the first, he says out loud the number of seconds elapsed since the dance began, written in base without leading zeros. For , the first five numbers he says are , , , , and , spoken at seconds 1 through 5.
Joker chose an array of nonnegative integers. He decided to stop dancing as soon as, after some number he says, over the whole dance he has said digit exactly times for every . Determine how many seconds the dance lasts, or report that it goes on forever.
Input
The first line contains the base (). The second line contains integers ().
Output
If Joker never finishes his dance, print . Otherwise, print the duration of the dance in seconds.