結果
問題 | No.1712 Read and Pile |
ユーザー | 👑 hos.lyric |
提出日時 | 2021-10-16 00:12:53 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 14,352 bytes |
コンパイル時間 | 3,171 ms |
コンパイル使用メモリ | 153,376 KB |
実行使用メモリ | 60,300 KB |
最終ジャッジ日時 | 2024-09-17 19:05:44 |
合計ジャッジ時間 | 50,102 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
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 | 2 ms
6,940 KB |
testcase_07 | AC | 2 ms
6,940 KB |
testcase_08 | AC | 1,254 ms
32,936 KB |
testcase_09 | AC | 1,871 ms
56,656 KB |
testcase_10 | AC | 1,160 ms
32,868 KB |
testcase_11 | AC | 1,463 ms
56,440 KB |
testcase_12 | AC | 1,013 ms
32,944 KB |
testcase_13 | AC | 1,309 ms
32,416 KB |
testcase_14 | AC | 1,158 ms
32,748 KB |
testcase_15 | AC | 1,506 ms
32,924 KB |
testcase_16 | AC | 1,540 ms
33,308 KB |
testcase_17 | AC | 1,364 ms
33,076 KB |
testcase_18 | AC | 1,420 ms
32,668 KB |
testcase_19 | AC | 1,288 ms
32,044 KB |
testcase_20 | AC | 1,340 ms
32,708 KB |
testcase_21 | AC | 1,645 ms
56,044 KB |
testcase_22 | AC | 1,376 ms
56,156 KB |
testcase_23 | AC | 1,877 ms
60,152 KB |
testcase_24 | AC | 1,711 ms
60,124 KB |
testcase_25 | AC | 1,958 ms
60,152 KB |
testcase_26 | TLE | - |
testcase_27 | TLE | - |
testcase_28 | AC | 1,098 ms
32,636 KB |
testcase_29 | AC | 1,770 ms
58,552 KB |
testcase_30 | AC | 1,346 ms
57,016 KB |
testcase_31 | AC | 707 ms
59,792 KB |
testcase_32 | AC | 626 ms
57,532 KB |
testcase_33 | AC | 1,149 ms
57,656 KB |
testcase_34 | AC | 1,209 ms
56,900 KB |
testcase_35 | TLE | - |
testcase_36 | TLE | - |
testcase_37 | TLE | - |
testcase_38 | AC | 2 ms
6,944 KB |
testcase_39 | AC | 13 ms
6,940 KB |
testcase_40 | AC | 16 ms
6,940 KB |
testcase_41 | AC | 20 ms
6,940 KB |
testcase_42 | AC | 24 ms
6,944 KB |
ソースコード
#pragma GCC optimize ("Ofast") #pragma GCC optimize ("unroll-loops") #pragma GCC target ("avx") #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]); } void update(int a, int b, const S &s) { update(1, 0, n, a, b, s); } T calc(int a, int b) { return calc(1, 0, n, a, b); } private: void update(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; if (a == l && b == r) { ts[u] = opST(s, ts[u], r - l); ss[u] = opSS(s, ss[u]); return; } 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; } update(uL, l, mid, a, b, s); update(uR, mid, r, a, b, s); ts[u] = opTT(ts[uL], ts[uR]); return; } T calc(int u, int l, int r, int a, int b) { if (a < l) a = l; if (b > r) b = r; if (a >= b) return idT; if (a == l && b == r) { 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 = calc(uL, l, mid, a, b); const T resR = calc(uR, mid, r, a, b); 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.calc(app[A[i]], app[A[i]] + 1); Mint tmp = 0; tmp += pw1[ls[i] - ls[app[A[i]] + 1]] * Mint(1 + m); tmp += Mint(N - 1 - m) * (pw1[ls[i] - ls[app[A[i]] + 1]] - pw2[ls[i] - ls[app[A[i]] + 1]]); tmp *= pw0[ls[app[A[i]]] + (ls[N + M] - ls[i + 1])]; ans[0][0] += tmp; } { const auto res1 = seg1.calc(app[A[i]], i); const auto res2 = seg2.calc(app[A[i]], i); 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.calc(0, i); const auto res2 = seg2.calc(0, i); 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.update(app[A[i]], i, 1); seg1.update(app[A[i]], i, 1); seg2.update(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.calc(app[A[i]], min(i, M)); const auto res2 = seg2.calc(app[A[i]], min(i, M)); 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.update(app[A[i]], i, 1); seg2.update(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; }