結果

問題 No.2388 At Least K-Characters
ユーザー risujirohrisujiroh
提出日時 2023-07-21 23:52:02
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,596 ms / 4,000 ms
コード長 5,832 bytes
コンパイル時間 3,019 ms
コンパイル使用メモリ 261,944 KB
実行使用メモリ 11,728 KB
最終ジャッジ日時 2024-07-05 04:13:20
合計ジャッジ時間 57,477 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
10,876 KB
testcase_01 AC 14 ms
10,980 KB
testcase_02 AC 15 ms
10,944 KB
testcase_03 AC 14 ms
10,908 KB
testcase_04 AC 14 ms
10,876 KB
testcase_05 AC 15 ms
10,856 KB
testcase_06 AC 16 ms
11,072 KB
testcase_07 AC 15 ms
11,000 KB
testcase_08 AC 14 ms
11,052 KB
testcase_09 AC 15 ms
10,908 KB
testcase_10 AC 14 ms
11,040 KB
testcase_11 AC 14 ms
10,968 KB
testcase_12 AC 14 ms
10,988 KB
testcase_13 AC 23 ms
10,972 KB
testcase_14 AC 21 ms
11,020 KB
testcase_15 AC 20 ms
10,952 KB
testcase_16 AC 2,447 ms
11,692 KB
testcase_17 AC 2,176 ms
11,640 KB
testcase_18 AC 2,501 ms
11,624 KB
testcase_19 AC 2,532 ms
11,620 KB
testcase_20 AC 2,506 ms
11,520 KB
testcase_21 AC 2,498 ms
11,600 KB
testcase_22 AC 2,508 ms
11,616 KB
testcase_23 AC 2,494 ms
11,644 KB
testcase_24 AC 2,520 ms
11,672 KB
testcase_25 AC 2,519 ms
11,580 KB
testcase_26 AC 2,527 ms
11,588 KB
testcase_27 AC 2,531 ms
11,728 KB
testcase_28 AC 2,527 ms
11,648 KB
testcase_29 AC 2,512 ms
11,616 KB
testcase_30 AC 2,504 ms
11,692 KB
testcase_31 AC 2,501 ms
11,580 KB
testcase_32 AC 2,514 ms
11,652 KB
testcase_33 AC 2,492 ms
11,584 KB
testcase_34 AC 2,596 ms
11,604 KB
testcase_35 AC 2,569 ms
11,552 KB
testcase_36 AC 2,483 ms
11,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#if __INCLUDE_LEVEL__ == 0

#include <bits/stdc++.h>

using namespace std;

#include __BASE_FILE__

namespace {

using Fp = atcoder::modint998244353;

void solve() {
  int n, m, k;
  cin >> tie(n, m, k);
  string s;
  cin >> s;
  Comb<Fp> comb(1e6);
  array<int, 26> occ{};
  int x = 0;
  Fp ans = 0;
  array<array<Fp, 27>, 27> coef{};
  for (int x : rep1(0, 26)) {
    for (int z : rep1(0, 26)) {
      for (int y : rep1(max(z, k), 26)) {
        coef[x][z] += comb.binom(26 - x, y - x) * comb.binom(x, z - (y - x));
      }
    }
  }
  for (int p : rep(n)) {
    ans += k <= x;
    array<Fp, 27> f;
    for (int i : rep(f.size())) {
      if (i == 0) {
        f[i] = 1;
      } else if (i == 1) {
        f[i] = m - p;
      } else {
        f[i] = (Fp(i).pow(m - p) - 1) * comb.recip(i - 1);
      }
    }
    array<Fp, 27> g{};
    for (int i : rep(g.size())) {
      for (int j : rep1(0, i)) {
        g[i] += ((i - j) & 1 ? -1 : 1) * comb.binom(i, j) * f[j];
      }
    }
    for (char c : rep('a', s[p])) {
      x += occ[c - 'a']++ == 0;
      for (int z : rep1(0, 26)) {
        ans += g[z] * coef[x][z];
      }
      x -= --occ[c - 'a'] == 0;
    }
    x += occ[s[p] - 'a']++ == 0;
  }
  print(ans);
}

}  // namespace

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  solve();
}

#else  // __INCLUDE_LEVEL__

#undef assert
#define assert(expr) (expr) || (__builtin_unreachable(), 0)

#include <atcoder/modint>

template <class T>
vector<T> make_vector_for_overwrite(int n) {
  static_assert(is_trivially_destructible_v<T>);
  vector<T> v;
  v.reserve(n);
  auto p = (T**)&v;
  p[1] = p[2];
  return v;
}

template <class T>
class Comb {
 public:
  Comb() = default;

  explicit Comb(int max_n)
      : fact_(make_vector_for_overwrite<T>(max_n + 1)),
        recip_fact_(make_vector_for_overwrite<T>(max_n + 1)) {
    fact_[0] = 1;
    for (int n = 1; n <= max_n; ++n) {
      fact_[n] = fact_[n - 1] * n;
    }
    recip_fact_[max_n] = 1 / fact_[max_n];
    for (int n = max_n; 1 <= n; --n) {
      recip_fact_[n - 1] = n * recip_fact_[n];
    }
  }

  T recip(int n) const {
    assert(n);
    return n < 0 ? -recip(-n) : recip_fact_[n] * fact_[n - 1];
  }

  T fact(int n) const {
    assert(0 <= n);
    return fact_[n];
  }

  T recip_fact(int n) const { return n < 0 ? 0 : recip_fact_[n]; }

  T falling_fact(int n, int k) const {
    assert(0 <= n || n < k);
    if (n < 0) {
      T t = falling_fact(k - n - 1, k);
      return k & 1 ? -t : t;
    }
    return n < k ? 0 : recip_fact(n - k) * fact(n);
  }

  T recip_falling_fact(int n, int k) const {
    assert(n < 0 || k <= n);
    return falling_fact(n - k, -k);
  }

  T rising_fact(int n, int k) const {
    assert(n <= 0 || 0 < n + k);
    return falling_fact(n + k - 1, k);
  }

  T recip_rising_fact(int n, int k) const {
    assert(0 < n || n + k <= 0);
    return falling_fact(n - 1, -k);
  }

  T binom(int n, int k) const {
    if ((n < 0) ^ (k < 0) ^ (n < k)) {
      return 0;
    }
    if (n < 0 && k < 0) {
      k = n - k;
    }
    return recip_fact(k) * falling_fact(n, k);
  }

  T recip_binom(int n, int k) const {
    assert((0 <= n) ^ (0 <= k) ^ (k <= n));
    k = max(k, n - k);
    return recip_falling_fact(n, k) * fact(k);
  }

  T multiset(int n, int k) const { return binom(n + k - 1, k); }

  T recip_multiset(int n, int k) const {
    assert((0 < n) ^ (0 <= k) ^ (0 < n + k));
    return recip_binom(n + k - 1, k);
  }

 private:
  vector<T> fact_;
  vector<T> recip_fact_;
};

template <class T, class U = T>
bool chmin(T& x, U&& y) {
  return y < x && (x = forward<U>(y), true);
}

template <class T, class U = T>
bool chmax(T& x, U&& y) {
  return x < y && (x = forward<U>(y), true);
}

namespace std {

template <class T1, class T2>
istream& operator>>(istream& is, pair<T1, T2>& p) {
  return is >> p.first >> p.second;
}

template <class... Ts>
istream& operator>>(istream& is, tuple<Ts...>& t) {
  return apply([&is](auto&... xs) -> istream& { return (is >> ... >> xs); }, t);
}

template <class... Ts>
istream& operator>>(istream& is, tuple<Ts&...>&& t) {
  return is >> t;
}

template <class R, enable_if_t<!is_convertible_v<R, string>>* = nullptr>
auto operator>>(istream& is, R&& r) -> decltype(is >> *begin(r)) {
  for (auto&& e : r) {
    is >> e;
  }
  return is;
}

template <class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) {
  return os << p.first << ' ' << p.second;
}

template <class... Ts>
ostream& operator<<(ostream& os, const tuple<Ts...>& t) {
  auto f = [&os](const auto&... xs) -> ostream& {
    [[maybe_unused]] auto sep = "";
    ((os << exchange(sep, " ") << xs), ...);
    return os;
  };
  return apply(f, t);
}

template <class R, enable_if_t<!is_convertible_v<R, string_view>>* = nullptr>
auto operator<<(ostream& os, R&& r) -> decltype(os << *begin(r)) {
  auto sep = "";
  for (auto&& e : r) {
    os << exchange(sep, " ") << e;
  }
  return os;
}

}  // namespace std

namespace atcoder {

template <class T, internal::is_modint_t<T>* = nullptr>
istream& operator>>(istream& is, T& x) {
  int v;
  is >> v;
  x = T::raw(v);
  return is;
}

template <class T, internal::is_modint_t<T>* = nullptr>
ostream& operator<<(ostream& os, const T& x) {
  return os << x.val();
}

}  // namespace atcoder

template <class... Ts>
void print(Ts&&... xs) {
  cout << tie(xs...) << '\n';
}

inline auto rep(int l, int r) { return views::iota(min(l, r), r); }
inline auto rep(int n) { return rep(0, n); }
inline auto rep1(int l, int r) { return rep(l, r + 1); }
inline auto rep1(int n) { return rep(1, n + 1); }
inline auto per(int l, int r) { return rep(l, r) | views::reverse; }
inline auto per(int n) { return per(0, n); }
inline auto per1(int l, int r) { return per(l, r + 1); }
inline auto per1(int n) { return per(1, n + 1); }

#endif  // __INCLUDE_LEVEL__
0