This documentation is automatically generated by competitive-verifier/competitive-verifier
// competitive-verifier: PROBLEM https://yukicoder.me/problems/no/362
// competitive-verifier: TLE 0.5
// competitive-verifier: MLE 64
#include <iostream>
#include <vector>
#include <array>
#include "src/Misc/Automaton.hpp"
using namespace std;
signed main() {
cin.tie(0);
ios::sync_with_stdio(0);
vector alp= {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
auto tr_le= [&](int64_t s, int c) { return (s - c + 10) / 10 - 1; };
auto ac_le= [&](int64_t) { return true; };
using state_t= array<int, 2>;
auto tr_kado= [&](state_t s, int c) -> state_t {
if (s[0] == -1) {
if (s[1] == c) return {-2, -2};
else return {s[1], c};
}
if (s[0] == c || s[1] == c) return {-2, -2};
auto [mi, mx]= minmax({s[0], s[1], c});
if (s[1] == mi || s[1] == mx) return {s[1], c};
return {-2, -2};
};
auto ac_kado= [&](state_t s) { return true; };
Automaton dfa_kado(alp, state_t{-1, -1}, tr_kado, ac_kado, state_t{-2, -2});
auto tr_0= [&](bool, int c) -> bool { return c; };
Automaton dfa_0(alp, false, tr_0, [](bool s) { return s; });
auto dfa1= dfa_kado & dfa_0;
int T;
cin >> T;
while (T--) {
int64_t K;
cin >> K;
int64_t l= 101, h= 37294859064823;
while (h - l > 1) {
int64_t X= (h + l) / 2;
int n= 0;
for (auto m= X; m; m/= 10) ++n;
Automaton dfa_le(alp, X, tr_le, ac_le, int64_t(-1));
auto dfa= dfa1 & dfa_le;
int64_t sum= dfa.num<int64_t>(n);
for (int i= 3; i < n; ++i) sum+= dfa1.num<int64_t>(i);
if (sum < K) l= X;
else h= X;
}
cout << h << '\n';
}
return 0;
}
#line 1 "test/yukicoder/362.test.cpp"
// competitive-verifier: PROBLEM https://yukicoder.me/problems/no/362
// competitive-verifier: TLE 0.5
// competitive-verifier: MLE 64
#include <iostream>
#include <vector>
#include <array>
#line 2 "src/Misc/Automaton.hpp"
#include <type_traits>
#include <set>
#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 8 "test/yukicoder/362.test.cpp"
using namespace std;
signed main() {
cin.tie(0);
ios::sync_with_stdio(0);
vector alp= {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
auto tr_le= [&](int64_t s, int c) { return (s - c + 10) / 10 - 1; };
auto ac_le= [&](int64_t) { return true; };
using state_t= array<int, 2>;
auto tr_kado= [&](state_t s, int c) -> state_t {
if (s[0] == -1) {
if (s[1] == c) return {-2, -2};
else return {s[1], c};
}
if (s[0] == c || s[1] == c) return {-2, -2};
auto [mi, mx]= minmax({s[0], s[1], c});
if (s[1] == mi || s[1] == mx) return {s[1], c};
return {-2, -2};
};
auto ac_kado= [&](state_t s) { return true; };
Automaton dfa_kado(alp, state_t{-1, -1}, tr_kado, ac_kado, state_t{-2, -2});
auto tr_0= [&](bool, int c) -> bool { return c; };
Automaton dfa_0(alp, false, tr_0, [](bool s) { return s; });
auto dfa1= dfa_kado & dfa_0;
int T;
cin >> T;
while (T--) {
int64_t K;
cin >> K;
int64_t l= 101, h= 37294859064823;
while (h - l > 1) {
int64_t X= (h + l) / 2;
int n= 0;
for (auto m= X; m; m/= 10) ++n;
Automaton dfa_le(alp, X, tr_le, ac_le, int64_t(-1));
auto dfa= dfa1 & dfa_le;
int64_t sum= dfa.num<int64_t>(n);
for (int i= 3; i < n; ++i) sum+= dfa1.num<int64_t>(i);
if (sum < K) l= X;
else h= X;
}
cout << h << '\n';
}
return 0;
}
Env | Name | Status | Elapsed | Memory |
---|---|---|---|---|
g++-13 | 0_1_sample.txt |
![]() |
34 ms | 4 MB |
g++-13 | 0_2_sample.txt |
![]() |
28 ms | 4 MB |
g++-13 | 1_1.txt |
![]() |
28 ms | 4 MB |
g++-13 | 1_2.txt |
![]() |
17 ms | 4 MB |
g++-13 | 1_3.txt |
![]() |
11 ms | 4 MB |
g++-13 | 1_4.txt |
![]() |
43 ms | 4 MB |
g++-13 | 1_5.txt |
![]() |
77 ms | 4 MB |
g++-13 | 1_6.txt |
![]() |
46 ms | 4 MB |
g++-13 | 1_7.txt |
![]() |
65 ms | 4 MB |
g++-13 | 1_8.txt |
![]() |
106 ms | 4 MB |
g++-13 | 1_9.txt |
![]() |
146 ms | 4 MB |
g++-13 | 2_1.txt |
![]() |
138 ms | 4 MB |
g++-13 | 2_2.txt |
![]() |
140 ms | 4 MB |
g++-13 | 2_3.txt |
![]() |
144 ms | 4 MB |
g++-13 | 2_4.txt |
![]() |
138 ms | 4 MB |
g++-13 | 2_5.txt |
![]() |
142 ms | 4 MB |
g++-13 | 3_1.txt |
![]() |
135 ms | 4 MB |
g++-13 | 3_2.txt |
![]() |
117 ms | 4 MB |
g++-13 | 3_3.txt |
![]() |
147 ms | 4 MB |
g++-13 | 3_4.txt |
![]() |
63 ms | 4 MB |
g++-13 | 3_5.txt |
![]() |
150 ms | 4 MB |
g++-13 | 4_1_sample.txt |
![]() |
20 ms | 4 MB |
clang++-18 | 0_1_sample.txt |
![]() |
32 ms | 4 MB |
clang++-18 | 0_2_sample.txt |
![]() |
26 ms | 4 MB |
clang++-18 | 1_1.txt |
![]() |
26 ms | 4 MB |
clang++-18 | 1_2.txt |
![]() |
16 ms | 4 MB |
clang++-18 | 1_3.txt |
![]() |
10 ms | 4 MB |
clang++-18 | 1_4.txt |
![]() |
40 ms | 4 MB |
clang++-18 | 1_5.txt |
![]() |
72 ms | 4 MB |
clang++-18 | 1_6.txt |
![]() |
44 ms | 4 MB |
clang++-18 | 1_7.txt |
![]() |
60 ms | 4 MB |
clang++-18 | 1_8.txt |
![]() |
97 ms | 4 MB |
clang++-18 | 1_9.txt |
![]() |
135 ms | 4 MB |
clang++-18 | 2_1.txt |
![]() |
127 ms | 4 MB |
clang++-18 | 2_2.txt |
![]() |
127 ms | 4 MB |
clang++-18 | 2_3.txt |
![]() |
131 ms | 4 MB |
clang++-18 | 2_4.txt |
![]() |
126 ms | 4 MB |
clang++-18 | 2_5.txt |
![]() |
133 ms | 4 MB |
clang++-18 | 3_1.txt |
![]() |
126 ms | 4 MB |
clang++-18 | 3_2.txt |
![]() |
108 ms | 4 MB |
clang++-18 | 3_3.txt |
![]() |
135 ms | 4 MB |
clang++-18 | 3_4.txt |
![]() |
58 ms | 4 MB |
clang++-18 | 3_5.txt |
![]() |
138 ms | 4 MB |
clang++-18 | 4_1_sample.txt |
![]() |
20 ms | 4 MB |