結果

問題 No.1677 mæx
ユーザー ziitaziita
提出日時 2021-09-10 23:25:13
言語 Rust
(1.77.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 3,902 bytes
コンパイル時間 4,731 ms
コンパイル使用メモリ 147,476 KB
実行使用メモリ 8,412 KB
最終ジャッジ日時 2023-09-02 22:04:18
合計ジャッジ時間 6,117 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,368 KB
testcase_01 AC 1 ms
4,368 KB
testcase_02 AC 1 ms
4,372 KB
testcase_03 AC 1 ms
4,372 KB
testcase_04 AC 14 ms
6,716 KB
testcase_05 AC 14 ms
6,824 KB
testcase_06 AC 14 ms
6,764 KB
testcase_07 AC 14 ms
6,516 KB
testcase_08 AC 14 ms
6,840 KB
testcase_09 AC 14 ms
6,832 KB
testcase_10 AC 14 ms
6,812 KB
testcase_11 AC 14 ms
6,768 KB
testcase_12 AC 14 ms
6,812 KB
testcase_13 AC 14 ms
6,536 KB
testcase_14 AC 13 ms
6,836 KB
testcase_15 AC 13 ms
6,816 KB
testcase_16 AC 14 ms
6,480 KB
testcase_17 AC 14 ms
6,744 KB
testcase_18 AC 14 ms
6,748 KB
testcase_19 AC 1 ms
4,368 KB
testcase_20 AC 1 ms
4,372 KB
testcase_21 AC 15 ms
8,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused_imports)]
#![allow(non_snake_case, unused)]

// ---------- begin input macro ----------
// https://atcoder.jp/contests/hokudai-hitachi2019-1/submissions/10518254 より
pub fn readln() -> String {
    let mut line = String::new();
    ::std::io::stdin().read_line(&mut line).unwrap_or_else(|e| panic!("{}", e));
    line
}

macro_rules! read {
    ($($t:tt),*; $n:expr) => {{
        let stdin = ::std::io::stdin();
        let ret = ::std::io::BufRead::lines(stdin.lock()).take($n).map(|line| {
            let line = line.unwrap();
            let mut it = line.split_whitespace();
            _read!(it; $($t),*)
        }).collect::<Vec<_>>();
        ret
    }};
    ($($t:tt),*) => {{
        let line = readln();
        let mut it = line.split_whitespace();
        _read!(it; $($t),*)
    }};
}

macro_rules! _read {
    ($it:ident; [char]) => {
        _read!($it; String).chars().collect::<Vec<_>>()
    };
    ($it:ident; [u8]) => {
        Vec::from(_read!($it; String).into_bytes())
    };
    ($it:ident; usize1) => {
        $it.next().unwrap_or_else(|| panic!("input mismatch")).parse::<usize>().unwrap_or_else(|e| panic!("{}", e)) - 1
    };
    ($it:ident; [usize1]) => {
        $it.map(|s| s.parse::<usize>().unwrap_or_else(|e| panic!("{}", e)) - 1).collect::<Vec<_>>()
    };
    ($it:ident; [$t:ty]) => {
        $it.map(|s| s.parse::<$t>().unwrap_or_else(|e| panic!("{}", e))).collect::<Vec<_>>()
    };
    ($it:ident; $t:ty) => {
        $it.next().unwrap_or_else(|| panic!("input mismatch")).parse::<$t>().unwrap_or_else(|e| panic!("{}", e))
    };
    ($it:ident; $($t:tt),+) => {
        ($(_read!($it; $t)),*)
    };
}
// ---------- end input macro ----------


use std::cmp::*;
use std::collections::*;
use std::ops::*;
use std::marker::*;
 
const INF: i64 = std::i64::MAX/100;
// const MOD: i64 = 1_000_000_007;
const MOD: i64 = 998_244_353;

fn main() {
    let c = read!([char]);
    let k = read!(usize);
    let ans = solve(0,&c);
    println!("{}",ans[k]);
}

fn solve(s: usize, c: &Vec<char>) -> Vec<i64> {
    if c[s]=='?' {
        return vec![1,1,1,(s+1) as i64];
    }
    if c[s]=='0' {
        return vec![1,0,0,(s+1) as i64];
    }
    if c[s]=='1' {
        return vec![0,1,0,(s+1) as i64];
    }
    if c[s]=='2' {
        return vec![0,0,1,(s+1) as i64];
    }
    if c[s..s+3]==['m','a','x'] {
        let ret = func(s+4,&c);
        let mut ans = vec![0;4];
        for i in 0..3 {
            for j in 0..3 {
                ans[max(i,j)] += ret[i][j];
                ans[max(i,j)] %= MOD;
            }
        }
        ans[3] = ret[0][3]+1;
        return ans;
    }
    if c[s..s+3]==['m','e','x'] {
        let ret = func(s+4,&c);
        let mut ans = vec![0;4];
        for i in 0..3 {
            for j in 0..3 {
                for k in 0..3 {
                    if k!=i&&k!=j {
                        ans[k] += ret[i][j];
                        ans[k] %= MOD;
                        break;
                    }
                }
            }
        }
        ans[3] = ret[0][3]+1;
        return ans;
    }
    let ret = func(s+4,&c);
    let mut ans = vec![0;4];
    for i in 0..3 {
        for j in 0..3 {
            for k in 0..3 {
                if k!=i&&k!=j {
                    ans[k] += ret[i][j];
                    ans[k] %= MOD;
                    break;
                }
            }
        }
    }
    for i in 0..3 {
        for j in 0..3 {
            ans[max(i,j)] += ret[i][j];
            ans[max(i,j)] %= MOD;
        }
    }
    ans[3] = ret[0][3]+1;
    return ans;
}

fn func(s: usize, c: &Vec<char>) -> Vec<Vec<i64>> {
    let x = solve(s,&c);
    let y = solve(x[3] as usize + 1, &c);
    let mut ans = vec![vec![0;4];4];
    for i in 0..3 {
        for j in 0..3 {
            ans[i][j] += x[i]*y[j];
            ans[i][j]%=MOD;
        }
    }
    ans[0][3] = y[3];
    return ans;
}
0