atcoder#ABC252C. [ABC252C] Slot Strategy
[ABC252C] Slot Strategy
Score : points
Problem Statement
There is a slot machine with reels.
The placement of symbols on the -th reel is represented by a string of length containing each of 0
, 1
, , 9
exactly once.
Each reel has a corresponding button. For each non-negative integer , Takahashi can press one of the buttons of his choice (or do nothing) seconds after the reels start spinning. If the button for the -th reel is pressed seconds after the start of the spin, the -th reel will stop to display the -th character of . Here, denotes the remainder when is divided by .
Takahashi wants to stop all reels to make them display the same character. Find the minimum number of seconds needed to achieve his objective after the start of the spin.
Constraints
- is an integer.
- is a string of length containing each of
0
,1
, ,9
exactly once.
Input
Input is given from Standard Input in the following format:
Output
Print the minimum number of seconds needed to achieve Takahashi's objective after the start of the spin.
3
1937458062
8124690357
2385760149
6
Takahashi can make all reels display 8
in seconds after the start of the spin by stopping them as follows.
- seconds after the start of the spin, press the button for the -nd reel, making it stop to display the -st character of ,
8
. - seconds after the start of the spin, press the button for the -rd reel, making it stop to display the -rd character of ,
8
. - seconds after the start of the spin, press the button for the -st reel, making it stop to display the -th character of ,
8
.
There is no way to make all reels display the same character in five seconds or less, so the answer is .
5
0123456789
0123456789
0123456789
0123456789
0123456789
40
Note that he must stop all reels to make them display the same character.