結果
問題 |
No.3040 Aoiスコア
|
ユーザー |
|
提出日時 | 2025-02-28 21:41:15 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 212 ms / 1,000 ms |
コード長 | 2,959 bytes |
コンパイル時間 | 1,822 ms |
コンパイル使用メモリ | 207,468 KB |
実行使用メモリ | 285,056 KB |
最終ジャッジ日時 | 2025-06-20 20:55:29 |
合計ジャッジ時間 | 4,406 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 26 |
ソースコード
#include <bits/stdc++.h> using namespace std; long long mod = 998244353; //入力が必ず-mod<a<modの時. struct mint{ long long v = 0; mint(){} mint(int a){v = a<0?a+mod:a;} mint(long long a){v = a<0?a+mod:a;} mint(unsigned long long a){v = a;} long long val(){return v;} void modu(){v %= mod;} mint repeat2mint(const long long a,long long b){ mint ret = 1,p = a; while(b){ if(b&1) ret *= p; p *= p; b >>= 1; } return ret; }; mint &operator=(const mint &b) = default; mint operator-() const {return mint(0)-(*this);} mint operator+(const mint b){return mint(v)+=b;} mint operator-(const mint b){return mint(v)-=b;} mint operator*(const mint b){return mint(v)*=b;} mint operator/(const mint b){return mint(v)/=b;} mint operator+=(const mint b){ v += b.v; if(v >= mod) v -= mod; return *this; } mint operator-=(const mint b){ v -= b.v; if(v < 0) v += mod; return *this; } mint operator*=(const mint b){v = v*b.v%mod; return *this;} mint operator/=(mint b){ if(b == 0) assert(false); int left = mod-2; while(left){if(left&1) *this *= b; b *= b; left >>= 1;} return *this; } mint operator++(int){*this += 1; return *this;} mint operator--(int){*this -= 1; return *this;} bool operator==(const mint b){return v == b.v;} bool operator!=(const mint b){return v != b.v;} bool operator>(const mint b){return v > b.v;} bool operator>=(const mint b){return v >= b.v;} bool operator<(const mint b){return v < b.v;} bool operator<=(const mint b){return v <= b.v;} mint pow(const long long x){return repeat2mint(v,x);} mint inv(){return mint(1)/v;} }; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int N,M; cin >> N >> M; string s; cin >> s; vector<vector<int>> Graph(N); for(int i=0; i<M; i++){ int u,v; cin >> u >> v; u--; v--; Graph.at(u).push_back(v); Graph.at(v).push_back(u); } int ques = 0; for(auto c : s) ques += c=='?'; mint div26 = mint(1)/26; vector<mint> dp(N),dp2(N); for(int i=0; i<N; i++){ if(s.at(i) == 'a') dp.at(i) = 1; if(s.at(i) == '?') dp.at(i) = div26; } vector<vector<mint>> ok(N,vector<mint>(N)); for(int pos=0; pos<N; pos++) if(dp.at(pos) > 0) for(auto to : Graph.at(pos)){ if(s.at(to) == 'o') dp2.at(to) += dp.at(pos),ok.at(pos).at(to) = dp.at(pos); else if(s.at(to) == '?') dp2.at(to) += dp.at(pos)*div26,ok.at(pos).at(to) = dp.at(pos)*div26; } mint answer = 0; for(int pos=0; pos<N; pos++) for(auto to : Graph.at(pos)){ if(s.at(to) == 'i') answer += dp2.at(pos); else if(s.at(to) == '?') answer += (dp2.at(pos)-ok.at(to).at(pos))*div26; } answer *= mint(26).pow(ques); cout << answer.v << endl; }