This documentation is automatically generated by competitive-verifier/competitive-verifier
// competitive-verifier: PROBLEM https://onlinejudge.u-aizu.ac.jp/challenges/sources/JAG/Prelim/2587
// competitive-verifier: TLE 1
// competitive-verifier: MLE 64
#include <iostream>
#include <vector>
#include <set>
#include <array>
#include "src/Misc/Automaton.hpp"
using namespace std;
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
vector<int> low, high;
vector alp= {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
while (cin >> N && N != 0) {
low.resize(N), high.resize(N);
for (int i= 0; i < N; ++i) cin >> low[i] >> high[i];
using state_t= array<int, 2>;
auto tr= [&](state_t s, int c) -> set<state_t> {
auto [i, j]= s;
if (i >= N) return {};
auto [l1, l0]= div(low[i], 10);
auto [h1, h0]= div(high[i], 10);
if (j == 1) {
if (l0 <= c) return {{i + 1, 0}};
} else if (j == 2) return {{i + 1, 0}};
else if (j == 3) {
if (c <= h0) return {{i + 1, 0}};
} else if (j == 4) {
if (l0 <= c && c <= h0) return {{i + 1, 0}};
} else {
if (c == 0) return {};
if (l1 == h1) {
if (c == l1) return {{i, 4}};
} else {
if (c == l1) return {{i, 1}};
if (l1 < c && c < h1) return {{i, 2}};
if (c == h1) return {{i, 3}};
}
}
return {};
};
auto eps= [&](state_t s) -> set<state_t> {
auto [i, j]= s;
if (i >= N) return {};
if (j == 0 && (low[i] / 10) == 0) {
if ((high[i] / 10) == 0) return {{i, 4}};
else return {{i, 1}};
}
return {};
};
auto ac= [&](state_t s) { return s[0] == N && s[1] == 0; };
Automaton nfa(alp, state_t{0, 0}, tr, ac, eps);
int64_t ans= 0;
for (int i= N; i <= 2 * N; ++i) ans+= nfa.num<int64_t>(i);
cout << ans << '\n';
}
return 0;
}
#line 1 "test/aoj/2587.test.cpp"
// competitive-verifier: PROBLEM https://onlinejudge.u-aizu.ac.jp/challenges/sources/JAG/Prelim/2587
// competitive-verifier: TLE 1
// competitive-verifier: MLE 64
#include <iostream>
#include <vector>
#include <set>
#include <array>
#line 2 "src/Misc/Automaton.hpp"
#include <type_traits>
#line 4 "src/Misc/Automaton.hpp"
#include <map>
#include <unordered_map>
#line 7 "src/Misc/Automaton.hpp"
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <cassert>
template <class symbol_t> class Automaton {
std::vector<int> table;
std::vector<int8_t> info;
std::vector<symbol_t> alph;
const int m;
template <class Map, class state_t, class F, class G, class H> void build(const state_t &initial_state, const F &transition, const G &is_accept, const H &abs_reject) {
static_assert(std::is_same_v<bool, std::invoke_result_t<G, state_t>>);
static_assert(std::is_same_v<bool, std::invoke_result_t<H, state_t>>);
Map encode;
std::vector<state_t> decode;
int ts= 0;
decode.push_back(initial_state), encode.emplace(initial_state, ts++);
for (int i= 0, k= 0; i < ts; ++i) {
auto s= decode[i];
table.resize(table.size() + m);
for (int j= 0; j < m; ++j) {
if (auto t= transition(s, j); abs_reject(t)) table[k++]= -1;
else if (auto it= encode.find(t); it != encode.end()) table[k++]= it->second;
else table[k++]= ts, decode.push_back(t), encode.emplace(t, ts++);
}
}
info.resize(ts);
for (int i= ts; i--;) info[i]= is_accept(decode[i]);
}
Automaton(const std::vector<symbol_t> &alphabet): alph(alphabet), m(alph.size()) {}
public:
template <class state_t, class F, class G, std::enable_if_t<std::is_invocable_r_v<state_t, F, state_t, symbol_t>, std::nullptr_t> = nullptr> Automaton(const std::vector<symbol_t> &alphabet, const state_t &initial_state, const F &transition, const G &is_accept): alph(alphabet), m(alph.size()) {
std::sort(alph.begin(), alph.end());
auto tr= [&](const state_t &s, int i) { return transition(s, alph[i]); };
auto rej= [](const state_t &) { return false; };
if constexpr (std::is_integral_v<state_t>) build<std::unordered_map<state_t, int>, state_t>(initial_state, tr, is_accept, rej);
else build<std::map<state_t, int>, state_t>(initial_state, tr, is_accept, rej);
}
template <class state_t, class F, class G, std::enable_if_t<std::is_invocable_r_v<state_t, F, state_t, symbol_t>, std::nullptr_t> = nullptr> Automaton(const std::vector<symbol_t> &alphabet, const state_t &initial_state, const F &transition, const G &is_accept, const state_t &abs_rej_state): alph(alphabet), m(alph.size()) {
std::sort(alph.begin(), alph.end());
auto tr= [&](const state_t &s, int i) { return transition(s, alph[i]); };
auto rej= [abs_rej_state](const state_t &s) { return s == abs_rej_state; };
if constexpr (std::is_integral_v<state_t>) build<std::unordered_map<state_t, int>, state_t>(initial_state, tr, is_accept, rej);
else build<std::map<state_t, int>, state_t>(initial_state, tr, is_accept, rej);
}
template <class state_t, class F, class G, std::enable_if_t<std::is_invocable_r_v<std::set<state_t>, F, state_t, symbol_t>, std::nullptr_t> = nullptr> Automaton(const std::vector<symbol_t> &alphabet, const state_t &initial_state, const F &transition, const G &is_accept): alph(alphabet), m(alph.size()) {
static_assert(std::is_same_v<bool, std::invoke_result_t<G, state_t>>);
std::sort(alph.begin(), alph.end());
auto tr= [&](const std::set<state_t> &s, int i) {
std::set<state_t> ret;
for (const auto &x: s) {
auto ys= transition(x, alph[i]);
ret.insert(ys.begin(), ys.end());
}
return ret;
};
auto ac= [&](const std::set<state_t> &s) { return std::any_of(s.begin(), s.end(), is_accept); };
auto rej= [](const std::set<state_t> &s) { return s == std::set<state_t>(); };
build<std::map<std::set<state_t>, int>, std::set<state_t>>(std::set<state_t>({initial_state}), tr, ac, rej);
}
template <class state_t, class F, class G, class H, std::enable_if_t<std::is_invocable_r_v<std::set<state_t>, F, state_t, symbol_t>, std::nullptr_t> = nullptr> Automaton(const std::vector<symbol_t> &alphabet, const state_t &initial_state, const F &transition, const G &is_accept, const H &eps_trans): alph(alphabet), m(alph.size()) {
static_assert(std::is_same_v<bool, std::invoke_result_t<G, state_t>>);
static_assert(std::is_same_v<std::set<state_t>, std::invoke_result_t<H, state_t>>);
std::sort(alph.begin(), alph.end());
auto eps_closure= [&](std::set<state_t> s) {
for (std::set<state_t> t; s != t;) {
t= s;
for (const auto &x: t) {
auto ys= eps_trans(x);
s.insert(ys.begin(), ys.end());
}
}
return s;
};
auto tr= [&](const std::set<state_t> &s, int i) {
std::set<state_t> ret;
for (const auto &x: s) {
auto ys= transition(x, alph[i]);
ret.insert(ys.begin(), ys.end());
}
return eps_closure(ret);
};
auto ac= [&](const std::set<state_t> &s) { return std::any_of(s.begin(), s.end(), is_accept); };
auto rej= [](const std::set<state_t> &s) { return s == std::set<state_t>(); };
build<std::map<std::set<state_t>, int>, std::set<state_t>>(eps_closure({initial_state}), tr, ac, rej);
}
Automaton operator&(const Automaton &r) const {
assert(alph == r.alph);
const int S= info.size();
auto tr= [&](int s, int q) {
auto [s1, s0]= std::div(s, S);
int t1= r.table[s1 * m + q], t0= table[s0 * m + q];
return t0 == -1 || t1 == -1 ? -1 : t1 * S + t0;
};
auto ac= [&](int s) {
auto [s1, s0]= std::div(s, S);
return info[s0] == 1 && r.info[s1] == 1;
};
auto rej= [](int s) { return s == -1; };
Automaton ret(alph);
return ret.build<std::unordered_map<int, int>, int>(0, tr, ac, rej), ret;
}
template <class T, class A, class F> T dp_run(int n, const A &op, const T &ti, const F &f, const T &init) const {
static_assert(std::is_same_v<T, std::invoke_result_t<A, T, T>>);
static_assert(std::is_same_v<T, std::invoke_result_t<F, T, symbol_t, int>>);
const size_t S= info.size();
std::queue<std::pair<int, int>> que;
T dp[2][S], ret= ti;
bool in[2][S];
for (std::fill_n(dp[0], S, ti), std::fill_n(dp[1], S, ti), std::fill_n(in[0], S, 0), std::fill_n(in[1], S, 0), dp[0][0]= init, que.emplace(0, 0), in[0][0]= 1; que.size();) {
auto [s, i]= que.front();
bool b= i & 1;
T tmp= dp[b][s];
if (que.pop(), in[b][s]= 0, dp[b][s]= ti; i == n) {
if (info[s] == 1) ret= op(ret, tmp);
continue;
}
auto l= table.cbegin() + s * m;
for (int j= m; j--;)
if (int t= l[j]; t != -1)
if (dp[!b][t]= op(dp[!b][t], f(tmp, alph[j], i)); !in[!b][t]) que.emplace(t, i + 1), in[!b][t]= 1;
}
return ret;
}
template <class T> T num(int n) const {
return dp_run(n, [](const T &l, const T &r) { return l + r; }, T(), [](const T &x, const auto &, auto) { return x; }, T(1));
}
};
#line 9 "test/aoj/2587.test.cpp"
using namespace std;
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
vector<int> low, high;
vector alp= {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
while (cin >> N && N != 0) {
low.resize(N), high.resize(N);
for (int i= 0; i < N; ++i) cin >> low[i] >> high[i];
using state_t= array<int, 2>;
auto tr= [&](state_t s, int c) -> set<state_t> {
auto [i, j]= s;
if (i >= N) return {};
auto [l1, l0]= div(low[i], 10);
auto [h1, h0]= div(high[i], 10);
if (j == 1) {
if (l0 <= c) return {{i + 1, 0}};
} else if (j == 2) return {{i + 1, 0}};
else if (j == 3) {
if (c <= h0) return {{i + 1, 0}};
} else if (j == 4) {
if (l0 <= c && c <= h0) return {{i + 1, 0}};
} else {
if (c == 0) return {};
if (l1 == h1) {
if (c == l1) return {{i, 4}};
} else {
if (c == l1) return {{i, 1}};
if (l1 < c && c < h1) return {{i, 2}};
if (c == h1) return {{i, 3}};
}
}
return {};
};
auto eps= [&](state_t s) -> set<state_t> {
auto [i, j]= s;
if (i >= N) return {};
if (j == 0 && (low[i] / 10) == 0) {
if ((high[i] / 10) == 0) return {{i, 4}};
else return {{i, 1}};
}
return {};
};
auto ac= [&](state_t s) { return s[0] == N && s[1] == 0; };
Automaton nfa(alp, state_t{0, 0}, tr, ac, eps);
int64_t ans= 0;
for (int i= N; i <= 2 * N; ++i) ans+= nfa.num<int64_t>(i);
cout << ans << '\n';
}
return 0;
}
Env | Name | Status | Elapsed | Memory |
---|---|---|---|---|
g++-13 | testcase_00 |
![]() |
17 ms | 4 MB |
g++-13 | testcase_01 |
![]() |
19 ms | 4 MB |
clang++-18 | testcase_00 |
![]() |
16 ms | 4 MB |
clang++-18 | testcase_01 |
![]() |
17 ms | 4 MB |