結果
| 問題 |
No.1677 mæx
|
| コンテスト | |
| ユーザー |
Manuel1024
|
| 提出日時 | 2022-09-07 03:15:28 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 23 ms / 2,000 ms |
| コード長 | 2,214 bytes |
| コンパイル時間 | 852 ms |
| コンパイル使用メモリ | 79,180 KB |
| 実行使用メモリ | 10,640 KB |
| 最終ジャッジ日時 | 2024-11-22 03:34:24 |
| 合計ジャッジ時間 | 2,311 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 18 |
ソースコード
#include <iostream>
#include <vector>
#include <string>
using namespace std;
using ll = long long;
using P = pair<int, vector<int>>;
constexpr ll MOD = 998244353;
vector<P> G;
vector<vector<ll>> dp;
int expr(const string &s, int &i){
// cerr << i << " " << s[i] << ", ";
int ret = G.size();
if(s[i] == 'm'){
G.emplace_back(0, vector<int>{});
if(s[i+1] == 'a'){// max
G[ret].first = 4;
}else if(s[i+1] == 'e'){// mex
G[ret].first = 5;
}else{// m?x
G[ret].first = 6;
}
i += 4;
int x = expr(s, i);
G[ret].second.emplace_back(x);
// cerr << "add ok" << endl;
i++;
int y = expr(s, i);
G[ret].second.emplace_back(y);
i++;
}else{
G.emplace_back(s[i] == '?' ? 3 : s[i]-'0', vector<int>{});
i++;
}
// cerr << "ret: " << ret << ", ";
return ret;
}
ll f(int v, int x){
if(dp[x][v] != -1) return dp[x][v];
auto mex = [](int a, int b){
for(int res = 0; ; res++){
if(res != a && res != b) return res;
}
};
dp[x][v] = 0;
if(G[v].first < 3) dp[x][v] = x == G[v].first ? 1 : 0;
else if(G[v].first == 3) dp[x][v] = 1;
else{
if(G[v].first == 4 || G[v].first == 6){
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(max(i, j) == x){
dp[x][v] += f(G[v].second[0], i)*f(G[v].second[1], j)%MOD;
dp[x][v] %= MOD;
}
}
}
}
if(G[v].first == 5 || G[v].first == 6){
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(mex(i, j) == x){
dp[x][v] += f(G[v].second[0], i)*f(G[v].second[1], j)%MOD;
dp[x][v] %= MOD;
}
}
}
}
}
return dp[x][v];
}
int main(){
string s; cin >> s;
int k; cin >> k;
{
int i = 0;
expr(s, i);
}
dp = vector<vector<ll>>(3, vector<ll>(G.size(), -1));
cout << f(0, k) << endl;
return 0;
}
Manuel1024