-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC.cpp
More file actions
155 lines (149 loc) · 3.25 KB
/
Copy pathC.cpp
File metadata and controls
155 lines (149 loc) · 3.25 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <bits/stdc++.h>
using namespace std;
template <typename T> inline int len(const T &a) { return a.size(); }
constexpr int mod = 1000 * 1000 * 1000 + 7;
int add(int a, int b) {
a += b;
if (a >= mod) {
a -= mod;
}
if (a < 0) {
a += mod;
}
return a;
}
constexpr int maxn = 2e3 + 10;
char a[maxn][maxn];
int dp1[maxn][maxn], dp2[maxn][maxn];
int dp1_sum[maxn][maxn], dp2_sum[maxn][maxn];
int col_stone[maxn][maxn], row_stone[maxn][maxn];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> (a[i] + 1);
}
for (int i = 1; i <= n; ++i) {
for (int j = m; j > 0; --j) {
row_stone[i][j] = (j < m ? row_stone[i][j + 1] : 0) + (a[i][j] == 'R');
}
}
for (int j = 1; j <= m; ++j) {
for (int i = n; i > 0; --i) {
col_stone[j][i] = (i < n ? col_stone[j][i + 1] : 0) + (a[i][j] == 'R');
}
}
dp1[1][1] = 1;
dp2[1][1] = 1;
dp1_sum[1][1] = 1;
dp2_sum[1][1] = 1;
#ifdef LOCAL
vector<vector<int>> dp1_low(n + 1, vector<int>(m + 1));
vector<vector<int>> dp2_low(n + 1, vector<int>(m + 1));
#endif
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (i == 1 && j == 1) {
continue;
}
{
auto check = [&](int mid) {
return row_stone[i][mid + 1] <= m - j;
};
int low = 1, high = j - 1;
while (high - low > 0) {
int mid = (low + high) / 2;
if (!check(mid)) {
low = mid + 1;
} else {
high = mid;
}
}
if (j == 1 || !check(low)) {
dp2[i][j] = 0;
} else {
dp2[i][j] = add(dp1_sum[i][j - 1], -dp1_sum[i][low - 1]);
}
#ifdef LOCAL
dp2_low[i][j] = low;
#endif
dp2_sum[i][j] = add(dp2_sum[i - 1][j], dp2[i][j]);
}
{
auto check = [&](int mid) {
return col_stone[j][mid + 1] <= n - i;
};
int low = 1, high = i - 1;
while (high - low > 0) {
int mid = (low + high) / 2;
if (!check(mid)) {
low = mid + 1;
} else {
high = mid;
}
}
if (i == 1 || !check(low)) {
dp1[i][j] = 0;
} else {
dp1[i][j] = add(dp2_sum[i - 1][j], -dp2_sum[low - 1][j]);
}
#ifdef LOCAL
dp1_low[i][j] = low;
#endif
dp1_sum[i][j] = add(dp1_sum[i][j - 1], dp1[i][j]);
}
}
}
#ifdef LOCAL
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cerr << dp1[i][j] << ' ';
}
cerr << '\n';
}
cerr << '\n';
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cerr << dp2[i][j] << ' ';
}
cerr << '\n';
}
cerr << '\n';
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cerr << dp1_low[i][j] << ' ';
}
cerr << '\n';
}
cerr << '\n';
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cerr << dp2_low[i][j] << ' ';
}
cerr << '\n';
}
cerr << '\n';
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cerr << dp1_sum[i][j] << ' ';
}
cerr << '\n';
}
cerr << '\n';
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cerr << dp2_sum[i][j] << ' ';
}
cerr << '\n';
}
cerr << '\n';
#endif
if (n == 1 && m == 1) {
cout << 1;
} else {
cout << add(dp1[n][m], dp2[n][m]);
}
return 0;
}