QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#744677#9614. 分治hos_lyric100 ✓1728ms17716kbC++149.8kb2024-11-13 22:48:372024-11-13 22:48:37

Judging History

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

  • [2024-11-13 22:48:37]
  • 评测
  • 测评结果:100
  • 用时:1728ms
  • 内存:17716kb
  • [2024-11-13 22:48:37]
  • 提交

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>;

constexpr int LIM_INV = 400'010;
Mint inv[LIM_INV], fac[LIM_INV], invFac[LIM_INV];
Mint two[LIM_INV];

void prepare() {
  inv[1] = 1;
  for (int i = 2; i < LIM_INV; ++i) {
    inv[i] = -((Mint::M / i) * inv[Mint::M % i]);
  }
  fac[0] = invFac[0] = 1;
  for (int i = 1; i < LIM_INV; ++i) {
    fac[i] = fac[i - 1] * i;
    invFac[i] = invFac[i - 1] * inv[i];
  }
  two[0] = 1;
  for (int i = 1; i < LIM_INV; ++i) {
    two[i] = two[i - 1] * 2;
  }
}
Mint binom(Int n, Int k) {
  if (n < 0) {
    if (k >= 0) {
      return ((k & 1) ? -1 : +1) * binom(-n + k - 1, k);
    } else if (n - k >= 0) {
      return (((n - k) & 1) ? -1 : +1) * binom(-k - 1, n - k);
    } else {
      return 0;
    }
  } else {
    if (0 <= k && k <= n) {
      assert(n < LIM_INV);
      return fac[n] * invFac[k] * invFac[n - k];
    } else {
      return 0;
    }
  }
}

////////////////////////////////////////////////////////////////////////////////


/*
  0: ceil child
  1: floor child
  
  ex. N = 11 = 1011(2):
    0000
    0001
    001
    0100
    0101
    011
    1000
    1001
    101
    110
    111
  reversed:
    0000
    0001
    0010
     100
     101
     110
     111
    1000
    1001
    1010
  
  2^(L-1) <= N < 2^L
  reversed:
    [0, N - 2^(L-1))       in L bits
    [N - 2^(L-1), 2^(L-1)) in (L-1) bits
    [2^(L-1), N)           in L bits
  
  ans = \sum (1 + (longest contiguous 0))
      = N + \sum[1<=k<=L] \sum [longest >= k]
      = N + \sum[1<=k<=L] (N - \sum [longest < k])
  
  [N - 2^(L-1), 2^(L-1)) and [2^(L-1), N): same as {0,1}^(L-1)
  [0, N - 2^(L-1)):
    ex. N = 22 = 10110(2)
      00****
      0100**
      01010*
  
  f[k][n] := \sum[s \in {0,1}^n] [contiguous 0 in s < k]
           = [x^n] (1 - x^k) / (1 - 2 x + x^(k+1))
           = [x^n] (1 - x^k) \sum[d>=0] (-1)^d x^((k+1)d) / (1-2x)^(d+1)
               B[d][n] := [n>=0] 2^n binom(n+d, d)
           = \sum[d>=0] (-1)^d (B[d][n-(k+1)d] - B[d][n-(k+1)d-k])
           = \sum[d>=0] (-1)^d (B[d][n-(k+1)d] - B[d][n+1-(k+1)(d+1)])
  
  d = 0 && first term: 2^n
  other terms:
    becomes 0 for k > L
    \sum[k1<=k<=L] is written by:
      g0[d][n] := \sum[k>=0] B[d][n-kd]     = B[d][n] + g[d][n-d]
      g1[d][n] := \sum[k>=0] B[d][n-k(d+1)] = B[d][n] + g[d][n-(d+1)]
*/

int L;
char S[200'010];

Mint slow() {
  vector<vector<Mint>> f(L + 1, vector<Mint>(L + 1, 0));
  for (int k = 1; k <= L; ++k) {
    f[k][0] = 1;
    for (int n = 1; n <= k; ++n) f[k][n] = f[k][n - 1] * 2;
    f[k][k] -= 1;
    for (int n = k + 1; n <= L; ++n) f[k][n] = f[k][n - 1] * 2 - f[k][n - (k + 1)];
  }
  Mint all = 0;
  for (int i = 0; i < L; ++i) (all *= 2) += (S[i] - '0');
  Mint ans = (L + 1) * all;
  for (int k = 1; k <= L; ++k) ans -= f[k][L - 1];
  int mx = 1, now = 1;
  for (int i = 1; i < L; ++i) {
    ++now;
    if (S[i] == '0') {
      chmax(mx, now);
    } else {
      for (int k = mx + 1; k <= L; ++k) {
        // \sum[s \in 0^now {0,1}^(L-i-1)] [longest < k]
        // {0,1}^now {0,1}^(L-i-1)
        ans -= f[k][now + (L - i - 1)];
        for (int j = 0; j < now; ++j) if (j < k) {
          // exclude those starting with 0^j 1
          ans += f[k][(now - j - 1) + (L - i - 1)];
        }
      }
      now = 0;
    }
  }
  return ans;
}

Mint fast() {
  // ((k1, len), sig): ans += sig \sum[k1<=k<=L] f[k][len]
  vector<pair<pair<int, int>, int>> es;
  es.emplace_back(make_pair(1, L - 1), -1);
  {
    int mx = 1, now = 1;
    for (int i = 1; i < L; ++i) {
      ++now;
      if (S[i] == '0') {
        chmax(mx, now);
      } else {
        // note that \sum now <= L
        es.emplace_back(make_pair(mx + 1, now + (L - i - 1)), -1);
        for (int j = 0; j < now; ++j) {
          es.emplace_back(make_pair(max(mx, j) + 1, (now - j - 1) + (L - i - 1)), +1);
        }
        now = 0;
      }
    }
  }
// cerr<<"es = "<<es<<endl;
  
  Mint all = 0;
  for (int i = 0; i < L; ++i) (all *= 2) += (S[i] - '0');
  Mint ans = (L + 1) * all;
  
  const int K = min(max((int)sqrt(1.5 * L), 0), L);
cerr<<"L = "<<L<<", K = "<<K<<endl;
cerr<<COLOR("95")<<__LINE__<<": "<<clock()<<COLOR()<<endl;
  // k <= K
  vector<Mint> fs(L + 1);
  for (int k = 1; k <= K; ++k) {
    for (int n = 0; n <= k; ++n) fs[n] = two[n];
    fs[k] -= 1;
    for (int n = k+1; n <= L; ++n) fs[n] = 2 * fs[n - 1] - fs[n - (k+1)];
    Mint sum = 0;
    for (const auto &e : es) {
      const int k1 = e.first.first;
      const int len = e.first.second;
      if (k >= k1) sum += e.second * fs[len];
    }
    ans += sum;
  }
cerr<<COLOR("95")<<__LINE__<<": "<<clock()<<COLOR()<<endl;
  // k > K && d = 0 && first term
  {
    Mint sum = 0;
    for (const auto &e : es) {
      const int k1 = max(e.first.first, K + 1);
      const int len = e.first.second;
      sum += e.second * ((L - k1 + 1) * two[len]);
    }
    ans += sum;
  }
cerr<<COLOR("95")<<__LINE__<<": "<<clock()<<COLOR()<<endl;
  // k > K && other terms
  vector<Mint> gs0(L + 1), gs1(L + 1);
  for (int d = 0; L - (K+1) * d >= 0; ++d) {
    for (int n = 0; n <= L; ++n) gs0[n] = gs1[n] = two[n] * binom(n + d, d);
    for (int n = d  ; n <= L; ++n) gs0[n] += gs0[n - d];
    for (int n = d+1; n <= L; ++n) gs1[n] += gs1[n - (d+1)];
    Mint sum = 0;
    for (const auto &e : es) {
      const int k1 = max(e.first.first, K + 1);
      const int len = e.first.second;
      const int n0 = len - (k1+1) * d;
      const int n1 = len+1 - (k1+1) * (d+1);
      if (d >= 1 && n0 >= 0) sum += e.second * gs0[n0];
      if (n1 >= 0) sum -= e.second * gs1[n1];
    }
    ans += (d&1?-1:+1) * sum;
  }
cerr<<COLOR("95")<<__LINE__<<": "<<clock()<<COLOR()<<endl;
  return ans;
}

int main() {
  prepare();
  
  for (; ~scanf("%s", S); ) {
    L = strlen(S);
    
    const Mint ans = fast();
    printf("%u\n", ans.x);
#ifdef LOCAL
if(L<=2000){
 const Mint slw=slow();
 if(slw!=ans){
  cerr<<"S = "<<S<<endl;
  cerr<<"slw = "<<slw<<endl;
  cerr<<"ans = "<<ans<<endl;
  assert(false);
 }
}
#endif
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 6ms
memory: 10140kb

input:

110

output:

15

result:

ok 1 number(s): "15"

Test #2:

score: 10
Accepted
time: 6ms
memory: 10272kb

input:

101

output:

12

result:

ok 1 number(s): "12"

Subtask #2:

score: 10
Accepted

Dependency #1:

100%
Accepted

Test #3:

score: 10
Accepted
time: 6ms
memory: 10124kb

input:

111110

output:

198

result:

ok 1 number(s): "198"

Test #4:

score: 10
Accepted
time: 6ms
memory: 10120kb

input:

1001001

output:

253

result:

ok 1 number(s): "253"

Subtask #3:

score: 20
Accepted

Dependency #2:

100%
Accepted

Test #5:

score: 20
Accepted
time: 6ms
memory: 10212kb

input:

10100011000100111

output:

386882

result:

ok 1 number(s): "386882"

Test #6:

score: 20
Accepted
time: 6ms
memory: 10092kb

input:

111010011111010110

output:

1107742

result:

ok 1 number(s): "1107742"

Subtask #4:

score: 5
Accepted

Test #7:

score: 5
Accepted
time: 6ms
memory: 10132kb

input:

100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

output:

412796008

result:

ok 1 number(s): "412796008"

Test #8:

score: 5
Accepted
time: 6ms
memory: 10084kb

input:

100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

output:

818656648

result:

ok 1 number(s): "818656648"

Subtask #5:

score: 5
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

100%
Accepted

Test #9:

score: 5
Accepted
time: 6ms
memory: 10204kb

input:

10000000100000010010011110111101101110000000000001100000011000111111010011010101010000101001110110010001100110000110111101000101001111101111001010001001011101011111010000100010111100110000001101111

output:

703266161

result:

ok 1 number(s): "703266161"

Test #10:

score: 5
Accepted
time: 6ms
memory: 10052kb

input:

110100000100001000101000010010101000110111101010110000101001001100100111000011100101110110010000001111010011101001111110110010001110011101001111010101100100010011101010101111111111010110001100100110

output:

330527406

result:

ok 1 number(s): "330527406"

Subtask #6:

score: 5
Accepted

Dependency #4:

100%
Accepted

Test #11:

score: 5
Accepted
time: 7ms
memory: 10264kb

input:

100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...

output:

340672883

result:

ok 1 number(s): "340672883"

Test #12:

score: 5
Accepted
time: 4ms
memory: 10280kb

input:

100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...

output:

555946758

result:

ok 1 number(s): "555946758"

Subtask #7:

score: 10
Accepted

Dependency #5:

100%
Accepted

Dependency #6:

100%
Accepted

Test #13:

score: 10
Accepted
time: 4ms
memory: 10164kb

input:

110011100110101000000110101010111111001101101011010110100100110010111110110110000111011001110000101111110111011111000110001011011011101100001100100011010010111111010110010000101001001000100001100100000001000111110100000101001011100001100011011110110101101111110011100111001010001010001111001110111100...

output:

324123594

result:

ok 1 number(s): "324123594"

Test #14:

score: 10
Accepted
time: 4ms
memory: 10276kb

input:

110100110100110110001011100000011010000010000101100100001101100100110000101000111001111100001110001001101010110010111101000100111010001011001110101010001101111010000011000010110011000011100101110100000001011100111000101111010100001101011010100101110000010001101001000100111001101101110000101101011011...

output:

209285599

result:

ok 1 number(s): "209285599"

Subtask #8:

score: 10
Accepted

Dependency #6:

100%
Accepted

Test #15:

score: 10
Accepted
time: 406ms
memory: 11136kb

input:

100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...

output:

468567454

result:

ok 1 number(s): "468567454"

Test #16:

score: 10
Accepted
time: 877ms
memory: 12116kb

input:

100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...

output:

12752860

result:

ok 1 number(s): "12752860"

Subtask #9:

score: 25
Accepted

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

100%
Accepted

Dependency #5:

100%
Accepted

Dependency #6:

100%
Accepted

Dependency #7:

100%
Accepted

Dependency #8:

100%
Accepted

Test #17:

score: 25
Accepted
time: 1728ms
memory: 16728kb

input:

101100010100101011010110001111101101001010000111001111000100110110010111101100011011011111010110000000011110000010100110111110110001101001101101001110101110011000010100100101000011000010000101011001011011000000100111011110100010000100001101011110100101110000100011000101100000111111100110000111010000...

output:

711712397

result:

ok 1 number(s): "711712397"

Test #18:

score: 25
Accepted
time: 1719ms
memory: 16924kb

input:

110101110100100010101100000110000110101101111100110011100111111110000101111001101001111000110111100111110111010001000010111111110000001001011110101110001011010010010011101000110110000110110101000100111000100110101111011101111101000010000101001001000010011011000011001100111111011000111000010000100111...

output:

171668334

result:

ok 1 number(s): "171668334"

Test #19:

score: 25
Accepted
time: 1166ms
memory: 14528kb

input:

100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...

output:

397846555

result:

ok 1 number(s): "397846555"

Test #20:

score: 25
Accepted
time: 1257ms
memory: 17716kb

input:

100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...

output:

592103795

result:

ok 1 number(s): "592103795"

Extra Test:

score: 0
Extra Test Passed