This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// 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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |