QOJ.ac

QOJ

Time Limit: 1.0 s Memory Limit: 512 MB Total points: 100 Hackable ✓

#14693. 神祕如鳶尾

Statistics

對一個由正整數組成的陣列 $x$ 進行以下兩個步驟,稱為一次操作:

  1. 選擇 $x$ 中任意兩個相鄰的元素,將其中一個減 $1$,並將另一個設為 $0$。
  2. 從 $x$ 中移除所有的零。

我們稱 $x$ 是神祕的(mystic),當且僅當它可以在有限次操作(可能為零次)後被轉化為一個空序列。

給定一個由 $n$ 個整數組成的陣列 $a$,以及一個整數 $m$。$a$ 中的每個元素要麼是 $1$ 到 $m$ 之間的整數,要麼是 $-1$。你的任務是將 $a$ 中所有出現的 $-1$ 替換為 $1$ 到 $m$ 之間的任意整數。

求在替換後可以得到的相異神祕陣列 $a$ 的數量。由於答案可能非常大,請將其對 $10^9 + 7$ 取模後輸出。

輸入格式

輸入的第一行包含兩個整數 $n$ 與 $m$($2 \le n \le 10^6$,$1 \le m \le 10^8$)。

輸入的第二行包含 $n$ 個整數 $a_1, a_2, \dots, a_n$($1 \le a_i \le m$ 或 $a_i = -1$)。

輸出格式

輸出一個整數,表示可以得到的相異神祕序列 $a$ 的數量,對 $10^9 + 7$ 取模後的結果。

範例

範例輸入 1

2 2
-1 -1

範例輸出 1

3

範例輸入 2

6 10
-1 -1 -1 -1 1 7

範例輸出 2

9125

說明

在第一筆測試資料中,陣列 $a$ 為 $[-1, -1]$。透過替換兩個 $-1$,一種可能的結果是 $[1, 2]$。在此情況下,我們選擇兩個相鄰的數:將第一個數減 $1$ 並將第二個數設為 $0$,得到 $[0, 0]$。移除所有零後,序列變為空。因此,$[1, 2]$ 是一個神祕序列。

類似地,如果我們將 $-1$ 替換為 $[1, 1]$ 或 $[2, 1]$,這兩個序列也都可以被化為空。因此,相異的神祕序列總數為 $3$。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.