結果

問題 No.1654 Binary Compression
ユーザー ei1333333ei1333333
提出日時 2021-08-20 23:54:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 586 ms / 2,000 ms
コード長 8,613 bytes
コンパイル時間 2,988 ms
コンパイル使用メモリ 219,268 KB
実行使用メモリ 77,140 KB
最終ジャッジ日時 2024-04-22 08:56:53
合計ジャッジ時間 13,448 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 353 ms
42,168 KB
testcase_11 AC 330 ms
42,528 KB
testcase_12 AC 360 ms
43,064 KB
testcase_13 AC 332 ms
43,036 KB
testcase_14 AC 316 ms
42,528 KB
testcase_15 AC 327 ms
42,904 KB
testcase_16 AC 517 ms
77,108 KB
testcase_17 AC 507 ms
77,140 KB
testcase_18 AC 360 ms
43,220 KB
testcase_19 AC 366 ms
43,220 KB
testcase_20 AC 313 ms
43,220 KB
testcase_21 AC 308 ms
43,220 KB
testcase_22 AC 313 ms
43,348 KB
testcase_23 AC 23 ms
9,452 KB
testcase_24 AC 22 ms
9,448 KB
testcase_25 AC 21 ms
9,448 KB
testcase_26 AC 21 ms
9,452 KB
testcase_27 AC 405 ms
49,232 KB
testcase_28 AC 437 ms
49,108 KB
testcase_29 AC 22 ms
9,448 KB
testcase_30 AC 22 ms
9,448 KB
testcase_31 AC 21 ms
9,448 KB
testcase_32 AC 22 ms
9,448 KB
testcase_33 AC 449 ms
49,104 KB
testcase_34 AC 443 ms
49,104 KB
testcase_35 AC 22 ms
9,452 KB
testcase_36 AC 22 ms
9,452 KB
testcase_37 AC 21 ms
9,452 KB
testcase_38 AC 22 ms
9,448 KB
testcase_39 AC 22 ms
9,444 KB
testcase_40 AC 21 ms
9,444 KB
testcase_41 AC 21 ms
9,188 KB
testcase_42 AC 16 ms
9,192 KB
testcase_43 AC 15 ms
9,188 KB
testcase_44 AC 19 ms
9,192 KB
testcase_45 AC 20 ms
9,192 KB
testcase_46 AC 12 ms
7,528 KB
testcase_47 AC 2 ms
5,376 KB
testcase_48 AC 2 ms
5,376 KB
testcase_49 AC 586 ms
73,684 KB
testcase_50 AC 464 ms
73,688 KB
testcase_51 AC 170 ms
26,180 KB
testcase_52 AC 163 ms
26,200 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
 
using namespace std;
 
using int64 = long long;
const int mod = 924844033;
// const int mod = 998244353;
 
const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;
 
struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;
 
 
template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 > &p) {
  os << p.first << " " << p.second;
  return os;
}
 
template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}
 
template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}
 
template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}
 
template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
 
template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
 
template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}
 
template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}
 
template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}
 
template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}
 
template< typename F >
struct FixPoint : F {
  FixPoint(F &&f) : F(forward< F >(f)) {}
 
  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};
 
template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}
 
 
template< int mod >
struct ModInt {
  int x;
 
  ModInt() : x(0) {}
 
  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
 
  ModInt &operator+=(const ModInt &p) {
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }
 
  ModInt &operator-=(const ModInt &p) {
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
 
  ModInt &operator*=(const ModInt &p) {
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }
 
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
 
  ModInt operator-() const { return ModInt(-x); }
 
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
 
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
 
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
 
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
 
  bool operator==(const ModInt &p) const { return x == p.x; }
 
  bool operator!=(const ModInt &p) const { return x != p.x; }
 
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
 
  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
 
  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }
 
  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }
 
  static int get_mod() { return mod; }
};
 
using modint = ModInt< mod >;
 
template< typename Monoid >
struct SegmentTree {
  using F = function< Monoid(Monoid, Monoid) >;
 
  int sz;
  vector< Monoid > seg;
 
  const F f;
  const Monoid M1;
 
  SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
    sz = 1;
    while(sz < n) sz <<= 1;
    seg.assign(2 * sz, M1);
  }
 
  void set(int k, const Monoid &x) {
    seg[k + sz] = x;
  }
 
  void build() {
    for(int k = sz - 1; k > 0; k--) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }
 
  void update(int k, const Monoid &x) {
    k += sz;
    seg[k] = x;
    while(k >>= 1) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }
 
  Monoid query(int a, int b) {
    Monoid L = M1, R = M1;
    for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if(a & 1) L = f(L, seg[a++]);
      if(b & 1) R = f(seg[--b], R);
    }
    return f(L, R);
  }
 
  Monoid operator[](const int &k) const {
    return seg[k + sz];
  }
 
  template< typename C >
  int find_subtree(int a, const C &check, Monoid &M, bool type) {
    while(a < sz) {
      Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]);
      if(check(nxt)) a = 2 * a + type;
      else M = nxt, a = 2 * a + 1 - type;
    }
    return a - sz;
  }
 
 
  template< typename C >
  int find_first(int a, const C &check) {
    Monoid L = M1;
    if(a <= 0) {
      if(check(f(L, seg[1]))) return find_subtree(1, check, L, false);
      return -1;
    }
    int b = sz;
    for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if(a & 1) {
        Monoid nxt = f(L, seg[a]);
        if(check(nxt)) return find_subtree(a, check, L, false);
        L = nxt;
        ++a;
      }
    }
    return -1;
  }
 
  template< typename C >
  int find_last(int b, const C &check) {
    Monoid R = M1;
    if(b >= sz) {
      if(check(f(seg[1], R))) return find_subtree(1, check, R, true);
      return -1;
    }
    int a = sz;
    for(b += sz; a < b; a >>= 1, b >>= 1) {
      if(b & 1) {
        Monoid nxt = f(seg[--b], R);
        if(check(nxt)) return find_subtree(b, check, R, true);
        R = nxt;
      }
    }
    return -1;
  }
};
 
int main() {
  string S;
  cin >> S;
  int N = (int) S.size();
 
  {
    bool one = false;
    for(auto &c : S) one |= c == '1';
    if(!one) {
      cout << S.size() << "\n";
      return 0;
    }
  }
 
  // {11111} {00000} {11111} {00000} {11111}
  // 両端の0は適当に処理します
 
  // 1は0個にできません
  // 0をx個連続させる場合は0の塊のうち最も左のやつを使う
  // 1を0≦x個連続させる場合は普通に現在位置から愚直に見ます
  deque< char > Z(S.begin(), S.end());
  int latte = 1, malta = 1;
  while(Z.front() == '0') {
    Z.pop_front();
    ++latte;
  }
  while(Z.back() == '0') {
    Z.pop_back();
    ++malta;
  }
 
 
  vector< pair< int, int > > run;
  for(auto &c : Z) {
    if(run.empty() || run.back().first != c) {
      run.emplace_back(c, 0);
    }
    run.back().second++;
  }
 
  auto dp = make_v< modint >(run.size());
  dp[0] = run[0].second;
  modint ans = 0;
 
 
  auto f = [](int a, int b) { return max(a, b); };
  SegmentTree< int > seg(run.size(), f, -inf);
  for(int i = 1; i < run.size(); i += 2) seg.set(i, run[i].second);
  seg.build();
 
 
  deque< pair< int, int > > lis;
  deque< modint > dps;
  {
    int last = 0;
    for(int i = 1; i < run.size(); i += 2) {
      if(run[i].second - last > 0) {
        lis.emplace_back(i, run[i].second - last);
        last = run[i].second;
        dps.emplace_back(0);
      }
    }
  }
  dps.emplace_back(0);
 
 
  for(int i = 0; i < run.size(); i += 2) {
 
 
    if(i >= 2) {
      // remove i - 1
      if(lis.size() && lis.front().first == i - 1) {
        dp[lis.front().first] += dps[0] * lis.front().second;
        dps[1] += dps[0];
        lis.pop_front();
        dps.pop_front();
        int last = 0, idx = i;
        vector< pair< int, int > > upd;
        while(true) {
          int pos = seg.find_first(idx, [&](int x) { return x > last; });
          if(pos == -1) break;
          if(lis.size() && lis.front().first == pos) break;
          upd.emplace_back(pos, run[pos].second - last);
          last = run[pos].second;
          idx = pos;
        }
        if(lis.size()) {
          dp[lis.front().first] += dps[0] * lis.front().second;
          lis.front().second = run[lis.front().first].second - last;
          dps[1] += dps[0];
          dps[0] = 0;
        }
        for(int j = (int) upd.size() - 1; j >= 0; j--) {
          lis.emplace_front(upd[j]);
          dps.emplace_front(0);
        }
      }
    }
    if(i >= 2) {
      dp[i] += dp[i - 2] / run[i - 2].second * run[i].second;
    }
    if(i >= 1) {
      dp[i] += dp[i - 1] * run[i].second;
      dp[i - 1] = 0;
    }
    dps[0] += dp[i];
  }
  for(int i = 0; i < run.size(); i++) ans += dp[i];
  cout << ans * latte * malta << "\n";
}
0