#abc313e. E - Duplicate

E - Duplicate

Score : 600600 points

问题描述

对于由数字19组成的字符串 SS,令 f(S)f(S) 为通过以下过程得到的字符串 TT。其中,SiS_i 表示 SS 的第 ii 个字符。

  • 初始化一个空字符串 TT
  • 对于 i=1,2,,S1i=1, 2, \dots, |S| - 1,执行以下操作:
    • Si+1S_{i+1} 解释为整数时的值 nn,将 SiS_inn 个副本追加到 TT 的末尾。

例如,当 S=S= 313 时,按照以下步骤可得 f(S)=f(S)= 3111

  • TT 初始为空。
  • i=1i=1 时,我们有 n=1n = 1。向 TT 追加一个 3 的副本,使其变为 3
  • i=2i=2 时,我们有 n=3n = 3。向 TT 追加三个 1 的副本,使其变为 3111
  • 结束该过程。我们得到 T=T= 3111

给定一个长度为 NN、由数字19组成的字符串 SS

您需要重复以下操作,直到字符串 SS 的长度变为 11:用 f(S)f(S) 替换 SS

求出在完成操作之前,你需要进行此操作的次数,并对 998244353998244353 取模。如果该操作会无限重复,请输出 -1

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

Problem Statement

For a string SS consisting of digits from 1 through 9, let f(S)f(S) be the string TT obtained by the following procedure. (SiS_i denotes the ii-th character of SS.)

  • Let TT be an initially empty string.
  • For i=1,2,,S1i=1, 2, \dots, |S| - 1, perform the following operation:
    • Append nn copies of SiS_i to the tail of TT, where nn is the value when Si+1S_{i+1} is interpreted as an integer.

For example, S=S = 313 yields f(S)=f(S) = 3111 by the following steps.

  • TT is initially empty.
  • For i=1i=1, we have n=1n = 1. Append one copy of 3 to TT, which becomes 3.
  • For i=2i=2, we have n=3n = 3. Append three copies of 1 to TT, which becomes 3111.
  • Terminate the procedure. We obtain T=T = 3111.

You are given a length-NN string SS consisting of digits from 1 through 9.
You repeat the following operation until the length of SS becomes 11: replace SS with f(S)f(S).
Find how many times, modulo 998244353998244353, you perform the operation until you complete it. If you will repeat the operation indefinitely, print -1 instead.

Constraints

  • 2N1062 \leq N \leq 10^6
  • SS is a length-NN string consisting of 1, 2, 3, 4, 5, 6, 7, 8, and 9.

Input

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

NN

SS

Output

Print the number of times, modulo 998244353998244353, that you perform the operation until you complete it. If you will repeat the operation indefinitely, print -1 instead.

Sample Input 1

3
313

Sample Output 1

4

If S=S = 313, the length of SS be comes 11 after four operations.

  • We have f(S)=f(S) = 3111. Replace SS with 3111.
  • We have f(S)=f(S) = 311. Replace SS with 311.
  • We have f(S)=f(S) = 31. Replace SS with 31.
  • We have f(S)=f(S) = 3. Replace SS with 3.
  • Now that the length of SS is 11, terminate the repetition.

Sample Input 2

9
123456789

Sample Output 2

-1

If S=S = 123456789, you indefinitely repeat the operation. In this case, -1 should be printed.

Sample Input 3

2
11

Sample Output 3

1

update @ 2024/3/10 08:56:04