結果

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

void fast_io() {
	ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
}

#include <atcoder/modint>
using mint = atcoder::modint998244353;
int main() {
	fast_io();
	int n, m;
	cin >> n >> m;
	string s;
	cin >> s;
	vector<vector<int>> g(n);
	for (int i = 0; i < m; i++) {
		int u, v;
		cin >> u >> v;
		u--, v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	mint inv_26 = mint(26).inv();
	mint inv_26_2 = mint(26 * 26).inv();
	mint inv_26_3 = mint(26 * 26 * 26).inv();
	mint ans = 0;
	for (int i = 0; i < n; i++) {
		if (s[i] != '?' && s[i] != 'o') {
			continue;
		}
		long long a_cnt = 0;
		long long q_cnt = 0;
		long long i_cnt = 0;
		for (int j : g[i]) {
			if (s[j] == 'a') {
				a_cnt++;
			} else if (s[j] == '?') {
				q_cnt++;
			} else if (s[j] == 'i') {
				i_cnt++;
			}
		}
		if (s[i] == 'o') {
			ans += a_cnt * i_cnt + a_cnt * q_cnt * inv_26 +
				   q_cnt * i_cnt * inv_26 + q_cnt * (q_cnt - 1) * inv_26_2;
		} else {
			ans += a_cnt * i_cnt * inv_26 + a_cnt * q_cnt * inv_26_2 +
				   q_cnt * i_cnt * inv_26_2 + q_cnt * (q_cnt - 1) * inv_26_3;
		}
	}
	int q_cnt = count(s.begin(), s.end(), '?');
	ans *= mint(26).pow(q_cnt);
	cout << ans.val() << endl;
}
0