結果

問題 No.2720 Sum of Subarray of Subsequence of...
ユーザー KudeKude
提出日時 2024-04-05 23:33:43
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 319 ms / 4,000 ms
コード長 2,546 bytes
コンパイル時間 4,203 ms
コンパイル使用メモリ 293,304 KB
実行使用メモリ 17,864 KB
最終ジャッジ日時 2024-04-05 23:33:54
合計ジャッジ時間 8,071 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 2 ms
6,548 KB
testcase_04 AC 2 ms
6,548 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 2 ms
6,548 KB
testcase_07 AC 2 ms
6,548 KB
testcase_08 AC 2 ms
6,548 KB
testcase_09 AC 2 ms
6,548 KB
testcase_10 AC 2 ms
6,548 KB
testcase_11 AC 2 ms
6,548 KB
testcase_12 AC 2 ms
6,548 KB
testcase_13 AC 2 ms
6,548 KB
testcase_14 AC 41 ms
6,548 KB
testcase_15 AC 66 ms
7,336 KB
testcase_16 AC 85 ms
8,448 KB
testcase_17 AC 107 ms
9,224 KB
testcase_18 AC 142 ms
10,708 KB
testcase_19 AC 172 ms
12,604 KB
testcase_20 AC 194 ms
13,240 KB
testcase_21 AC 231 ms
14,252 KB
testcase_22 AC 250 ms
15,656 KB
testcase_23 AC 278 ms
16,752 KB
testcase_24 AC 313 ms
17,860 KB
testcase_25 AC 3 ms
6,548 KB
testcase_26 AC 2 ms
6,548 KB
testcase_27 AC 117 ms
10,572 KB
testcase_28 AC 41 ms
6,548 KB
testcase_29 AC 41 ms
6,548 KB
testcase_30 AC 319 ms
17,864 KB
testcase_31 AC 296 ms
17,388 KB
testcase_32 AC 167 ms
12,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using mint = modint998244353;



vector<mint> fps_inverse(vector<mint> f, int precision) {
  int n = f.size();
  assert(n >= 1 && f[0] != 0);
  int len = 1;
  const int z = bit_ceil(1U * precision);
  vector<mint> g{f[0].inv()};
  const mint inv4 = mint(4).inv();
  mint inv4k = 1;
  while (len < z) {
    int nlen = 2 * len;
    vector<mint> ft(f.begin(), f.begin() + min(n, nlen));
    ft.resize(nlen);
    butterfly(ft);
    vector<mint> gt = g;
    gt.resize(nlen);
    internal::butterfly(gt);
    for (int i = 0; i < nlen; i++) ft[i] *= gt[i];
    internal::butterfly_inv(ft);
    ft.erase(ft.begin(), ft.begin() + len);
    ft.resize(nlen);
    internal::butterfly(ft);
    for (int i = 0; i < nlen; i++) ft[i] *= gt[i];
    internal::butterfly_inv(ft);
    inv4k *= inv4;
    mint c = -inv4k;
    for (int i = 0; i < len; i++) g.emplace_back(c * ft[i]);
    len = nlen;
  }
  g.resize(precision);
  return g;
}



} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  VI a(n);
  rep(i, n) cin >> a[i];
  string s;
  cin >> s;
  VI cnt(m + 1);
  rrep(i, m) cnt[i] = cnt[i+1] + (s[i] == 's');
  using VM = vector<mint>;
  deque<VM> q1{{1}}, q2{{1}};
  q2.emplace_back(VM{1, -(cnt[0] + 1)});
  rep(i, m) if (s[i] == 'a') {
    int si = cnt[i+1] + 1;
    q1.emplace_back(VM{1, -si + 1});
    q2.emplace_back(VM{1, -si});
  }
  rep(_, 2) {
    while (q1.size() >= 2) {
      auto f = move(q1.front()); q1.pop_front();
      auto g = move(q1.front()); q1.pop_front();
      q1.emplace_back(convolution(f, g));
    }
    swap(q1, q2);
  }
  auto f = move(q1.front()); q1.pop_front();
  auto g = move(q2.front()); q2.pop_front();
  f = convolution(fps_inverse(g, n + 1), f);
  mint ans;
  rep(i, n) {
    ans += a[i] * f[i] * f[n - (i + 1)];
  }
  cout << ans.val() << '\n';
}
0