結果

問題 No.3040 Aoiスコア
ユーザー sai
提出日時 2025-02-28 22:01:15
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 4 ms / 1,000 ms
コード長 1,640 bytes
コンパイル時間 2,995 ms
コンパイル使用メモリ 284,584 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-06-20 20:57:53
合計ジャッジ時間 3,958 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/modint>
using mint = atcoder::modint998244353;

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int N, M; std::string S; std::cin >> N >> M >> S;
  std::vector<std::vector<int>> G(N);
  for (int i = 0; i < M; i++) {
    int a, b; std::cin >> a >> b; a--, b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  int q = 0;
  for (int i = 0; i < N; i++) {
    q += (S[i] == '?');
  }
  std::vector<mint> pow(q + 1, 1);
  for (int i = 0; i < q; i++) {
    pow[i + 1] = 26 * pow[i];
  }
  mint ans = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == 'o') {
      int cnt_a = 0, cnt_i = 0, cnt_q = 0;
      for (auto v : G[i]) {
        if (S[v] == 'a') {
          cnt_a++;
        } else if (S[v] == 'i') {
          cnt_i++;
        } else if (S[v] == '?') {
          cnt_q++;
        }
      }
      ans += cnt_a * cnt_i * pow[q];
      if (q > 0) {
        ans += cnt_a * cnt_q * pow[q - 1];
        ans += cnt_i * cnt_q * pow[q - 1];
      }
      if (q > 1) {
        ans += cnt_q * (cnt_q - 1) * pow[q - 2];
      }
    } else if (S[i] == '?') {
      int cnt_a = 0, cnt_i = 0, cnt_q = 0;
      for (auto v : G[i]) {
        if (S[v] == 'a') {
          cnt_a++;
        } else if (S[v] == 'i') {
          cnt_i++;
        } else if (S[v] == '?') {
          cnt_q++;
        }
      }
      ans += cnt_a * cnt_i * pow[q - 1];
      if (q > 1) {
        ans += cnt_a * cnt_q * pow[q - 2];
        ans += cnt_i * cnt_q * pow[q - 2];
      }
      if (q > 2) {
        ans += cnt_q * (cnt_q - 1) * pow[q - 3];
      }
    }
  }
  std::cout << ans.val() << '\n';
}
0