-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_description.cpp
60 lines (45 loc) · 1.03 KB
/
array_description.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <bits/stdc++.h>
using namespace std;
#define long long long int
const int MOD = 1e9 + 7;
int n, m;
vector<int> x;
vector<vector<int>> memo;
int arrays(int last, int i = 1) {
if (i >= n) return 1;
int &ans = memo[last][i];
if (~ans) return ans;
if (x[i]) {
if (abs(x[i] - last) > 1) return 0;
return ans = arrays(x[i], i + 1);
}
ans = 0;
for (int j = -1; j <= 1; j++) {
int xi = last + j;
if (xi >= 1 && xi <= m) {
ans += arrays(xi, i + 1);
ans %= MOD;
}
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
x.resize(n);
for (int i = 0; i < n; i++)
cin >> x[i];
memo.assign(m + 5, vector<int>(n + 5, -1));
if (x[0]) {
int ans = arrays(x[0]);
cout << ans << "\n";
} else {
int ans = 0;
for (int i = 1; i <= m; i++) {
ans += arrays(i);
ans %= MOD;
}
cout << ans << "\n";
}
}