#abc320g. G - Slot Strategy 2 (Hard)
G - Slot Strategy 2 (Hard)
Score : points
问题描述
本问题为问题 C 的更难版本,设有 个转轴,并且 的上限更大。
存在一台带有 个转轴的老虎机。
第 个转轴上的符号排列由字符串 表示。其中, 是一个长度为 的由数字组成的字符串。
每个转轴都对应一个按钮。对于每个非负整数 ,高桥在转轴开始旋转后恰好 秒时,可以选择按下任意一个按钮或选择不操作。
如果他在转轴开始旋转后恰好 秒时按下第 个转轴对应的按钮,那么第 个转轴将会停止并显示 的第 个字符。
这里, 表示 除以 后的余数。
高桥希望让所有转轴停止转动,使得所有显示的字符都相同。
找出从转轴开始转动到所有转轴停止转动为止,实现这一目标所需的最短可能时间(以秒计)。
如果无法实现,则报告这一事实。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
This problem is a harder version of Problem C, with reels instead of three and a greater upper limit for .
There is a slot machine with reels.
The arrangement of symbols on the -th reel is represented by the string . Here, is a string of length consisting of digits.
Each reel has a corresponding button. For each non-negative integer , Takahashi can either choose and press one button or do nothing exactly seconds after the reels start spinning.
If he presses the button corresponding to the -th reel exactly seconds after the reels start spinning, the -th reel will stop and display the -th character of .
Here, denotes the remainder when is divided by .
Takahashi wants to stop all the reels so that all the displayed characters are the same.
Find the minimum possible number of seconds from the start of the spin until all the reels are stopped so that his goal is achieved.
If this is impossible, report that fact.
Constraints
- and are integers.
- is a string of length consisting of digits.
Input
The input is given from Standard Input in the following format:
Output
If it is impossible to stop all the reels so that all the displayed characters are the same, print -1
.
Otherwise, print the minimum possible number of seconds from the start of the spin until such a state is achieved.
Sample Input 1
3 10
1937458062
8124690357
2385760149
Sample Output 1
6
Takahashi can stop each reel as follows so that seconds after the reels start spinning, all the reels display 8
.
- Press the button corresponding to the second reel seconds after the reels start spinning. The second reel stops and displays
8
, the -st character of . - Press the button corresponding to the third reel seconds after the reels start spinning. The third reel stops and displays
8
, the -rd character of . - Press the button corresponding to the first reel seconds after the reels start spinning. The first reel stops and displays
8
, the -th character of .
There is no way to make the reels display the same character in or fewer seconds, so print .
Sample Input 2
10 20
01234567890123456789
01234567890123456789
01234567890123456789
01234567890123456789
01234567890123456789
01234567890123456789
01234567890123456789
01234567890123456789
01234567890123456789
01234567890123456789
Sample Output 2
90
Note that he must stop all the reels and make them display the same character.
Sample Input 3
5 10
0000000000
1111111111
2222222222
3333333333
4444444444
Sample Output 3
-1
It is impossible to stop the reels so that all the displayed characters are the same.
In this case, print -1
.
Sample Input 4
10 20
14159265358979323846
26433832795028841971
69399375105820974944
59230781640628620899
86280348253421170679
82148086513282306647
09384460955058223172
53594081284811174502
84102701938521105559
64462294895493038196
Sample Output 4
11
update @ 2024/3/10 01:39:32