#abc320g. G - Slot Strategy 2 (Hard)

G - Slot Strategy 2 (Hard)

Score : 600600 points

问题描述

本问题为问题 C 的更难版本,设有 NN 个转轴,并且 MM 的上限更大。

存在一台带有 NN 个转轴的老虎机。
ii 个转轴上的符号排列由字符串 SiS_i 表示。其中,SiS_i 是一个长度为 MM 的由数字组成的字符串。

每个转轴都对应一个按钮。对于每个非负整数 tt,高桥在转轴开始旋转后恰好 tt 秒时,可以选择按下任意一个按钮或选择不操作。
如果他在转轴开始旋转后恰好 tt 秒时按下第 ii 个转轴对应的按钮,那么第 ii 个转轴将会停止并显示 SiS_i 的第 ((tmodM)+1)((t \bmod M)+1) 个字符。
这里,tmodMt \bmod M 表示 tt 除以 MM 后的余数。

高桥希望让所有转轴停止转动,使得所有显示的字符都相同。
找出从转轴开始转动到所有转轴停止转动为止,实现这一目标所需的最短可能时间(以秒计)。
如果无法实现,则报告这一事实。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

This problem is a harder version of Problem C, with NN reels instead of three and a greater upper limit for MM.

There is a slot machine with NN reels.
The arrangement of symbols on the ii-th reel is represented by the string SiS_i. Here, SiS_i is a string of length MM consisting of digits.

Each reel has a corresponding button. For each non-negative integer tt, Takahashi can either choose and press one button or do nothing exactly tt seconds after the reels start spinning.
If he presses the button corresponding to the ii-th reel exactly tt seconds after the reels start spinning, the ii-th reel will stop and display the ((tmodM)+1)((t \bmod M)+1)-th character of SiS_i.
Here, tmodMt \bmod M denotes the remainder when tt is divided by MM.

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

  • 1N1001 \leq N \leq 100
  • 1M1051 \leq M \leq 10^5
  • NN and MM are integers.
  • SiS_i is a string of length MM consisting of digits.

Input

The input is given from Standard Input in the following format:

NN MM

S1S_1

\vdots

SNS_N

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 66 seconds after the reels start spinning, all the reels display 8.

  • Press the button corresponding to the second reel 00 seconds after the reels start spinning. The second reel stops and displays 8, the ((0mod10)+1=1)((0 \bmod 10)+1=1)-st character of S2S_2.
  • Press the button corresponding to the third reel 22 seconds after the reels start spinning. The third reel stops and displays 8, the ((2mod10)+1=3)((2 \bmod 10)+1=3)-rd character of S3S_3.
  • Press the button corresponding to the first reel 66 seconds after the reels start spinning. The first reel stops and displays 8, the ((6mod10)+1=7)((6 \bmod 10)+1=7)-th character of S1S_1.

There is no way to make the reels display the same character in 55 or fewer seconds, so print 66.

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