結果

問題 No.1677 mæx
ユーザー beetbeet
提出日時 2021-09-10 21:41:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 2,919 bytes
コンパイル時間 2,288 ms
コンパイル使用メモリ 200,768 KB
実行使用メモリ 814,560 KB
最終ジャッジ日時 2023-09-02 16:30:07
合計ジャッジ時間 4,403 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 MLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using Int = long long;
const char newl = '\n';

template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}
template<typename T=int>
vector<T> read(size_t n){
  vector<T> ts(n);
  for(size_t i=0;i<n;i++) cin>>ts[i];
  return ts;
}


template<typename T, T MOD = 1000000007>
struct Mint{
  inline static constexpr T mod = MOD;
  T v;
  Mint():v(0){}
  Mint(signed v):v(v){}
  Mint(long long t){v=t%MOD;if(v<0) v+=MOD;}

  Mint pow(long long k){
    Mint res(1),tmp(v);
    while(k){
      if(k&1) res*=tmp;
      tmp*=tmp;
      k>>=1;
    }
    return res;
  }

  static Mint add_identity(){return Mint(0);}
  static Mint mul_identity(){return Mint(1);}

  Mint inv(){return pow(MOD-2);}

  Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;}
  Mint& operator/=(Mint a){return (*this)*=a.inv();}

  Mint operator+(Mint a) const{return Mint(v)+=a;}
  Mint operator-(Mint a) const{return Mint(v)-=a;}
  Mint operator*(Mint a) const{return Mint(v)*=a;}
  Mint operator/(Mint a) const{return Mint(v)/=a;}

  Mint operator+() const{return *this;}
  Mint operator-() const{return v?Mint(MOD-v):Mint(v);}

  bool operator==(const Mint a)const{return v==a.v;}
  bool operator!=(const Mint a)const{return v!=a.v;}

  static Mint comb(long long n,int k){
    Mint num(1),dom(1);
    for(int i=0;i<k;i++){
      num*=Mint(n-i);
      dom*=Mint(i+1);
    }
    return num/dom;
  }
};
template<typename T, T MOD>
ostream& operator<<(ostream &os,Mint<T, MOD> m){os<<m.v;return os;}


using M = Mint<int, 998244353>;
using A = array<M, 3>;

A expression(string,int&);
A number(string,int&);

bool f;

A expression(string s,int& p){
  if(s[p]!='m') return number(s,p);

  assert(s[p]=='m' and s[p+2]=='x' and s[p+3]=='(');
  char c=s[p+1];
  p+=4;
  A lhs=expression(s,p);
  assert(s[p]==',');
  p+=1;
  A rhs=expression(s,p);
  assert(s[p]==')');
  p+=1;

  auto mex=[&](int i,int j){
    if(i!=0 and j!=0) return 0;
    if(i!=1 and j!=1) return 1;
    if(i!=2 and j!=2) return 2;
    std::abort();
  };
  A res={0,0,0};
  for(int i=0;i<3;i++){
    for(int j=0;j<3;j++){
      if(c=='a' or c=='?') res[max(i,j)]+=lhs[i]*rhs[j];
      if(c=='e' or c=='?') res[mex(i,j)]+=lhs[i]*rhs[j];
    }
  }
  return res;
}

A number(string s,int& p){
  A res={0,0,0};
  if(isdigit(s[p])){
    res[s[p]-'0']=M(1);
  }else{
    assert(s[p]=='?');
    res[0]=res[1]=res[2]=M(1);
  }
  p++;
  return res;
}

//INSERT ABOVE HERE
signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  string s;
  cin>>s;

  int p=0;
  A ans=expression(s,p);

  int k;
  cin>>k;
  cout<<ans[k]<<newl;
  return 0;
}
0