QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#673654#9489. 0100 Insertionhos_lyricAC ✓90ms200896kbC++147.6kb2024-10-25 05:46:232024-10-25 05:46:24

Judging History

你现在查看的是最新测评结果

  • [2024-10-25 05:46:24]
  • 评测
  • 测评结果:AC
  • 用时:90ms
  • 内存:200896kb
  • [2024-10-25 05:46:23]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")

////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
  static constexpr unsigned M = M_;
  unsigned x;
  constexpr ModInt() : x(0U) {}
  constexpr ModInt(unsigned x_) : x(x_ % M) {}
  constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
  constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
  constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
  ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
  ModInt pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModInt inv() const {
    unsigned a = M, b = x; int y = 0, z = 1;
    for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
    assert(a == 1U); return ModInt(y);
  }
  ModInt operator+() const { return *this; }
  ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
  ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
  ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
  ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
  ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
  template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
  template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
  template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
  template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModInt &a) const { return (x == a.x); }
  bool operator!=(const ModInt &a) const { return (x != a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////

constexpr unsigned MO = 998244353;
using Mint = ModInt<MO>;


/*
  0^a[0] 1 0^a[1] 1 ... 1 0^a[n]
  a,b -> a+b-3  (a >= 1, b >= 2)
  
  a[i] -= 3
  a,b -> a+b  (a >= -2, b >= -1)
  -3: failed (if n >= 1)
  -2: cannot merge with its left
    cannot merge with its right -1 (if n >= 2)
    merge with 0 ==> still -2
    need +1 to the right
*/

void exper() {
  constexpr int N = 7;
  set<int> dp[N + 1];
  dp[0].insert(0);
  for (int n = 0; n < N; ++n) {
    for (const int p : dp[n]) {
      for (int i = 0; i <= 4*n; ++i) {
        dp[n + 1].insert((p & ((1<<i)-1)) | (2 << i) | (p >> i << (i+4)));
      }
    }
  }
  for (int n = 0; n <= N; ++n) {
    cerr << "|dp[n]| = " << dp[n].size() << endl;
  }
  for (int n = 0; n <= N; ++n) {
    for (const int p : dp[n]) {
      vector<int> is{-1};
      for (int i = 0; i < 4*n; ++i) if (p >> i & 1) is.push_back(i);
      is.push_back(4*n);
      vector<int> as(n + 1);
      for (int j = 0; j <= n; ++j) as[j] = (is[j + 1] - is[j] - 1) - 3;
      for (int i = 0; i < 4*n; ++i) putchar("01"[p >> i & 1]);
      cout << " " << as << endl;
    }
  }
  for (int n = 0; n <= N; ++n) {
    for (int p = 0; p < 1 << (4*n); ++p) if (__builtin_popcount(p) == n) {
      vector<int> is{-1};
      for (int i = 0; i < 4*n; ++i) if (p >> i & 1) is.push_back(i);
      is.push_back(4*n);
      vector<int> as(n + 1);
      for (int j = 0; j <= n; ++j) as[j] = (is[j + 1] - is[j] - 1) - 3;
      
      const bool brt = dp[n].count(p);
      const bool grd = [&]() -> bool {
        for (auto bs = as; ; ) {
          const int bsLen = bs.size();
          if (bsLen == 1) return true;
          if (bs[0] >= -2 && *min_element(bs.begin() + 1, bs.end()) >= -1) return true;
          for (int j = bsLen; --j >= 0; ) if (bs[j] <= -3) {
            return false;
          }
          for (int j = bsLen; --j >= 0; ) if (bs[j] == -2) {
            if (j + 1 == bsLen) return false;
            if (bs[j + 1] <= 0) {
              if (j + 2 == bsLen) return false;
              // merge bs[j+1, j+2]
              bs[j + 1] += bs[j + 2];
              bs.erase(bs.begin() + (j + 2));
              goto found;
            } else {
              // merge bs[j, j+1]
              bs[j] += bs[j + 1];
              bs.erase(bs.begin() + (j + 1));
              goto found;
            }
          }
          assert(false);
         found:{}
        }
      }();
      const bool slv = [&]() -> bool {
        if (n == 0) return true;
        for (int j = 0; j <= n; ++j) if (as[j] <= -3) return false;
        int now = 0;
        int mn = 0;
        for (int j = n; j >= 1; --j) {
          now += as[j];
          if (mn - 2 >= now) return false;
          chmin(mn, now);
        }
        return true;
      }();
      
      if (!(brt == grd && brt == slv)) {
        cerr << "FAIL ";
        for (int i = 0; i < 4*n; ++i) cerr << (p >> i & 1);
        cerr << " " << as << ": " << brt << " " << grd << " " << slv << endl;
        assert(false);
      }
    }
    cerr << "PASSED n = " << n << endl;
  }
}


int N;
char S[510];

// pos, # 1, now - mn, last
Mint dp[510][130][380][2];

int main() {
  // exper();
  
  for (; ~scanf("%d", &N); ) {
    N /= 4;
    scanf("%s", S);
    
    memset(dp, 0, sizeof(dp));
    dp[4*N][0][0][1] = 1;
    for (int i = 4*N; --i >= 0; ) {
      for (int k = 0; k <= N; ++k) for (int d = 0; d <= 3*N; ++d) for (int l = 0; l < 2; ++l) {
        if (S[i] == '?' || S[i] == '0') {
          dp[i][k][d + 1][0] += dp[i + 1][k][d][l];
        }
        if (S[i] == '?' || S[i] == '1') {
          if (d >= 2 && l == 0) {
            dp[i][k + 1][max(d - 3, 0)][1] += dp[i + 1][k][d][l];
          }
        }
      }
    }
    Mint ans = 0;
    for (int d = 0; d <= 3*N; ++d) {
      ans += dp[0][N][d][0];
    }
    printf("%u\n", ans.x);
  }
  return 0;
}

详细

Test #1:

score: 100
Accepted
time: 11ms
memory: 200628kb

input:

8
0??0?100

output:

2

result:

ok "2"

Test #2:

score: 0
Accepted
time: 4ms
memory: 200700kb

input:

4
?10?

output:

1

result:

ok "1"

Test #3:

score: 0
Accepted
time: 0ms
memory: 200692kb

input:

28
???????????0???0??????1???0?

output:

2023

result:

ok "2023"

Test #4:

score: 0
Accepted
time: 7ms
memory: 200684kb

input:

4
????

output:

1

result:

ok "1"

Test #5:

score: 0
Accepted
time: 0ms
memory: 200604kb

input:

8
11111111

output:

0

result:

ok "0"

Test #6:

score: 0
Accepted
time: 89ms
memory: 200888kb

input:

500
????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????...

output:

870731023

result:

ok "870731023"

Test #7:

score: 0
Accepted
time: 87ms
memory: 200888kb

input:

500
???????????????????0???????????????????????????????????????0??????????????????????1???????????????????????????????????????00????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????1???????????????????????????????????...

output:

763641704

result:

ok "763641704"

Test #8:

score: 0
Accepted
time: 28ms
memory: 200896kb

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: 8ms
memory: 200604kb

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: 14ms
memory: 200628kb

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: 25ms
memory: 200608kb

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: 31ms
memory: 200600kb

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: 50ms
memory: 200560kb

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: 7ms
memory: 200628kb

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: 0ms
memory: 200556kb

input:

116
???????????????????????????????01?1???????????????0?00????0?????????????????1?0?????????????????1???????????????00??

output:

717954632

result:

ok "717954632"

Test #16:

score: 0
Accepted
time: 24ms
memory: 200600kb

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: 24ms
memory: 200604kb

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: 85ms
memory: 200612kb

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: 90ms
memory: 200624kb

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: 81ms
memory: 200592kb

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: 12ms
memory: 200604kb

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: 4ms
memory: 200600kb

input:

24
?0?0?????0??????????????

output:

587

result:

ok "587"

Test #23:

score: 0
Accepted
time: 7ms
memory: 200604kb

input:

76
?????0?????????????????0????????????????????????????1???0???????????????????

output:

83575746

result:

ok "83575746"

Test #24:

score: 0
Accepted
time: 7ms
memory: 200896kb

input:

28
00???0???0?0??0???0???0?????

output:

859

result:

ok "859"

Test #25:

score: 0
Accepted
time: 12ms
memory: 200604kb

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: 57ms
memory: 200604kb

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: 74ms
memory: 200556kb

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: 70ms
memory: 200692kb

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: 83ms
memory: 200620kb

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: 66ms
memory: 200600kb

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: 82ms
memory: 200604kb

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: 89ms
memory: 200560kb

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: 71ms
memory: 200604kb

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: 200604kb

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: 78ms
memory: 200696kb

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