// Created by Nikolay Budin
#ifdef DEBUG
# define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define ff first
#define ss second
#define szof(x) ((int)x.size())
#ifndef LOCAL
# define cerr __get_ce
#endif
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef unsigned long long ull;
using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename K, typename V> using ordered_map = tree<K, V, less<K>, rb_tree_tag, tree_order_statistics_node_update>;
int const INF = (int)1e9 + 1e3;
ll const INFL = (ll)1e18 + 1e6;
#ifdef LOCAL
mt19937 tw(9450189);
#else
mt19937 tw(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif
uniform_int_distribution<ll> ll_distr;
ll rnd(ll a, ll b) { return ll_distr(tw) % (b - a + 1) + a; }
const int MOD = 1'000'000'007;
void add(int& a, int b) {
a += b;
if (a >= MOD) {
a -= MOD;
}
}
ll encode(int len, int deg1, int mask_have, int connected) {
ll ret = len;
ret <<= 11;
ret |= deg1 + 1000;
ret <<= 2;
ret |= mask_have;
ret <<= 1;
ret |= connected;
return ret;
}
void decode(ll val, int& len, int& deg1, int& mask_have, int& connected) {
connected = val & 1;
val >>= 1;
mask_have = val & 3;
val >>= 2;
deg1 = (val & 2047) - 1000;
val >>= 11;
len = val;
}
void solve() {
int n, l;
cin >> n >> l;
// len, deg_in[1] - deg_out[1], mask_have, connected
vector<pair<ll, int>> vars, next;
vars.push_back({encode(0, 0, 0, 0), 1});
int len, deg1, mask_have, connected;
int ans = 0;
vector<pii> beg, end;
for (int it = 0; it < n; ++it) {
int a;
string b, c;
cin >> a >> b >> c;
int vl, vr;
if (b == "none") {
vl = 2;
} else if (b == "in") {
vl = 0;
} else {
vl = 1;
}
if (c == "none") {
vr = 3;
} else if (c == "in") {
vr = 1;
} else {
vr = 0;
}
if (vl == 2 && vr == 3) {
if (a == l) {
add(ans, 1);
}
continue;
} else if (vl == 2) {
beg.push_back({a, vr});
continue;
} else if (vr == 3) {
end.push_back({a, vl});
continue;
}
next.clear();
for (auto p : vars) {
decode(p.ff, len, deg1, mask_have, connected);
next.push_back(p);
if (len + a > l) {
continue;
}
len += a;
mask_have |= 1 << vl;
mask_have |= 1 << vr;
if (vl == 0) {
--deg1;
}
if (vr == 0) {
++deg1;
}
connected |= vl != vr;
next.push_back({encode(len, deg1, mask_have, connected), p.ss});
}
sort(next.begin(), next.end());
vars.clear();
for (auto p : next) {
if (!szof(vars) || vars.back().ff != p.ff) {
vars.push_back(p);
} else {
add(vars.back().ss, p.ss);
}
}
}
vector res(2, vector(2, vector(l + 1, 0)));
for (auto p : vars) {
decode(p.ff, len, deg1, mask_have, connected);
if (mask_have == 3 && !connected) {
continue;
}
if (mask_have == 1) {
if (deg1 == 0) {
add(res[0][0][len], p.ss);
}
} else if (mask_have == 2) {
if (deg1 == 0) {
add(res[1][1][len], p.ss);
}
} else {
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
int tmp = deg1;
if (i == 0) {
++tmp;
}
if (j == 0) {
--tmp;
}
if (tmp == 0) {
add(res[i][j][len], p.ss);
}
}
}
}
}
for (auto [l1, vl] : beg) {
for (auto [l2, vr] : end) {
if (l1 + l2 == l && vl == vr) {
add(ans, 1);
}
if (l1 + l2 < l) {
add(ans, res[vl][vr][l - l1 - l2]);
}
}
}
cout << ans << "\n";
}
int main() {
#ifdef LOCAL
auto start_time = clock();
cerr << setprecision(3) << fixed;
#endif
cout << setprecision(15) << fixed;
ios::sync_with_stdio(false);
cin.tie(nullptr);
int test_count = 1;
// cin >> test_count;
for (int test = 1; test <= test_count; ++test) {
solve();
}
#ifdef LOCAL
auto end_time = clock();
cerr << "Execution time: " << (end_time - start_time) * (int)1e3 / CLOCKS_PER_SEC << " ms\n";
#endif
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
3 ms |
332 KB |
Output is correct |
4 |
Correct |
3 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
6 ms |
332 KB |
Output is correct |
10 |
Correct |
6 ms |
332 KB |
Output is correct |
11 |
Correct |
6 ms |
332 KB |
Output is correct |
12 |
Correct |
7 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
352 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
332 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
4 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
352 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
332 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
4 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
2 ms |
396 KB |
Output is correct |
17 |
Correct |
12 ms |
584 KB |
Output is correct |
18 |
Correct |
2 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
332 KB |
Output is correct |
22 |
Correct |
19 ms |
588 KB |
Output is correct |
23 |
Correct |
17 ms |
612 KB |
Output is correct |
24 |
Correct |
19 ms |
596 KB |
Output is correct |
25 |
Correct |
15 ms |
588 KB |
Output is correct |
26 |
Correct |
11 ms |
456 KB |
Output is correct |
27 |
Correct |
9 ms |
460 KB |
Output is correct |
28 |
Correct |
4 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
33 ms |
848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
3 ms |
332 KB |
Output is correct |
16 |
Correct |
3 ms |
332 KB |
Output is correct |
17 |
Correct |
2 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
6 ms |
332 KB |
Output is correct |
22 |
Correct |
6 ms |
332 KB |
Output is correct |
23 |
Correct |
6 ms |
332 KB |
Output is correct |
24 |
Correct |
7 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
2 ms |
352 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
2 ms |
332 KB |
Output is correct |
34 |
Correct |
2 ms |
332 KB |
Output is correct |
35 |
Correct |
2 ms |
332 KB |
Output is correct |
36 |
Correct |
2 ms |
332 KB |
Output is correct |
37 |
Correct |
2 ms |
332 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
4 ms |
460 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
2 ms |
396 KB |
Output is correct |
42 |
Correct |
12 ms |
584 KB |
Output is correct |
43 |
Correct |
2 ms |
332 KB |
Output is correct |
44 |
Correct |
1 ms |
204 KB |
Output is correct |
45 |
Correct |
1 ms |
332 KB |
Output is correct |
46 |
Correct |
2 ms |
332 KB |
Output is correct |
47 |
Correct |
19 ms |
588 KB |
Output is correct |
48 |
Correct |
17 ms |
612 KB |
Output is correct |
49 |
Correct |
19 ms |
596 KB |
Output is correct |
50 |
Correct |
15 ms |
588 KB |
Output is correct |
51 |
Correct |
11 ms |
456 KB |
Output is correct |
52 |
Correct |
9 ms |
460 KB |
Output is correct |
53 |
Correct |
4 ms |
332 KB |
Output is correct |
54 |
Correct |
1 ms |
332 KB |
Output is correct |
55 |
Correct |
33 ms |
848 KB |
Output is correct |
56 |
Correct |
1 ms |
204 KB |
Output is correct |
57 |
Correct |
1 ms |
204 KB |
Output is correct |
58 |
Correct |
23 ms |
600 KB |
Output is correct |
59 |
Correct |
32 ms |
716 KB |
Output is correct |
60 |
Correct |
324 ms |
3088 KB |
Output is correct |
61 |
Correct |
31 ms |
744 KB |
Output is correct |
62 |
Correct |
5 ms |
332 KB |
Output is correct |
63 |
Correct |
12 ms |
440 KB |
Output is correct |
64 |
Correct |
20 ms |
588 KB |
Output is correct |
65 |
Correct |
479 ms |
1996 KB |
Output is correct |
66 |
Correct |
466 ms |
1804 KB |
Output is correct |
67 |
Correct |
469 ms |
1724 KB |
Output is correct |
68 |
Correct |
413 ms |
1728 KB |
Output is correct |
69 |
Correct |
303 ms |
1180 KB |
Output is correct |
70 |
Correct |
116 ms |
800 KB |
Output is correct |
71 |
Correct |
47 ms |
616 KB |
Output is correct |
72 |
Correct |
10 ms |
460 KB |
Output is correct |
73 |
Correct |
1016 ms |
4732 KB |
Output is correct |