結果

問題 No.1677 mæx
ユーザー 👑 NachiaNachia
提出日時 2021-09-10 21:44:22
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 32 ms / 2,000 ms
コード長 1,611 bytes
コンパイル時間 2,308 ms
コンパイル使用メモリ 112,744 KB
最終ジャッジ日時 2025-01-24 10:06:41
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <atcoder/modint>
using namespace std;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
#define rep(i,n) for(int i=0; i<(n); i++)


using m32 = atcoder::modint998244353;
struct Exp{ m32 A[3] = {}; };
int mex(int a, int b){
  static const int mat[3][3] = { {1,2,1},{2,0,0},{1,0,0} };
  return mat[a][b];
}


Exp operator+(Exp a, Exp b){
  Exp res;
  rep(i,3) res.A[i] = a.A[i] + b.A[i];
  return res;
}
Exp maxExp(Exp a, Exp b){
  Exp res;
  rep(i,3) rep(j,3) res.A[max(i,j)] += a.A[i] * b.A[j];
  return res;
}
Exp mexExp(Exp a, Exp b){
  Exp res;
  rep(i,3) rep(j,3) res.A[mex(i,j)] += a.A[i] * b.A[j];
  return res;
}



Exp getExp(const string& S, int& p){
  if(S[p] == '?'){
    p++;
    return Exp{ {1,1,1} };
  }
  if(S[p] == '0'){
    p++;
    return Exp{ {1,0,0} };
  }
  if(S[p] == '1'){
    p++;
    return Exp{ {0,1,0} };
  }
  if(S[p] == '2'){
    p++;
    return Exp{ {0,0,1} };
  }
  if(S[p] == 'm'){
    p++; // m
    char nx = S[p];
    p++; // ?
    p++; // x
    p++; // (
    Exp a = getExp(S,p);
    p++; // ,
    Exp b = getExp(S,p);
    p++; // )
    if(nx == '?') return mexExp(a,b) + maxExp(a,b);
    if(nx == 'e') return mexExp(a,b);
    if(nx == 'a') return maxExp(a,b);
  }
  exit(1);
}


int main(){
  string S; cin >> S;
  int p = 0;
  auto res = getExp(S,p);
  int k; cin >> k;
  cout << res.A[k].val() << endl;
  return 0;
}




struct ios_do_not_sync {
    ios_do_not_sync() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    }
} ios_do_not_sync_instance;
0