結果

問題 No.1677 mæx
ユーザー hir355hir355
提出日時 2021-09-10 22:29:33
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 34 ms / 2,000 ms
コード長 3,452 bytes
コンパイル時間 2,361 ms
コンパイル使用メモリ 214,508 KB
実行使用メモリ 4,988 KB
最終ジャッジ日時 2023-09-02 18:50:20
合計ジャッジ時間 3,826 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <atcoder/modint>
#include <bits/stdc++.h>

using namespace std;
using namespace atcoder;

constexpr long long INF_LL = 2000000000000000000LL;
constexpr int INF = 100000000;
constexpr long long MOD = 998244353;

#define all(x) x.begin(), x.end()
#define REP(i, a, b) for(int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<P> vp;
typedef vector<ll> vl;

int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
int sign[2] = {1, -1};

template <class T> bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}

ll modpow(ll a, ll b, ll m) {
    if(b == 0)
        return 1;
    ll t = modpow(a, b / 2, m);
    if(b & 1) {
        return (t * t % m) * a % m;
    } else {
        return t * t % m;
    }
}

struct edge {
    int to;
    ll cost;
    edge(int t, ll c) { to = t, cost = c; }
};

typedef vector<vector<edge>> graph;

using mint = modint998244353;
// constexpr int MAX_COM = 100001;
// mint fac[MAX_COM], ifac[MAX_COM];
// void initfac() {
//     fac[0] = ifac[0] = 1;
//     REP(i, 1, MAX_COM) fac[i] = i * fac[i - 1];
//     REP(i, 1, MAX_COM) ifac[i] = 1 / fac[i];
// }
// mint nCr(int n, int r){
//     if(r < 0 || n < r) return 0;
//     return fac[n] * ifac[n - r] * ifac[r];
// }

// typedef int S;
// S op(S x, S y){ return max(x, y); }
// S e(){ return 0; }
// typedef int F;
// S mapping(F f, S x){ return f + x; }
// F composition(F f, F g){ return f + g; }
// F id(){ return 0; }

typedef vector<mint> vm;

vm ma(vm &a, vm &b){
    vm res(3, 0);
    rep(i, 3) rep(j, 3){
        res[max(i, j)] += a[i] * b[j];
    }
    return res;
}

vm me(vm &a, vm &b){
    vm res(3, 0);
    rep(i, 3) rep(j, 3){
        int k = 0;
        while(i == k || j == k) k++;
        res[k] += a[i] * b[j];
    }
    return res;
}

vm mq(vm &a, vm &b){
    vm res(3, 0), x = ma(a, b), y = me(a, b);
    rep(i, 3) res[i] = (x[i] + y[i]) / 2;
    return res;
}

void solve(){
    string s;
    int k;
    cin >> s >> k;
    reverse(all(s));
    stack<vm> st;
    char tmp = '-';
    mint cnt = 1;
    for(char c : s){
        if(c == '?'){
            if(tmp == 'x'){
                cnt *= 2;
                vm x = st.top();
                st.pop();
                vm y = st.top();
                st.pop();
                st.push(mq(x, y));
            }else{
                cnt *= 3;
                st.push({(mint)1/3, (mint)1/3, (mint)1/3});
            }
        }else if(c == '0'){
            st.push({1, 0, 0});
        }else if(c == '1'){
            st.push({0, 1, 0});
        }else if(c == '2'){
            st.push({0, 0, 1});
        }else if(c == 'a'){
            vm x = st.top();
            st.pop();
            vm y = st.top();
            st.pop();
            st.push(ma(x, y));
        }else if(c == 'e'){
            vm x = st.top();
            st.pop();
            vm y = st.top();
            st.pop();
            st.push(me(x, y));
        }
        tmp = c;
    }
    cout << (st.top()[k] * cnt).val() << endl;
}

int main(){
    cin.tie(0);
    std::ios_base::sync_with_stdio(false);
    std::cout << std::fixed << std::setprecision(16);

    // initfac();
    int t;
    t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
    }
}
0