#QHD0000017. Mô tả mảng (Array Description)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Mô tả mảng (Array Description)
Array Description
Source: CSES
Version: Phuoc Hung OJ Extended
Problem Statement
Complete an array of elements, each an integer between and . Adjacent elements must differ by at most one: for .
Some positions already have fixed values. Unknown positions are represented by and may be assigned any integer in . Fixed positions cannot be changed.
Count all distinct completed arrays satisfying these conditions, modulo .
Input
The first line contains and , the array length and maximum allowed element.
The second line contains integers. A zero represents an unknown position, and a nonzero value is fixed.
Output
Print the number of valid arrays modulo .
Subtasks
- Subtask 1 (20 points): ; ; .
- Subtask 2 (30 points): ; ; .
- Subtask 3 (50 points): ; ; .
Examples
Input
3 5
2 0 2
Output
3
Explanation
The first and third entries are fixed at . The middle entry must differ by at most from both, so it can only be , , or . The three valid arrays are , , and .