結果

問題 No.2215 Slide Subset Sum
ユーザー maspymaspy
提出日時 2023-02-11 12:58:07
言語 C++23(draft)
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,455 bytes
コンパイル時間 5,708 ms
コンパイル使用メモリ 321,604 KB
実行使用メモリ 188,008 KB
最終ジャッジ日時 2023-09-22 10:58:05
合計ジャッジ時間 12,223 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 636 ms
39,404 KB
testcase_01 TLE -
testcase_02 TLE -
testcase_03 TLE -
testcase_04 TLE -
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 3 ms
4,384 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 6 ms
4,380 KB
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 TLE -
testcase_27 AC 89 ms
9,556 KB
testcase_28 AC 111 ms
9,696 KB
testcase_29 AC 164 ms
15,200 KB
testcase_30 AC 1,330 ms
81,128 KB
testcase_31 AC 943 ms
61,184 KB
testcase_32 AC 222 ms
18,116 KB
testcase_33 AC 1,827 ms
110,060 KB
testcase_34 AC 1,566 ms
86,560 KB
testcase_35 AC 209 ms
17,240 KB
testcase_36 AC 528 ms
36,596 KB
testcase_37 TLE -
testcase_38 AC 307 ms
24,884 KB
testcase_39 TLE -
testcase_40 WA -
testcase_41 WA -
testcase_42 TLE -
testcase_43 AC 2,466 ms
183,152 KB
testcase_44 AC 2,424 ms
183,216 KB
testcase_45 TLE -
testcase_46 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>

using namespace std;
using namespace atcoder;
using mint = modint998244353;

// モノイド
using poly = vector<mint>;
poly unit = {};

poly op(poly f, poly g) {
  // unit
  if (f.empty()) return g;
  if (g.empty()) return f;
  int K = f.size();
  // より sparse な方を f にする
  int nf = 0, ng = 0;
  for (auto x: f)
    if (x.val() != 0) ++nf;
  for (auto x: g)
    if (x.val() != 0) ++ng;
  if (nf > ng) swap(f, g);
  // f[i] == 0 の場合を枝狩り。f が疎だと高速になる。
  poly h(K);
  for (int i = 0; i < K; ++i) {
    if (f[i].val() == 0) continue;
    for (int j = 0; j < K; ++j) {
      int k = (i + j < K ? i + j : i + j - K);
      h[k] += f[i] * g[j];
    }
  }
  return h;
}

void solve() {
  int N, M, K;
  cin >> N >> M >> K;
  vector<poly> A(N);
  for (int n = 0; n < N; ++n) {
    int a;
    cin >> a;
    A[n].resize(K);
    A[n][0] += 1, A[n][a] += 1;
  }

  using T = tuple<int, int, int>; // (index, left, right)
  int Q = N - M + 1;
  vector<T> query(Q);
  for (int q = 0; q < Q; ++q) { query[q] = {q, q, q + M}; }

  vector<int> ANS(Q);

  auto calc = [&](int L, int M, int R, vector<T>& query) -> void {
    static vector<poly> dp(N + 1);
    // 累積積の計算
    dp[M] = unit;
    for (int i = M; i > L; --i) dp[i - 1] = op(A[i - 1], dp[i]);
    for (int i = M; i < R; ++i) dp[i + 1] = op(dp[i], A[i]);
    // 答の計算
    for (auto [q, a, b]: query) {
      // 0 次だけでよいので、積を計算しきる必要はない
      poly &f = dp[a], &g = dp[b];
      mint ans = 0;
      for (int i = 0; i < K; ++i) {
        int j = (i == 0 ? 0 : K - i);
        ans += f[i] * g[j];
      }
      ans -= 1;
      ANS[q] = ans.val();
    }
  };

  auto dfs = [&](auto& dfs, int L, int R, vector<T>& query) -> void {
    if (R - L <= 1) {
      for (auto [q, a, b]: query) ANS[q] = (A[a][0].val() == 0 ? 1 : 0);
      return;
    }
    int M = (L + R) / 2;
    vector<T> query_L, query_R, other;
    for (auto [q, a, b]: query) {
      if (b <= M) query_L.emplace_back(q, a, b);
      if (M <= a) query_R.emplace_back(q, a, b);
      if (a < M && M < b) other.emplace_back(q, a, b);
    }
    calc(L, M, R, other), dfs(dfs, L, M, query_L), dfs(dfs, M, R, query_R);
  };
  dfs(dfs, 0, N, query);

  // 出力
  for (auto x: ANS) cout << x << '\n';
}

signed main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  solve();
  return 0;
}
0