QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#681349 | #9489. 0100 Insertion | nhuang685 | AC ✓ | 96ms | 5032kb | C++23 | 8.2kb | 2024-10-27 05:43:05 | 2024-10-27 05:43:06 |
Judging History
answer
/**
* @author n685
* @brief
* @date 2024-10-26 16:22:15
*
*
*/
#include <algorithm>
#include "bits/stdc++.h"
#ifdef LOCAL
#include "dd/debug.h"
#else
#define dbg(...) 42
#define dbg_proj(...) 420
#define dbg_rproj(...) 420420
void nline() {}
void bar() {}
void start_clock() {}
void end_clock() {}
#endif
namespace rs = std::ranges;
namespace rv = std::views;
template <class T> constexpr std::pair<T, T> ex_eucl(T a, T b) {
if (a < b) {
auto [x, y] = ex_eucl(b, a);
return {y, x};
}
if (b == 0) {
assert(a == 1);
return {1, 0};
}
auto [x, y] = ex_eucl(b, a % b);
return {y, x - (a / b) * y};
}
template <class Md, class V = int64_t>
requires std::signed_integral<std::decay_t<decltype(Md::value)>>
struct Mod {
using T = std::decay_t<decltype(Md::value)>;
T val = 0;
static constexpr T normalize(std::integral auto val) {
using U = decltype(Md::value + val);
U uval = static_cast<U>(val);
U umd = static_cast<U>(Md::value);
if (uval <= -umd || umd <= uval) {
uval %= umd;
}
if (val < 0) {
uval += umd;
}
return static_cast<T>(uval);
}
constexpr Mod() : val(0) {}
constexpr explicit Mod(std::integral auto _val) : val(normalize(_val)) {}
static inline const Mod ZERO = Mod(0);
static inline const Mod ONE = Mod(1);
static inline const Mod TWO = Mod(2);
// addition
constexpr Mod& operator+=(Mod b) {
val += b.val;
if (val >= Md::value) {
val -= Md::value;
}
return *this;
}
friend constexpr Mod operator+(Mod a, Mod b) { return a += b; }
constexpr Mod& operator++() { return *this += Mod(1); }
constexpr Mod operator++(int) {
Mod res = *this;
++(*this);
return res;
}
// subtraction
constexpr Mod& operator-=(Mod b) {
val -= b.val;
if (val < 0) {
val += Md::value;
}
return *this;
}
friend constexpr Mod operator-(Mod a, Mod b) { return a -= b; }
constexpr Mod& operator--() { return *this -= Mod(1); }
constexpr Mod operator--(int) {
Mod res = *this;
--(*this);
return res;
}
// negation
constexpr Mod operator-() const { return Mod(-val); }
// multiplication
constexpr Mod& operator*=(Mod b) {
val = static_cast<T>(static_cast<V>(val) * b.val % Md::value);
return *this;
}
friend constexpr Mod operator*(Mod a, Mod b) { return a *= b; }
constexpr Mod binpow(std::integral auto b) const {
Mod res = Mod(1), a = *this;
while (b > 0) {
if (b % 2 == 1) {
res *= a;
}
a *= a;
b /= 2;
}
return res;
}
// factorial
// align with fft, if code fails to compile make this smaller (if using array)
static constexpr int MXINV = 1 << 22;
static inline bool init = false;
static inline std::vector<Mod> ff, iff;
static void reset_fac() { init = false; }
static void init_fac() {
if (init) {
return;
}
ff.resize(MXINV + 1);
ff[0] = Mod(1);
for (int i = 1; i <= MXINV; ++i) {
ff[i] = ff[i - 1] * Mod(i);
}
iff.resize(MXINV + 1);
iff[MXINV] = ff[MXINV].large_inv();
for (int i = MXINV - 1; i >= 0; --i) {
iff[i] = iff[i + 1] * Mod(i + 1);
}
init = true;
}
static Mod fac(int v) {
if (!init) {
init_fac();
}
return ff[v];
}
static Mod ifac(int v) {
if (!init) {
init_fac();
}
return iff[v];
}
static Mod comb(int n, int k) {
if (n < 0 || k < 0 || n < k) {
return Mod(0);
}
return fac(n) * ifac(n - k) * ifac(k);
}
static Mod perm(int n, int k) {
if (n < 0 || k < 0 || n < k) {
return Mod(0);
}
return fac(n) * ifac(n - k);
}
// inverse
Mod small_inv() const { return ifac(static_cast<int>(val)) * fac(static_cast<int>(val) - 1); }
constexpr Mod large_inv() const {
return Mod(ex_eucl(static_cast<V>(val), static_cast<V>(Md::value)).first);
// return binpow(Md::value - 2);
}
Mod inv() const {
if (val <= MXINV) {
return small_inv();
}
return large_inv();
}
// sqrt
std::optional<Mod> sqrt() {
static std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());
Mod c = Mod::ZERO;
while ((c * c - *this).binpow((Md::value - 1) / 2) == Mod::ONE) {
c = Mod(rng());
}
if (c == Mod::ZERO) {
return std::nullopt;
}
std::pair<Mod, Mod> res(Mod::ONE, Mod::ZERO), a(c, Mod::ONE);
T b = (Md::value + 1) / 2;
auto mul
= [&c,
this](const std::pair<Mod, Mod>& u, const std::pair<Mod, Mod>& v) -> std::pair<Mod, Mod> {
return {
u.first * v.first + u.second * v.second * (c * c - *this),
u.second * v.first + u.first * v.second
};
};
while (b > 0) {
if (b % 2 == 1) {
res = mul(res, a);
}
a = mul(a, a);
b /= 2;
}
return res.first;
// return std::min(res.first, -res.first);
}
// comparison
constexpr bool operator==(const Mod& b) const = default;
constexpr std::strong_ordering operator<=>(const Mod& b) const = default;
// io
friend std::istream& operator>>(std::istream& in, Mod& a) {
int64_t v;
in >> v;
a = Mod(v);
return in;
}
friend std::ostream& operator<<(std::ostream& out, const Mod& a) {
out << a.val;
return out;
}
// conversion
constexpr T value() const { return val; }
};
#if defined(LOCAL) && __cplusplus >= 202302L
template <class Md, class V>
requires std::formattable<typename Mod<Md, V>::T, char>
struct std::formatter<Mod<Md, V>, char> {
using T = typename Mod<Md, V>::T;
std::formatter<T, char> underlying;
constexpr formatter() {
if constexpr (requires { underlying.set_debug_format(); }) {
underlying.set_debug_format();
}
}
template <class ParseContext> constexpr auto parse(ParseContext& ctx) {
return underlying.parse(ctx);
}
template <class FormatContext> auto format(const Mod<Md, V>& val, FormatContext& ctx) const {
return underlying.format(val.value(), ctx);
}
};
#endif
constexpr int MOD = 998'244'353;
using Mint = Mod<std::integral_constant<std::decay_t<decltype(MOD)>, MOD>>;
template <class T> struct NArr {
using iterator = std::vector<T>::iterator;
using const_iterator = std::vector<T>::const_iterator;
int sz{0}, off{0};
std::vector<T> data;
NArr() = default;
NArr(int l, int r) : sz(r - l + 1), off(l), data(sz) {}
NArr(int l, int r, const T& v) : sz(r - l + 1), off(l), data(sz, v) {}
T& operator[](int ind) { return data[ind - off]; }
const T& operator[](int ind) const { return data[ind - off]; }
iterator begin() { return data.begin(); }
const_iterator begin() const { return data.begin(); }
iterator end() { return data.end(); }
const_iterator end() const { return data.end(); }
// void swap(NArr& other) noexcept {
// std::swap(sz, other.sz);
// std::swap(off, other.off);
// data.swap(other.data);
// }
};
int main() {
#ifndef LOCAL
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
#endif
int n;
std::cin >> n;
n /= 4;
std::vector<char> s(4 * n);
for (int i = 4 * n - 1; i >= 0; --i) {
char c;
std::cin >> c;
s[i] = c;
}
NArr id(-n, 3 * n, NArr<std::array<Mint, 2>>(-n, 0));
std::array<NArr<NArr<std::array<Mint, 2>>>, 2> dp;
dp.fill(id);
dp[0][0][0][0] = Mint::ONE;
for (int i = 0; i < 4 * n; ++i) {
for (int pre = -n; pre <= 3 * n; ++pre) {
for (int mi = -n; mi <= std::min(0, pre); ++mi) {
for (int last : {0, 1}) {
if (dp[0][pre][mi][last] == Mint::ZERO) {
continue;
}
// add 1 (0)
if (s[i] != '1' && pre + 1 <= 3 * n) {
dp[1][pre + 1][mi][0] += dp[0][pre][mi][last];
}
// subtract 3 (1)
if (s[i] != '0' && last != 1 && pre - 3 >= std::max(-n, mi - 1)) {
dp[1][pre - 3][std::min(pre - 3, mi)][1] += dp[0][pre][mi][last];
}
}
}
}
// dp[0].swap(dp[1]);
std::swap(dp[0], dp[1]);
dp[1] = id;
}
Mint ans{0};
for (int mi = -n; mi <= 0; ++mi) {
ans += dp[0][0][mi][0];
}
std::cout << ans << '\n';
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3624kb
input:
8 0??0?100
output:
2
result:
ok "2"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3820kb
input:
4 ?10?
output:
1
result:
ok "1"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3504kb
input:
28 ???????????0???0??????1???0?
output:
2023
result:
ok "2023"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3552kb
input:
4 ????
output:
1
result:
ok "1"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3656kb
input:
8 11111111
output:
0
result:
ok "0"
Test #6:
score: 0
Accepted
time: 94ms
memory: 4964kb
input:
500 ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????...
output:
870731023
result:
ok "870731023"
Test #7:
score: 0
Accepted
time: 96ms
memory: 4836kb
input:
500 ???????????????????0???????????????????????????????????????0??????????????????????1???????????????????????????????????????00????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????1???????????????????????????????????...
output:
763641704
result:
ok "763641704"
Test #8:
score: 0
Accepted
time: 28ms
memory: 4256kb
input:
344 ?0???????????1???????0?0?????0?????????????0?0????0?0??1??0?????0??0???1?????0????00?????1?????1????????1?????10????0??????????1????0???1????????0???10?????1?0?0???1??????????0??0???1?0??00??0???????001????????1????1??????1?0????1??????????????????1????1????????????0???????????01?1??????0?0?????...
output:
273835616
result:
ok "273835616"
Test #9:
score: 0
Accepted
time: 3ms
memory: 3652kb
input:
152 ?1???1??????????????????00??????????0?????0????????0??1?????0????0????????0????010???????????????1??1??????0?10??1????0???????????????1?????0???1???????
output:
539361138
result:
ok "539361138"
Test #10:
score: 0
Accepted
time: 7ms
memory: 4120kb
input:
216 ???????1?????1??1????1???1?????????????0?1???????????????????????????????????????????????1?????????????????10???0???1???????????????????????1????????????0???????0??????0?????????1???????????????????????1?????????0???
output:
336079328
result:
ok "336079328"
Test #11:
score: 0
Accepted
time: 23ms
memory: 4184kb
input:
316 ??????00?????????????????????1??????????????????0?????????????????0??0??????0???1??1?????????0??????????????????????0?????????0?0?1??0?????1?1??????1?1?????????????????????????????????0??????????????????????10???????1???????????1?????????????1????1????????????0????????????????1????1???0??0??1???...
output:
919261244
result:
ok "919261244"
Test #12:
score: 0
Accepted
time: 28ms
memory: 4188kb
input:
340 ?0?001??1?0????1?????1???01?????????1??01??0???1???1?????????????0?1??????1??1?10??0????????10?????1????1?010???1?????????????1???????1????01???????1???????00???1?0?00??0????????00???0??????1??1?1?????10??1????0??1????1????0????????0????0??????00?0??00???1??001????????0?1?????????????????0???1??...
output:
743485908
result:
ok "743485908"
Test #13:
score: 0
Accepted
time: 44ms
memory: 4332kb
input:
392 ??????0??????????10???????????????????????1????????1???????0???????????0??????????????101???????????????????????0??????????0??0??????????1?????1???????????????????1?0?????????????1???????????????????????1????????0???0?????1????????????????0??????1?????????????0????????????????0?????????????????1...
output:
213859453
result:
ok "213859453"
Test #14:
score: 0
Accepted
time: 0ms
memory: 3620kb
input:
96 ????????0??1??1??????????????????????????1?0?????0??????1?????????1?0?????00??????????1???1?????
output:
860127276
result:
ok "860127276"
Test #15:
score: 0
Accepted
time: 2ms
memory: 3748kb
input:
116 ???????????????????????????????01?1???????????????0?00????0?????????????????1?0?????????????????1???????????????00??
output:
717954632
result:
ok "717954632"
Test #16:
score: 0
Accepted
time: 15ms
memory: 4092kb
input:
280 ?????1????0??0??????1????????0????0?1?0????????00???0???????0????0???????????1?0?1???????????1?????10????1??????100??????????1?1?????01?1??????0????01??1???1?????0????????0?1?0???????????01??00??????1?????????????????1?1?????????1????????????????0????????????????????????0????????
output:
553679844
result:
ok "553679844"
Test #17:
score: 0
Accepted
time: 13ms
memory: 3984kb
input:
260 ????????0????0???????????????1???????0?00??0???????????????0??????0?????1?????????????????????????????????????0???????1????????0??????????????????????1???????????????????1?0????1???????00?0????10???????????0?1?10??????????????????0??0???????1???????1??1???????
output:
121486637
result:
ok "121486637"
Test #18:
score: 0
Accepted
time: 93ms
memory: 4688kb
input:
500 ??0????1???1??????1?1??????????????????????????????????0??0??????????????????????1???????01????????0???0???????????????????????1????????1??????????????1????????0?1???0?????0???????????????????0??????????????1????????0???????0?0??????????????0???????1???????1??????????????????????????0????0??????...
output:
397451125
result:
ok "397451125"
Test #19:
score: 0
Accepted
time: 93ms
memory: 5032kb
input:
500 ??????1???????????????????0????????????????????????0??????0???????????1??????????10???0???0?????????????1??1????????0??????1???????????00????????????0???????????????????1??0???0????0?????????????0?????????????????????????????????????0????0??????????1??????????????????????????????????????????????...
output:
248838567
result:
ok "248838567"
Test #20:
score: 0
Accepted
time: 80ms
memory: 4744kb
input:
500 ?0??0????1???????????????????????????1????????????????????0????????1????????????????1????0?????????????1???????????1?0????????????????????0?????????????0????0????????????1????????1????????????????0???0?1???1???1???????????1?????1???0??????????1????????1?????1???0?1???????????1???????????????????...
output:
53824210
result:
ok "53824210"
Test #21:
score: 0
Accepted
time: 1ms
memory: 3668kb
input:
76 ????0????????0??0???1??0??????00?0????0???1??????1?0????0?1?????????????1???
output:
205686585
result:
ok "205686585"
Test #22:
score: 0
Accepted
time: 0ms
memory: 3560kb
input:
24 ?0?0?????0??????????????
output:
587
result:
ok "587"
Test #23:
score: 0
Accepted
time: 1ms
memory: 3636kb
input:
76 ?????0?????????????????0????????????????????????????1???0???????????????????
output:
83575746
result:
ok "83575746"
Test #24:
score: 0
Accepted
time: 0ms
memory: 3568kb
input:
28 00???0???0?0??0???0???0?????
output:
859
result:
ok "859"
Test #25:
score: 0
Accepted
time: 0ms
memory: 3516kb
input:
44 ?00??1???00??1???0??1?100??1?0???0??1???0???
output:
63
result:
ok "63"
Test #26:
score: 0
Accepted
time: 63ms
memory: 4780kb
input:
444 ???0???0???????????????1?????1??????0?????1??????1??????????10?1???????????????????????????????1?1??00?0??0????0??????????1???????101?0????10??????????????0?0???????1???????????????1????????0?????????1????????????1???0??????????????????????????0??????1??????????0???10??1???????????01????????????...
output:
847293915
result:
ok "847293915"
Test #27:
score: 0
Accepted
time: 76ms
memory: 4748kb
input:
484 ?????1?1???????????1?????1??????????0?1???????????1?????0?1???0000?1?1?0?00????1????0?0?00?????????????????????1????????1?01??????01??1?????0??????0???000?????????1????????0?1??01?????0???1???0?????1???0???????0?????1?1?0??0???????01????????0???1???0??0???0?????????????????1??????1???10??1010???...
output:
679474848
result:
ok "679474848"
Test #28:
score: 0
Accepted
time: 65ms
memory: 4524kb
input:
456 ?????0???????????????0?????1???????0???1??????????????????1??????1?????????????1??????1??????????0??????????????1????????????????????1??????????1???0???0??1???1????????00??0?????1???0?????????????1?0??1??1?1???0???1????????????0???????????????0?0??0?????0????????01?????????????????????????????10...
output:
920943116
result:
ok "920943116"
Test #29:
score: 0
Accepted
time: 78ms
memory: 4672kb
input:
484 ????????????????????????????????0????1???1???????1????????1?????????0?????1??1??????????????????????1???????01???????????????????1??1???????????????1?1?0?0??1????????1??1???????????????????????????0?????????????0?????????????1????0??????1?????????0?0??????????1????????????1??????????????????????...
output:
453361220
result:
ok "453361220"
Test #30:
score: 0
Accepted
time: 63ms
memory: 4532kb
input:
448 ?1?01??1????01?10????1???0????????????10????0??????1????1??????1?????1?0??1????101?1????1??????????????0?????1???0???????01?0?1?1????1???1?01?????????????0?1???1???01????0???????????????1??????01?1??????1?1?1??0???0?????????1????????1??1?0?1?0???????????????1?????1???0?0?????0????0??0???????????...
output:
84175678
result:
ok "84175678"
Test #31:
score: 0
Accepted
time: 76ms
memory: 4880kb
input:
480 ?0?????????????????????0???00???1???0?????1?????????1??????????1?????1????????0??????????1????0???10?????????1????1????0?????0???01??????????0?????0???0?????????0???????01??????????????0????????0?????1????1?0??0?0????????????????1???1??01????????????1????0??????0??????1??1???????????????????????...
output:
247404334
result:
ok "247404334"
Test #32:
score: 0
Accepted
time: 86ms
memory: 4716kb
input:
492 ??????00??????0??0??????????1???1?1?010????????????????????00???1???0?????0???????1?????????0???????1?????????????1???10???0?????????????????0??????????????0??????1???0????0?0???0??100?0?????????1??1????1???????????1????1??0?????????00??????1??1??????1????01?1?0??????0???????1????????????1??0???...
output:
487405442
result:
ok "487405442"
Test #33:
score: 0
Accepted
time: 68ms
memory: 4556kb
input:
456 ????1???????????1???1????0????????????????????????????????????1???????0?????1???1?1??????????????????????????1?????????1?00??????????10?0????????????1??????????1?0????????1?1?????????????????0??????0???????1???????????????????????????0????????0??????1??????????0??1???????????????????????????????...
output:
884555628
result:
ok "884555628"
Test #34:
score: 0
Accepted
time: 72ms
memory: 4660kb
input:
460 ??????1????1??????????0??0?????????????????0?????0???????????1?????????????1???????????01???????0????????????0???0???????????????????????????1???0????0??????????0??0??????1????0???????????????01?????????????0???????????????????????????????1????0????0????1??????1???????1???0??????????????????????...
output:
244536851
result:
ok "244536851"
Test #35:
score: 0
Accepted
time: 66ms
memory: 4556kb
input:
456 ???????1?1????????????????00??????????1??1?0???????0??????0??????????0??1?????0??1???0?1??1?1?1???00?10?0???01??????????????0???01?0??000?1????1????????0????1?1????1???01???0?????1?0??????????1???0???1?1??????????01???1?????0???????0?1????1???1?0??0????1????0?0??1????????????0???0?????0??????01?...
output:
890392505
result:
ok "890392505"
Extra Test:
score: 0
Extra Test Passed