// 100p
#include <array>
#include <iostream>
#include <cmath>
using namespace std;
const int N = 1000, P = 1e9 + 7;
int solve(int m, int j1, int i2, int j2) {
int d = i2 / (m - 1);
if ((d % 2 == 0 && i2 % (m - 1) > abs(j1 - j2)) || (d % 2 == 1 && i2 % (m - 1) > abs(m - 1 - j1 - j2)))
d++;
return d + 1;
}
int m;
struct mat {
array<array<int, N>, N> f;
void identity() {
for (int i = 0; i < m; i++) {
fill(f[i].begin(), f[i].end(), 0);
f[i][i] = 1;
}
}
void square() {
static array<array<int, N>, N> g;
for (int i = 0; i < m; i++)
fill(g[i].begin(), g[i].end(), 0);
for (int j = 0; j < m; j++)
for (int i = 0; i < m; i++)
g[0][j] = (g[0][j] + (long long) f[0][i] * f[i][j]) % P;
for (int i = 0; i < m; i++)
g[i][0] = g[0][i];
for (int i = 1; i < m; i++)
for (int j = 1; i + j < m; j++)
g[i][j] = (g[i - 1][j - 1] + g[0][i + j]) % P;
for (int i = 0; i < m; i++)
for (int j = 0; i + j < m; j++)
g[m - 1 - i][m - 1 - j] = g[i][j];
f = g;
}
void once() {
static array<int, N> g;
for (int i = 0; i < m; i++) {
g = f[i];
for (int j = 0; j < m; j++) {
f[i][j] = g[j];
if (j > 0)
f[i][j] = (f[i][j] + g[j - 1]) % P;
if (j < m - 1)
f[i][j] = (f[i][j] + g[j + 1]) % P;
}
}
}
void power(int k) {
if (k == 0)
identity();
else {
power(k / 2);
square();
if (k & 1)
once();
}
}
} dp;
int v[N + 1];
int ch(int n, int k) {
return k == 0 ? 1 : 1LL * ch(n - 1, k - 1) * n % P * v[k] % P;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
v[1] = 1;
for (int i = 2; i <= N; i++)
v[i] = 1LL * v[i - P % i] * (P / i + 1) % P;
int n, q;
cin >> n >> m >> q;
dp.power(n - 1);
while (q--) {
char t;
int j1, jn;
cin >> t >> j1 >> jn, j1--, jn--;
if (t == 'P') {
if (j1 == jn)
cout << n - 1 << ' ' << 1 << '\n';
else
cout << 0 << ' ' << 0 << '\n';
} else if (t == 'R') {
if (j1 == jn)
cout << 1 << ' ' << 1 << '\n';
else
cout << 2 << ' ' << 2 << '\n';
} else if (t == 'Q') {
if (j1 == jn || (n == m && ((j1 == 0 && jn == m - 1) || (j1 == m - 1 && jn == 0))))
cout << 1 << ' ' << 1 << '\n';
else {
int c = 4; // ll, ll, dl, dl
if (n == m && (j1 == 0 || j1 == m - 1 || jn == 0 || jn == m - 1)) // ld
c++;
if ((0 + j1) % 2 == (n - 1 + jn) % 2) { // dd
int x, y;
x = ((0 + j1) + (n - 1 - jn)) / 2, y = ((0 + j1) - (n - 1 - jn)) / 2;
c += x >= 0 && x < n && y >= 0 && y < m;
x = ((0 - j1) + (n - 1 - jn)) / 2, y = ((n - 1 - jn) - (0 - j1)) / 2;
c += x >= 0 && x < n && y >= 0 && y < m;
}
cout << 2 << ' ' << c << '\n';
}
} else if (t == 'B') {
if ((0 + j1) % 2 != (n - 1 + jn) % 2)
cout << 0 << ' ' << 0 << '\n';
else if (((j1 == 0 && jn == 0) || (j1 == m - 1 && jn == m - 1)) && ((n - 1) % ((m - 1) * 2) == 0))
cout << (n - 1) / (m - 1) << ' ' << 1 << '\n';
else if (((j1 == 0 && jn == m - 1) || (j1 == m - 1 && jn == 0)) && (((n - 1) % ((m - 1) * 2)) == m - 1))
cout << (n - 1) / (m - 1) << ' ' << 1 << '\n';
else {
int d = solve(m, j1, n - 1, jn), c = 0, x, y;
if (d % 2) {
if ((x = n - 1 + jn) <= (y = (m - 1) * (d - 1) + j1 + 0))
c = (c + ch((y - x) / 2 + d - 2, (y - x) / 2)) % P;
if ((x = n - 1 - jn) <= (y = (m - 1) * (d - 1) + m - 1 - j1 - (m - 1)))
c = (c + ch((y - x) / 2 + d - 2, (y - x) / 2)) % P;
} else {
if ((x = n - 1 + jn) <= (y = (m - 1) * (d - 1) + m - 1 - j1 + 0))
c = (c + ch((y - x) / 2 + d - 2, (y - x) / 2)) % P;
if ((x = n - 1 - jn) <= (y = (m - 1) * (d - 1) + j1 - (m - 1)))
c = (c + ch((y - x) / 2 + d - 2, (y - x) / 2)) % P;
}
cout << d << ' ' << c << '\n';
}
} else
cout << n - 1 << ' ' << dp.f[j1][jn] << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4180 KB |
Output is correct |
2 |
Correct |
120 ms |
7380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4284 KB |
Output is correct |
2 |
Correct |
6 ms |
4304 KB |
Output is correct |
3 |
Correct |
3 ms |
4180 KB |
Output is correct |
4 |
Correct |
17 ms |
5716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4180 KB |
Output is correct |
2 |
Correct |
4 ms |
4180 KB |
Output is correct |
3 |
Correct |
3 ms |
4180 KB |
Output is correct |
4 |
Correct |
3 ms |
4308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4180 KB |
Output is correct |
2 |
Correct |
4 ms |
4180 KB |
Output is correct |
3 |
Correct |
3 ms |
4180 KB |
Output is correct |
4 |
Correct |
3 ms |
4308 KB |
Output is correct |
5 |
Correct |
100 ms |
8148 KB |
Output is correct |
6 |
Correct |
51 ms |
6200 KB |
Output is correct |
7 |
Correct |
9 ms |
4308 KB |
Output is correct |
8 |
Correct |
282 ms |
8268 KB |
Output is correct |
9 |
Correct |
7 ms |
4180 KB |
Output is correct |
10 |
Correct |
10 ms |
4596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4308 KB |
Output is correct |
2 |
Correct |
4 ms |
4564 KB |
Output is correct |
3 |
Correct |
4 ms |
4564 KB |
Output is correct |
4 |
Correct |
3 ms |
4180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4308 KB |
Output is correct |
2 |
Correct |
4 ms |
4564 KB |
Output is correct |
3 |
Correct |
4 ms |
4564 KB |
Output is correct |
4 |
Correct |
3 ms |
4180 KB |
Output is correct |
5 |
Correct |
4 ms |
4308 KB |
Output is correct |
6 |
Correct |
4 ms |
4308 KB |
Output is correct |
7 |
Correct |
5 ms |
4612 KB |
Output is correct |
8 |
Correct |
5 ms |
4560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4308 KB |
Output is correct |
2 |
Correct |
4 ms |
4564 KB |
Output is correct |
3 |
Correct |
4 ms |
4564 KB |
Output is correct |
4 |
Correct |
3 ms |
4180 KB |
Output is correct |
5 |
Correct |
4 ms |
4308 KB |
Output is correct |
6 |
Correct |
4 ms |
4308 KB |
Output is correct |
7 |
Correct |
5 ms |
4612 KB |
Output is correct |
8 |
Correct |
5 ms |
4560 KB |
Output is correct |
9 |
Correct |
6 ms |
4304 KB |
Output is correct |
10 |
Correct |
7 ms |
4308 KB |
Output is correct |
11 |
Correct |
13 ms |
4564 KB |
Output is correct |
12 |
Correct |
11 ms |
4652 KB |
Output is correct |
13 |
Correct |
7 ms |
4308 KB |
Output is correct |
14 |
Correct |
6 ms |
4176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4308 KB |
Output is correct |
2 |
Correct |
4 ms |
4564 KB |
Output is correct |
3 |
Correct |
4 ms |
4564 KB |
Output is correct |
4 |
Correct |
3 ms |
4180 KB |
Output is correct |
5 |
Correct |
4 ms |
4308 KB |
Output is correct |
6 |
Correct |
4 ms |
4308 KB |
Output is correct |
7 |
Correct |
5 ms |
4612 KB |
Output is correct |
8 |
Correct |
5 ms |
4560 KB |
Output is correct |
9 |
Correct |
6 ms |
4304 KB |
Output is correct |
10 |
Correct |
7 ms |
4308 KB |
Output is correct |
11 |
Correct |
13 ms |
4564 KB |
Output is correct |
12 |
Correct |
11 ms |
4652 KB |
Output is correct |
13 |
Correct |
7 ms |
4308 KB |
Output is correct |
14 |
Correct |
6 ms |
4176 KB |
Output is correct |
15 |
Correct |
8 ms |
4308 KB |
Output is correct |
16 |
Correct |
9 ms |
4380 KB |
Output is correct |
17 |
Correct |
293 ms |
8160 KB |
Output is correct |
18 |
Correct |
300 ms |
8264 KB |
Output is correct |
19 |
Correct |
231 ms |
8012 KB |
Output is correct |
20 |
Correct |
238 ms |
8148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4180 KB |
Output is correct |
2 |
Correct |
120 ms |
7380 KB |
Output is correct |
3 |
Correct |
3 ms |
4284 KB |
Output is correct |
4 |
Correct |
6 ms |
4304 KB |
Output is correct |
5 |
Correct |
3 ms |
4180 KB |
Output is correct |
6 |
Correct |
17 ms |
5716 KB |
Output is correct |
7 |
Correct |
3 ms |
4180 KB |
Output is correct |
8 |
Correct |
4 ms |
4180 KB |
Output is correct |
9 |
Correct |
3 ms |
4180 KB |
Output is correct |
10 |
Correct |
3 ms |
4308 KB |
Output is correct |
11 |
Correct |
100 ms |
8148 KB |
Output is correct |
12 |
Correct |
51 ms |
6200 KB |
Output is correct |
13 |
Correct |
9 ms |
4308 KB |
Output is correct |
14 |
Correct |
282 ms |
8268 KB |
Output is correct |
15 |
Correct |
7 ms |
4180 KB |
Output is correct |
16 |
Correct |
10 ms |
4596 KB |
Output is correct |
17 |
Correct |
4 ms |
4308 KB |
Output is correct |
18 |
Correct |
4 ms |
4564 KB |
Output is correct |
19 |
Correct |
4 ms |
4564 KB |
Output is correct |
20 |
Correct |
3 ms |
4180 KB |
Output is correct |
21 |
Correct |
4 ms |
4308 KB |
Output is correct |
22 |
Correct |
4 ms |
4308 KB |
Output is correct |
23 |
Correct |
5 ms |
4612 KB |
Output is correct |
24 |
Correct |
5 ms |
4560 KB |
Output is correct |
25 |
Correct |
6 ms |
4304 KB |
Output is correct |
26 |
Correct |
7 ms |
4308 KB |
Output is correct |
27 |
Correct |
13 ms |
4564 KB |
Output is correct |
28 |
Correct |
11 ms |
4652 KB |
Output is correct |
29 |
Correct |
7 ms |
4308 KB |
Output is correct |
30 |
Correct |
6 ms |
4176 KB |
Output is correct |
31 |
Correct |
8 ms |
4308 KB |
Output is correct |
32 |
Correct |
9 ms |
4380 KB |
Output is correct |
33 |
Correct |
293 ms |
8160 KB |
Output is correct |
34 |
Correct |
300 ms |
8264 KB |
Output is correct |
35 |
Correct |
231 ms |
8012 KB |
Output is correct |
36 |
Correct |
238 ms |
8148 KB |
Output is correct |
37 |
Correct |
265 ms |
8156 KB |
Output is correct |
38 |
Correct |
297 ms |
8148 KB |
Output is correct |
39 |
Correct |
286 ms |
8152 KB |
Output is correct |
40 |
Correct |
3 ms |
4308 KB |
Output is correct |
41 |
Correct |
243 ms |
8144 KB |
Output is correct |
42 |
Correct |
3 ms |
4308 KB |
Output is correct |