QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#681342 | #9489. 0100 Insertion | nhuang685 | WA | 99ms | 4808kb | C++23 | 8.2kb | 2024-10-27 05:38:20 | 2024-10-27 05:38:21 |
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 = 1'000'000'007;
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]);
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: 3524kb
input:
8 0??0?100
output:
2
result:
ok "2"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3540kb
input:
4 ?10?
output:
1
result:
ok "1"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3656kb
input:
28 ???????????0???0??????1???0?
output:
2023
result:
ok "2023"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3592kb
input:
4 ????
output:
1
result:
ok "1"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3620kb
input:
8 11111111
output:
0
result:
ok "0"
Test #6:
score: -100
Wrong Answer
time: 99ms
memory: 4808kb
input:
500 ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????...
output:
467681810
result:
wrong answer 1st words differ - expected: '870731023', found: '467681810'