結果

問題 No.1712 Read and Pile
ユーザー 👑 hos.lyrichos.lyric
提出日時 2021-10-15 23:47:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 13,816 bytes
コンパイル時間 3,735 ms
コンパイル使用メモリ 155,684 KB
実行使用メモリ 60,196 KB
最終ジャッジ日時 2024-09-17 18:53:33
合計ジャッジ時間 47,920 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 1,228 ms
32,756 KB
testcase_09 AC 1,757 ms
56,608 KB
testcase_10 AC 1,090 ms
32,816 KB
testcase_11 AC 1,422 ms
56,368 KB
testcase_12 AC 977 ms
32,880 KB
testcase_13 AC 1,221 ms
32,568 KB
testcase_14 AC 1,107 ms
32,732 KB
testcase_15 AC 1,449 ms
32,928 KB
testcase_16 AC 1,520 ms
33,316 KB
testcase_17 AC 1,282 ms
33,064 KB
testcase_18 AC 1,334 ms
32,576 KB
testcase_19 AC 1,251 ms
32,128 KB
testcase_20 AC 1,269 ms
32,700 KB
testcase_21 AC 1,546 ms
55,968 KB
testcase_22 AC 1,353 ms
56,068 KB
testcase_23 AC 1,849 ms
60,092 KB
testcase_24 AC 1,614 ms
60,196 KB
testcase_25 AC 1,883 ms
60,116 KB
testcase_26 TLE -
testcase_27 TLE -
testcase_28 AC 1,094 ms
32,680 KB
testcase_29 AC 1,722 ms
58,616 KB
testcase_30 AC 1,312 ms
57,152 KB
testcase_31 AC 735 ms
59,692 KB
testcase_32 AC 644 ms
57,568 KB
testcase_33 AC 1,216 ms
57,608 KB
testcase_34 AC 1,264 ms
56,900 KB
testcase_35 AC 1,959 ms
58,412 KB
testcase_36 TLE -
testcase_37 AC 1,935 ms
58,156 KB
testcase_38 AC 2 ms
6,944 KB
testcase_39 AC 12 ms
6,944 KB
testcase_40 AC 13 ms
6,944 KB
testcase_41 AC 17 ms
6,940 KB
testcase_42 AC 23 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")

#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 <map>
#include <numeric>
#include <queue>
#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> 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; }

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


template <class T, class S, class OpTT, class OpST, class OpSS>
struct SegmentTree {
  const OpTT opTT;
  const OpST opST;
  const OpSS opSS;
  const T idT;
  const S idS;

  int n;
  vector<T> ts;
  vector<S> ss;
  SegmentTree(int n_, const OpTT opTT, const OpST opST, const OpSS opSS,
              const T &idT, const S &idS)
      : opTT(opTT), opST(opST), opSS(opSS), idT(idT), idS(idS) {
    for (n = 1; n < n_; n <<= 1) {}
    ts.assign(n << 1, idT);
    ss.assign(n << 1, idS);
  }
  T &at(int a) {
    return ts[n + a];
  }
  void build() {
    for (int a = n; --a; ) ts[a] = opTT(ts[a << 1], ts[a << 1 | 1]);
  }
  T query(int a, int b, const S &s) {
    return query(1, 0, n, a, b, s);
  }

 private:
  T query(int u, int l, int r, int a, int b, const S &s) {
    if (a < l) a = l;
    if (b > r) b = r;
    if (a >= b) return idT;
    if (a == l && b == r) {
      ts[u] = opST(s, ts[u], r - l);
      ss[u] = opSS(s, ss[u]);
      return ts[u];
    }
    const int uL = u << 1, uR = u << 1 | 1;
    const int mid = (l + r) >> 1;
    // speed-up: if (ss[u] != idS)
    if (ss[u])
    {
      ts[uL] = opST(ss[u], ts[uL], mid - l);
      ts[uR] = opST(ss[u], ts[uR], r - mid);
      ss[uL] = opSS(ss[u], ss[uL]);
      ss[uR] = opSS(ss[u], ss[uR]);
      ss[u] = idS;
    }
    const T resL = query(uL, l, mid, a, b, s);
    const T resR = query(uR, mid, r, a, b, s);
    ts[u] = opTT(ts[uL], ts[uR]);
    return opTT(resL, resR);
  }
};

namespace s0 {

using T = Int;
using S = Int;

T opTT(const T &t0, const T &t1) {
  return t0 + t1;
}
T opST(const S &s, const T &t, int sz) {
  return s * sz + t;
}
S opSS(const S &s0, const S &s1) {
  return s0 + s1;
}

constexpr T idT = 0;
constexpr S idS = 0;

using SegTree = SegmentTree<T, S, decltype(&opTT), decltype(&opST), decltype(&opSS)>;
SegTree factory(int n) {
  return SegTree(n, &opTT, &opST, &opSS, idT, idS);
}

}  // namespace s0

namespace segtree2 {

struct T {
  Mint x[2];
  T() : x{} {}
};
using S = Mint;

T opTT(const T &t0, const T &t1) {
  T tt;
  tt.x[0] = t0.x[0] + t1.x[0];
  tt.x[1] = t0.x[1] + t1.x[1];
  return tt;
}
T opST(const S &s, const T &t, int sz) {
  T tt;
  tt.x[0] = t.x[0];
  tt.x[1] = t.x[1] + s * t.x[0];
  return tt;
}
S opSS(const S &s0, const S &s1) {
  return s0 + s1;
}

const T idT;
constexpr S idS = 0;

using SegTree = SegmentTree<T, S, decltype(&opTT), decltype(&opST), decltype(&opSS)>;
SegTree factory(int n) {
  return SegTree(n, &opTT, &opST, &opSS, idT, idS);
}

}  // namespace segtree2

namespace segtree3 {

struct T {
  Mint x[3];
  T() : x{} {}
};
using S = Mint;

T opTT(const T &t0, const T &t1) {
  T tt;
  tt.x[0] = t0.x[0] + t1.x[0];
  tt.x[1] = t0.x[1] + t1.x[1];
  tt.x[2] = t0.x[2] + t1.x[2];
  return tt;
}
T opST(const S &s, const T &t, int sz) {
  T tt;
  tt.x[0] = t.x[0];
  tt.x[1] = t.x[1] + s * t.x[0];
  tt.x[2] = t.x[2] + s * (2 * t.x[1] + s * t.x[0]);
  return tt;
}
S opSS(const S &s0, const S &s1) {
  return s0 + s1;
}

const T idT;
constexpr S idS = 0;

using SegTree = SegmentTree<T, S, decltype(&opTT), decltype(&opST), decltype(&opSS)>;
SegTree factory(int n) {
  return SegTree(n, &opTT, &opST, &opSS, idT, idS);
}

}  // namespace segtree3


int N, M;
vector<int> A;

int main() {
  for (; ~scanf("%d%d", &N, &M); ) {
    A.resize(N + M);
    for (int i = 0; i < N; ++i) {
      A[i] = N - 1 - i;
    }
    for (int i = N; i < N + M; ++i) {
      scanf("%d", &A[i]);
      --A[i];
    }
// cerr<<"A = ";pv(A.begin(),A.end());
    
    if (N == 1) {
      printf("%u\n", Mint(M).x);
      continue;
    }
    if (N == 2) {
      Mint ans2 = M;
      for (int i = N; i < N + M; ++i) {
        for (int a = 0; a < 2; ++a) for (int b = 0; b < 2; ++b) if (a != b) {
          Mint tmp = 1;
          tmp *= ((A[i - 1] == -2) ? Mint(2).inv() : (A[i - 1] == a) ? 1 : 0);
          tmp *= ((A[i] == -2) ? Mint(2).inv() : (A[i] == b) ? 1 : 0);
          ans2 += tmp;
        }
      }
      printf("%u\n", ans2.x);
      continue;
    }
    
    vector<int> ls(N + M + 1, 0);
    for (int i = 0; i < N + M; ++i) {
      ls[i + 1] = ls[i] + ((A[i] == -2) ? 1 : 0);
    }
// cerr<<"ls = ";pv(ls.begin(),ls.end());
    
    Mint ans[2][2] = {};
    
    vector<Mint> pw0(N + M + 1), pw1(N + M + 1), pw2(N + M + 1), invPw1(N + M + 1), invPw2(N + M + 1);
    pw0[0] = pw1[0] = pw2[0] = invPw1[0] = invPw2[0] = 1;
    pw0[1] = Mint(N);
    pw1[1] = Mint(N - 1);
    pw2[1] = Mint(N - 2);
    invPw1[1] = Mint(N - 1).inv();
    invPw2[1] = Mint(N - 2).inv();
    for (int i = 2; i <= N + M; ++i) {
      pw0[i] = pw0[i - 1] * pw0[1];
      pw1[i] = pw1[i - 1] * pw1[1];
      pw2[i] = pw2[i - 1] * pw2[1];
      invPw1[i] = invPw1[i - 1] * invPw1[1];
      invPw2[i] = invPw2[i - 1] * invPw2[1];
    }
    {
      vector<int> app(N, 0);
      auto seg0 = s0::factory(N + M);
      auto seg1 = segtree2::factory(N + M);
      auto seg2 = segtree3::factory(N + M);
      for (int i = 0; i < N + M; ++i) {
        if (A[i] == -2) {
          seg1.at(i).x[0] = pw0[ls[i]] * invPw1[ls[i + 1]];
          seg2.at(i).x[0] = pw0[ls[i]] * invPw2[ls[i + 1]];
        }
      }
      seg1.build();
      seg2.build();
      for (int i = 0; i < N + M; ++i) {
        // get
        if (i >= N) {
          if (A[i] != -2) {
            {
              const int m = seg0.query(app[A[i]], app[A[i]] + 1, s0::idS);
              Mint tmp = 0;
              tmp += Mint(N - 1).pow(ls[i] - ls[app[A[i]] + 1]) * Mint(1 + m);
              tmp += Mint(N - 1 - m) * (Mint(N - 1).pow(ls[i] - ls[app[A[i]] + 1]) - Mint(N - 2).pow(ls[i] - ls[app[A[i]] + 1]));
              tmp *= Mint(N).pow(ls[app[A[i]]] + (ls[N + M] - ls[i + 1]));
              ans[0][0] += tmp;
            }
            {
              const auto res1 = seg1.query(app[A[i]], i, segtree2::idS);
              const auto res2 = seg2.query(app[A[i]], i, segtree3::idS);
              Mint tmp = 0;
              tmp += pw1[ls[i]] * (res1.x[0] + res1.x[1]);
              tmp += pw1[ls[i]] * (Mint(N - 1) * res1.x[0] - res1.x[1])
                   - pw2[ls[i]] * (Mint(N - 1) * res2.x[0] - res2.x[1]);
              tmp *= pw0[ls[N + M] - ls[i + 1]];
              ans[1][0] += tmp;
            }
          } else {
            {
              const auto res1 = seg1.query(0, i, segtree2::idS);
              const auto res2 = seg2.query(0, i, segtree3::idS);
              Mint tmp = 0;
              // tmp += pw1[ls[i]] * (Mint(N) * res1.x[0] + Mint(N - 1) * res1.x[1] - res1.x[2]);
              // tmp += pw1[ls[i]] * (Mint(N) * Mint(N - 1) * res1.x[0] - Mint(2 * N - 1) * res1.x[1] + res1.x[2])
              tmp += pw1[ls[i]] * (Mint(N) * res1.x[0] + Mint(N - 1) * res1.x[1]);
              tmp += pw1[ls[i]] * (Mint(N) * Mint(N - 1) * res1.x[0] - Mint(2 * N - 1) * res1.x[1])
                   - pw2[ls[i]] * (Mint(N) * Mint(N - 1) * res2.x[0] - Mint(2 * N - 1) * res2.x[1] + res2.x[2]);
              tmp *= pw0[ls[N + M] - ls[i + 1]];
              ans[1][1] += tmp;
            }
          }
        }
        // add
        if (A[i] != -2) {
          seg0.query(app[A[i]], i, 1);
          seg1.query(app[A[i]], i, 1);
          seg2.query(app[A[i]], i, 1);
          app[A[i]] = i;
        }
      }
    }
    
    {
      reverse(A.begin(), A.end());
      for (int i = 0; i < N + M; ++i) {
        ls[i + 1] = ls[i] + ((A[i] == -2) ? 1 : 0);
      }
// cerr<<"A = ";pv(A.begin(),A.end());
// cerr<<"ls = ";pv(ls.begin(),ls.end());
      vector<int> app(N, 0);
      auto seg1 = segtree2::factory(N + M);
      auto seg2 = segtree2::factory(N + M);
      for (int i = 0; i < N + M; ++i) {
        if (A[i] == -2) {
          seg1.at(i).x[0] = pw0[ls[i]] * invPw1[ls[i + 1]];
          seg2.at(i).x[0] = pw0[ls[i]] * invPw2[ls[i + 1]];
        }
      }
      seg1.build();
      seg2.build();
      for (int i = 0; i < M + N; ++i) {
        // get
        {
          if (A[i] != -2) {
            {}
            {
              const auto res1 = seg1.query(app[A[i]], min(i, M), segtree2::idS);
              const auto res2 = seg2.query(app[A[i]], min(i, M), segtree2::idS);
              Mint tmp = 0;
              tmp += pw1[ls[i]] * (res1.x[0] + res1.x[1]);
              tmp += pw1[ls[i]] * (Mint(N - 1) * res1.x[0] - res1.x[1])
                   - pw2[ls[i]] * (Mint(N - 1) * res2.x[0] - res2.x[1]);
              tmp *= pw0[ls[N + M] - ls[i + 1]];
              ans[0][1] += tmp;
            }
          } else {
            {}
          }
        }
        // add
        if (A[i] != -2) {
          seg1.query(app[A[i]], i, 1);
          seg2.query(app[A[i]], i, 1);
          app[A[i]] = i;
        }
      }
      reverse(A.begin(), A.end());
      for (int i = 0; i < N + M; ++i) {
        ls[i + 1] = ls[i] + ((A[i] == -2) ? 1 : 0);
      }
    }
    
    printf("%u\n", ((ans[0][0] + ans[0][1] + ans[1][0] + ans[1][1]) / Mint(N).pow(ls[N + M])).x);
    
#ifdef LOCAL
    Mint brt[2][2] = {};
    for (int i = 0; i < N + M; ++i) for (int j = N; j < N + M; ++j) {
      if (i < j) {
        set<int> as(A.begin() + i + 1, A.begin() + j);
        as.erase(-2);
        const int m = as.size();
        Mint tmp = 0;
        tmp += Mint(N - 1).pow(ls[j] - ls[i + 1]) * Mint(1 + m);
        tmp += Mint(N - 1 - m) * (Mint(N - 1).pow(ls[j] - ls[i + 1]) - Mint(N - 2).pow(ls[j] - ls[i + 1]));
        tmp *= Mint(N).pow(ls[i] + (ls[N + M] - ls[j + 1]));
        if (A[i] != -2) {
          if (A[j] != -2) {
            if (A[i] == A[j] && as.find(A[i]) == as.end()) {
              brt[0][0] += tmp;
            }
          } else {
            if (as.find(A[i]) == as.end()) {
              brt[0][1] += tmp;
            }
          }
        } else {
          if (A[j] != -2) {
            if (as.find(A[j]) == as.end()) {
              brt[1][0] += tmp;
            }
          } else {
            brt[1][1] += Mint(N - m) * tmp;
          }
        }
      }
    }
    cerr << "brt = " << ((brt[0][0] + brt[0][1] + brt[1][0] + brt[1][1]) / Mint(N).pow(ls[N + M])) << "; "
         << brt[0][0] << " " << brt[0][1] << " " << brt[1][0] << " " << brt[1][1] << endl;
    cerr << "ans = " << ((ans[0][0] + ans[0][1] + ans[1][0] + ans[1][1]) / Mint(N).pow(ls[N + M])) << "; "
         << ans[0][0] << " " << ans[0][1] << " " << ans[1][0] << " " << ans[1][1] << endl;
#endif
  }
  return 0;
}
0