QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#656993 | #9489. 0100 Insertion | ucup-team008# | AC ✓ | 1651ms | 19000kb | C++17 | 8.4kb | 2024-10-19 14:01:00 | 2024-10-19 14:01:01 |
Judging History
answer
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
using namespace std;
// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
typedef vector<int> vi;
#define f first
#define s second
#define derr if(0) cerr
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<" [" << #x << "] = ["; _print(x); cerr << "\e[39m" << flush;
// END NO SAD
template<class Fun>
class y_combinator_result {
Fun fun_;
public:
template<class T>
explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}
template<class ...Args>
decltype(auto) operator()(Args &&...args) {
return fun_(std::ref(*this), std::forward<Args>(args)...);
}
};
template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
template<class T>
bool updmin(T& a, T b) {
if(b < a) {
a = b;
return true;
}
return false;
}
template<class T>
bool updmax(T& a, T b) {
if(b > a) {
a = b;
return true;
}
return false;
}
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
struct barrett_reduction {
unsigned mod;
uint64_t div;
barrett_reduction(unsigned m) : mod(m), div(-1LLU / m) {}
unsigned operator()(uint64_t a) const {
#ifdef __SIZEOF_INT128__
uint64_t q = uint64_t(__uint128_t(div) * a >> 64);
uint64_t r = a - q * mod;
return unsigned(r < mod ? r : r - mod);
#endif
return unsigned(a % mod);
}
};
template<const int &MOD, const barrett_reduction &barrett>
struct _b_int {
int val;
_b_int(int64_t v = 0) {
if (v < 0) v = v % MOD + MOD;
if (v >= MOD) v %= MOD;
val = int(v);
}
_b_int(uint64_t v) {
if (v >= uint64_t(MOD)) v %= MOD;
val = int(v);
}
_b_int(int v) : _b_int(int64_t(v)) {}
_b_int(unsigned v) : _b_int(uint64_t(v)) {}
static int inv_mod(int a, int m = MOD) {
// https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Example
int g = m, r = a, x = 0, y = 1;
while (r != 0) {
int q = g / r;
g %= r; swap(g, r);
x -= q * y; swap(x, y);
}
return x < 0 ? x + m : x;
}
explicit operator int() const { return val; }
explicit operator unsigned() const { return val; }
explicit operator int64_t() const { return val; }
explicit operator uint64_t() const { return val; }
explicit operator double() const { return val; }
explicit operator long double() const { return val; }
_b_int& operator+=(const _b_int &other) {
val -= MOD - other.val;
if (val < 0) val += MOD;
return *this;
}
_b_int& operator-=(const _b_int &other) {
val -= other.val;
if (val < 0) val += MOD;
return *this;
}
static unsigned fast_mod(uint64_t x) {
#if !defined(_WIN32) || defined(_WIN64)
return barrett(x);
#endif
// Optimized mod for Codeforces 32-bit machines.
// x must be less than 2^32 * MOD for this to work, so that x / MOD fits in an unsigned 32-bit int.
unsigned x_high = unsigned(x >> 32), x_low = unsigned(x);
unsigned quot, rem;
asm("divl %4\n"
: "=a" (quot), "=d" (rem)
: "d" (x_high), "a" (x_low), "r" (MOD));
return rem;
}
_b_int& operator*=(const _b_int &other) {
val = fast_mod(uint64_t(val) * other.val);
return *this;
}
_b_int& operator/=(const _b_int &other) {
return *this *= other.inv();
}
friend _b_int operator+(const _b_int &a, const _b_int &b) { return _b_int(a) += b; }
friend _b_int operator-(const _b_int &a, const _b_int &b) { return _b_int(a) -= b; }
friend _b_int operator*(const _b_int &a, const _b_int &b) { return _b_int(a) *= b; }
friend _b_int operator/(const _b_int &a, const _b_int &b) { return _b_int(a) /= b; }
_b_int& operator++() {
val = val == MOD - 1 ? 0 : val + 1;
return *this;
}
_b_int& operator--() {
val = val == 0 ? MOD - 1 : val - 1;
return *this;
}
_b_int operator++(int) { _b_int before = *this; ++*this; return before; }
_b_int operator--(int) { _b_int before = *this; --*this; return before; }
_b_int operator-() const {
return val == 0 ? 0 : MOD - val;
}
friend bool operator==(const _b_int &a, const _b_int &b) { return a.val == b.val; }
friend bool operator!=(const _b_int &a, const _b_int &b) { return a.val != b.val; }
friend bool operator<(const _b_int &a, const _b_int &b) { return a.val < b.val; }
friend bool operator>(const _b_int &a, const _b_int &b) { return a.val > b.val; }
friend bool operator<=(const _b_int &a, const _b_int &b) { return a.val <= b.val; }
friend bool operator>=(const _b_int &a, const _b_int &b) { return a.val >= b.val; }
_b_int inv() const {
return inv_mod(val);
}
_b_int pow(int64_t p) const {
if (p < 0)
return inv().pow(-p);
_b_int a = *this, result = 1;
while (p > 0) {
if (p & 1)
result *= a;
p >>= 1;
if (p > 0)
a *= a;
}
return result;
}
friend ostream& operator<<(ostream &os, const _b_int &m) {
return os << m.val;
}
friend istream& operator>>(istream &is, _b_int &m) {
int64_t x;
is >> x;
m = x;
return is;
}
};
int MOD = 998244353;
barrett_reduction barrett(MOD);
using mnum = _b_int<MOD, barrett>;
void solve() {
int n;
string s;
cin >> n >> s;
reverse(all(s));
vector<vector<array<mnum, 2>>> dp(4*n); // <sum, min, lhs>
for(auto& x: dp) {
x.resize(n+1);
}
dp[3*n][n][0] = 1;
for(int i = 0; i < n; i++) {
vector<vector<array<mnum, 2>>> ndp(4*n+1);
for(auto& x: ndp) x.resize(n+1);
for(int a = 0; a < sz(dp); a++) for(int b = 0; b < sz(dp[a]); b++) for(int c = 0; c < sz(dp[a][b]); c++) {
if(dp[a][b][c] == 0) continue;
for(int x = 0; x < 2; x++) {
if(s[i] != '?' && s[i]-'0' != x) continue;
if(x == 1 && c == 1) continue;
if(x == 1 && i <= 1) continue;
if(x == 1 && i == n-1) continue;
int na = a + (x == 0 ? 1 : -3);
int nb = min(b, na-2*n);
if(b-nb >= 2) continue;
if(na < 0 || na >= 4*n+1) continue;
if(nb < 0 || nb > n) continue;
ndp[na][nb][x] += dp[a][b][c];
}
}
dp.swap(ndp);
}
mnum ret = 0;
for(int a = 0; a <= n; a++) ret += dp[3*n][a][0];
cout << ret << "\n";
}
// what would chika do
// are there edge cases?
// did you actually sort the thing instead of just thinking it?
// integer overflow?
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3604kb
input:
8 0??0?100
output:
2
result:
ok "2"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3628kb
input:
4 ?10?
output:
1
result:
ok "1"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3788kb
input:
28 ???????????0???0??????1???0?
output:
2023
result:
ok "2023"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3732kb
input:
4 ????
output:
1
result:
ok "1"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3544kb
input:
8 11111111
output:
0
result:
ok "0"
Test #6:
score: 0
Accepted
time: 1649ms
memory: 18904kb
input:
500 ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????...
output:
870731023
result:
ok "870731023"
Test #7:
score: 0
Accepted
time: 1651ms
memory: 18880kb
input:
500 ???????????????????0???????????????????????????????????????0??????????????????????1???????????????????????????????????????00????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????1???????????????????????????????????...
output:
763641704
result:
ok "763641704"
Test #8:
score: 0
Accepted
time: 534ms
memory: 10820kb
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: 48ms
memory: 4660kb
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: 134ms
memory: 6244kb
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: 419ms
memory: 9500kb
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: 515ms
memory: 10440kb
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: 790ms
memory: 12932kb
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: 10ms
memory: 3924kb
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: 25ms
memory: 4312kb
input:
116 ???????????????????????????????01?1???????????????0?00????0?????????????????1?0?????????????????1???????????????00??
output:
717954632
result:
ok "717954632"
Test #16:
score: 0
Accepted
time: 289ms
memory: 8172kb
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: 237ms
memory: 7580kb
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: 1648ms
memory: 19000kb
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: 1619ms
memory: 18880kb
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: 1650ms
memory: 18876kb
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: 7ms
memory: 3928kb
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: 1ms
memory: 3848kb
input:
24 ?0?0?????0??????????????
output:
587
result:
ok "587"
Test #23:
score: 0
Accepted
time: 7ms
memory: 3924kb
input:
76 ?????0?????????????????0????????????????????????????1???0???????????????????
output:
83575746
result:
ok "83575746"
Test #24:
score: 0
Accepted
time: 0ms
memory: 3660kb
input:
28 00???0???0?0??0???0???0?????
output:
859
result:
ok "859"
Test #25:
score: 0
Accepted
time: 2ms
memory: 3664kb
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: 1144ms
memory: 15632kb
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: 1487ms
memory: 17876kb
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: 1210ms
memory: 16360kb
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: 1488ms
memory: 17820kb
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: 1178ms
memory: 15896kb
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: 1453ms
memory: 17560kb
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: 1577ms
memory: 18404kb
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: 1242ms
memory: 16348kb
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: 1181ms
memory: 16684kb
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: 1236ms
memory: 16296kb
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