問題一覧 > 教育的問題

No.3491 Right-Associative Total Product

レベル : / AI推定難易度 : 800 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 100 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 34
作問者 : 👑 p-adic
お気に入りにしたユーザー ProblemId : 10133 / 自分の提出
問題文最終更新日: 2026-04-03 07:02:58
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 496 (順位表) の他の問題:

Note

The memory limit for this problem is 100[MB].

Problem Statement

You are given as input a non-negative integer $N$, a positive integer $M$ at most $N+1$, an integer sequence $A$ of length $2N+1$ consisting only of non-negative integers at most $N$, and a set $S$ with $M$ elements consisting only of non-negative integers at most $N$.

 

A set $T$ is initially given as $S$. Find the maximum possible number of elements of the final $T$ when $T$ is updated by repeating the following operation on $T$ any number of times, $0$ or more.

  • Choose $i \in S$ and $j \in T$, and replace $T$ with $T \cup \{A_{i+j}\}$.

Here, for each non-negative integer $k$ at most $2N$, the $1+k$-th element of $A$ is denoted by $A_k$.

Background

We explain the background for those familiar with algebra. Let $I_N$ be the set of all non-negative integers at most $N$, and regard $A$ as a $2$-ary operation on $I_N$ by defining $i A j = A_{i+j}$ for each pair of non-negative integers $i,j$ at most $N$. For each positive integer $\ell$ and each integer sequence $(i_k)_{k=1}^{\ell}$ of length $\ell$ consisting only of non-negative integers at most $N$, define its right-associative total product $A^{\prod} (i_k)_{k=1}^{\ell}$ with respect to $A$ by the following recursive formula:

$\displaystyle A^{\prod} (i_k)_{k=1}^{\ell} = \left\{ \begin{array}{ll} i_1 & (\ell = 1) \\ i_1 A (A^{\prod} (i_{k+1})_{k=1}^{\ell-1}) & (\ell > 1) \end{array} \right. $

In short, it is association from the right by $A$. For example, when $\ell = 3$, we have $A^{\prod} (i_k)_{k=1}^{\ell} = i_1 A (i_2 A i_3)$. Note that since $A$ is not necessarily unital, the definition should not be extended to the case $\ell = 0$ as is done for total products of a monoid operation, and that since $A$ is commutative but not necessarily associative, changing the order of association of $A$ may change the value of the total product.

Let $S'$ be the set of all right-associative total products of elements of $S$ with respect to $A$. Then $S'$ can be obtained by performing the operation on $T$ some number of times, and every final $T$ obtained by performing the operation on $T$ $0$ or more times is a subset of $S'$. Therefore, this problem is equivalent to finding the number of elements of $S'$.

Input

Number the elements of $S$ in ascending order by the non-negative integers $m$ less than $M$, and denote them by $s_m$.

Then, the input is given from standard input in $3$ lines in the following format:

  • Line $1$ contains $N, M$, separated by a single-byte space.
  • Line $2$ contains $A_k$ for each non-negative integer $k$ at most $2N$, in ascending order of $k$, separated by single-byte spaces.
  • Line $3$ contains $s_m$ for each non-negative integer $m$ less than $M$, in ascending order of $m$, separated by single-byte spaces.
$N$ $M$
$A_0$ $\cdots$ $A_{2N}$
$s_0$ $\cdots$ $s_{M-1}$

Constraints

The input satisfies the following constraints:

  • $N$ is an integer satisfying $0 \leq N \leq 4 \times 10^3$.
  • $M$ is an integer satisfying $1 \leq M \leq N+1$.
  • For any non-negative integer $k$ at most $2N$, $A_k$ is an integer satisfying $0 \leq A_k \leq N$.
  • For any non-negative integer $m$ less than $M$, $s_m$ is an integer satisfying $0 \leq s_m \leq N$.
  • For any non-negative integers $m_0, m_1$ less than $M$, if $m_0 < m_1$ then $s_{m_0} < s_{m_1}$.

Output

Output the maximum possible number of elements of the final $T$ in $1$ line.

Print a newline at the end.

Sample

Sample 1
Input
0 1
0
0
Output
1

We have $A = (A_0) = (0)$ and $S = \{0\}$. Initially, $T = S = \{0\}$ is given.

Perform the operation on $T$ for the $1$-st time. The only possible choice of $i \in S$ and $j \in T$ is $(i,j) = (0,0)$. As a result of choosing this, $T$ is replaced with $T \cup \{A_{i+j}\} = \{0\} \cup \{0\} = \{0\}$. That is, $T$ does not change. Therefore, $T$ after $0$ or more operations is always $\{0\}$, and its number of elements is $1$.

For those familiar with algebra, $(I_N,A)$ is the trivial group.

Sample 2
Input
1 1
0 1 0
1
Output
2

We have $A = (A_0,A_1,A_2) = (0,1,0)$ and $S = \{1\}$. Initially, $T = S = \{1\}$ is given.

Perform the operation on $T$ for the $1$-st time. The only possible choice of $i \in S$ and $j \in T$ is $(i,j) = (1,1)$. As a result of choosing this, $T$ is replaced with $T \cup \{A_{i+j}\} = \{1\} \cup \{0\} = \{0,1\}$. This is equal to the set of all non-negative integers at most $N = 1$.

Since each operation can only replace $T$ with a set that contains $T$ and consists only of non-negative integers at most $N$, $T$ after $1$ or more operations is always $\{0,1\}$, and its number of elements is $2$.

For those familiar with algebra, $(I_N,A)$ is isomorphic to the additive group modulo $2$.

Sample 3
Input
2 2
0 1 2 2 2
1 2
Output
2

We have $A = (A_0,A_1,A_2,A_3,A_4) = (0,1,2,2,2)$ and $S = \{1,2\}$. Initially, $T = S = \{1,2\}$ is given.

Perform the operation on $T$ for the $1$-st time. There are $4$ possible choices of $i \in S$ and $j \in T$: $(i,j) = (1,1), (1,2), (2,1), (2,2)$. Whichever of these is chosen, $T$ is replaced with $T \cup \{A_{i+j}\} = \{1,2\} \cup \{2\} = \{1,2\}$. That is, $T$ does not change. Therefore, $T$ after $1$ or more operations is always $\{1,2\}$, and its number of elements is $2$.

For those familiar with algebra, $(I_N,A)$ is the commutative monoid given by addition truncated at the upper bound $2$.

Sample 4
Input
3 1
0 0 0 3 0 1 2
3
Output
4

We have $A = (A_0,A_1,A_2,A_3,A_4,A_5,A_6) = (0,0,0,3,0,1,2)$ and $S = \{3\}$. Initially, $T = S = \{3\}$ is given.

Perform the operation on $T$ for the $1$-st time. The only possible choice of $i \in S$ and $j \in T$ is $(i,j) = (3,3)$. As a result of choosing this, $T$ is replaced with $T \cup \{A_{3+3}\} = \{3\} \cup \{2\} = \{2,3\}$.

Perform the operation on $T$ for the $2$-nd time. There are $2$ possible choices of $i \in S$ and $j \in T$: $(i,j) = (3,2), (3,3)$. For example, suppose we choose $(i,j) = (3,2)$. Then $T$ is replaced with $T \cup \{A_{3+2}\} = \{2,3\} \cup \{1\} = \{1,2,3\}$.

Perform the operation on $T$ for the $3$-rd time. There are $3$ possible choices of $i \in S$ and $j \in T$: $(i,j) = (3,1), (3,2), (3,3)$. For example, suppose we choose $(i,j) = (3,1)$. Then $T$ is replaced with $T \cup \{A_{3+1}\} = \{1,2,3\} \cup \{0\} = \{0,1,2,3\}$. This is equal to the set of all non-negative integers at most $N = 3$.

Since each operation can only replace $T$ with a set that contains $T$ and consists only of non-negative integers at most $N$, $T$ from this point on is always $\{0,1,2,3\}$, and its number of elements is $4$.

For those familiar with algebra, $(I_N,A)$ is a non-unital non-associative commutative magma.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。