#include <bits/stdc++.h>
using i64 = long long;
constexpr i64 mod = 1000000007;
i64 add(i64 a, i64 b) {
return (a + b) % mod;
}
void tadd(i64 &a, i64 b) {
a = add(a, b);
}
i64 sub(i64 a, i64 b) {
return ((a - b) % mod + mod) % mod;
}
i64 mul(i64 a, i64 b) {
return (a * b) % mod;
}
constexpr i64 maxN = 5000;
std::array<i64, maxN> fact, inv, inv_fact, pow_2;
void init() {
fact[0] = fact[1] = inv[1] = inv_fact[0] = inv_fact[1] = 1;
pow_2[0] = 1, pow_2[1] = 2;
for (int i = 2; i < maxN; ++i) {
fact[i] = mul(fact[i - 1], i);
inv[i] = mod - mul(inv[mod % i], mod / i);
inv_fact[i] = mul(inv_fact[i - 1], inv[i]);
pow_2[i] = mul(pow_2[i - 1], 2);
}
}
i64 combination(i64 n, i64 k) {
if (n < 0 or k < 0 or n < k) {
return 0;
}
return mul(fact[n], mul(inv_fact[n - k], inv_fact[k]));
}
void minus_1(std::string &x) {
const int n = (int)x.size();
for (int i = n - 1; i >= 0; --i) {
if (x[i] == '0') {
continue;
}
x[i] = '0';
for (int j = i + 1; j < n; ++j) {
x[j] = '1';
}
break;
}
}
int main() {
init();
int N, K;
std::cin >> N >> K;
std::string S;
std::cin >> S;
std::string A, B;
std::cin >> A >> B;
auto solve = [&](const std::string C) -> i64 {
// solve for [0, C]
// pre- calc
std::vector dp(N, std::vector(K + 1, std::array<i64, 2>{{0, 0}}));
dp[N - 1][0][S[N - 2] == 'R' ? 0 : 1] = 1;
for (int i = N - 1; i >= 2; --i) {
int c = S[i - 2] == 'R' ? 0 : 1;
for (int j = 0; j <= std::min(K, N - i - 1); ++j) {
for (int s = 0; s < 2; ++s) {
for (int nx = 0; nx < 2; ++nx) {
if (j + nx > K) {
continue;
}
// from dp[i][j][s] to dp[i - 1][j + nx][s]
// nm
const i64 ym = combination(N - i - 1, j);
// default
tadd(dp[i - 1][j + nx][s], dp[i][j][s]);
// add
if ((j + nx + c) % 2 == (s % 2)) {
tadd(dp[i - 1][j + nx][s], mul(pow_2[N - i], ym));
}
}
}
}
}
auto dfs = [&](auto &&self, const int t, int count_change) -> i64 {
if (count_change > K or (N - t - 1) < (K - count_change)) {
return 0;
}
if (t == N - 1) {
i64 ret = 0;
for (int i = 0; i < N; ++i) {
if (C[i] == '1') {
tadd(ret, pow_2[N - i - 1]);
}
}
return ret;
}
if (C[t + 1] == '0') {
// now direction
bool now_dir = (count_change % 2) xor (S[t] == 'L');
const int cost = now_dir ? 0 : 1;
return self(self, t + 1, count_change + cost);
} else {
bool now_dir = (count_change % 2) xor (S[t] == 'L');
const int cost = !now_dir ? 0 : 1;
count_change += cost;
const i64 res_right = self(self, t + 1, count_change);
count_change -= cost;
count_change += (1 - cost);
// calculate the left-side cost
const int k = K - count_change, r = N - t - 2;
if (k < 0 or k > r) {
// skip
return res_right;
}
// part of ancestors
i64 sum_acs = 0;
for (int i = 0; i <= t; ++i) {
if (C[i] == '1') {
tadd(sum_acs, pow_2[N - i - 1]);
}
}
i64 res_left = mul(sum_acs, combination(r, k));
// part of children
if (t != N - 2) {
for (int s = 0; s < 2; ++s) {
if (k - s < 0) {
continue;
}
if (k == r and s == 0) {
continue;
}
tadd(res_left, dp[t + 2][k - s][K % 2]);
}
}
return res_left + res_right;
}
};
return dfs(dfs, 0, 0);
};
auto calc_ans_ot = [&]() {
i64 answer = solve(B);
if (std::any_of(A.begin() + 1, A.end(), [&](char x) { return x == '1'; })) {
auto ad = A;
minus_1(ad);
answer = sub(answer, solve(ad));
}
return answer;
};
i64 answer = calc_ans_ot();
for (auto &e : S) {
e = e == 'L' ? 'R' : 'L';
}
tadd(answer, calc_ans_ot());
answer = (answer % mod + mod) % mod;
std::cout << answer << std::endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
456 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
464 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
9564 KB |
Output is correct |
2 |
Correct |
55 ms |
6728 KB |
Output is correct |
3 |
Correct |
67 ms |
7652 KB |
Output is correct |
4 |
Correct |
94 ms |
15256 KB |
Output is correct |
5 |
Correct |
54 ms |
6412 KB |
Output is correct |
6 |
Correct |
93 ms |
15872 KB |
Output is correct |
7 |
Correct |
34 ms |
4208 KB |
Output is correct |
8 |
Correct |
63 ms |
7548 KB |
Output is correct |
9 |
Correct |
4 ms |
844 KB |
Output is correct |
10 |
Correct |
59 ms |
6892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
456 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
456 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
464 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
74 ms |
9564 KB |
Output is correct |
25 |
Correct |
55 ms |
6728 KB |
Output is correct |
26 |
Correct |
67 ms |
7652 KB |
Output is correct |
27 |
Correct |
94 ms |
15256 KB |
Output is correct |
28 |
Correct |
54 ms |
6412 KB |
Output is correct |
29 |
Correct |
93 ms |
15872 KB |
Output is correct |
30 |
Correct |
34 ms |
4208 KB |
Output is correct |
31 |
Correct |
63 ms |
7548 KB |
Output is correct |
32 |
Correct |
4 ms |
844 KB |
Output is correct |
33 |
Correct |
59 ms |
6892 KB |
Output is correct |
34 |
Correct |
108 ms |
11644 KB |
Output is correct |
35 |
Correct |
41 ms |
4360 KB |
Output is correct |
36 |
Correct |
55 ms |
6828 KB |
Output is correct |
37 |
Correct |
86 ms |
14692 KB |
Output is correct |
38 |
Correct |
19 ms |
2260 KB |
Output is correct |
39 |
Correct |
91 ms |
12912 KB |
Output is correct |
40 |
Correct |
22 ms |
2016 KB |
Output is correct |
41 |
Correct |
76 ms |
8644 KB |
Output is correct |
42 |
Correct |
91 ms |
10932 KB |
Output is correct |
43 |
Correct |
74 ms |
10968 KB |
Output is correct |
44 |
Correct |
79 ms |
12832 KB |
Output is correct |
45 |
Correct |
33 ms |
3964 KB |
Output is correct |
46 |
Correct |
73 ms |
10000 KB |
Output is correct |
47 |
Correct |
85 ms |
10676 KB |
Output is correct |
48 |
Correct |
51 ms |
6304 KB |
Output is correct |
49 |
Correct |
6 ms |
864 KB |
Output is correct |
50 |
Correct |
88 ms |
12256 KB |
Output is correct |
51 |
Correct |
56 ms |
5792 KB |
Output is correct |
52 |
Correct |
53 ms |
6108 KB |
Output is correct |
53 |
Correct |
95 ms |
14840 KB |
Output is correct |
54 |
Correct |
38 ms |
4268 KB |
Output is correct |
55 |
Correct |
81 ms |
11860 KB |
Output is correct |
56 |
Correct |
90 ms |
13520 KB |
Output is correct |
57 |
Correct |
15 ms |
1828 KB |
Output is correct |
58 |
Correct |
78 ms |
10604 KB |
Output is correct |