結果

問題 No.1645 AB's abs
ユーザー 👑 terry_u16terry_u16
提出日時 2021-08-13 23:02:54
言語 Rust
(1.77.0)
結果
AC  
実行時間 27 ms / 2,000 ms
コード長 2,941 bytes
コンパイル時間 1,184 ms
コンパイル使用メモリ 173,300 KB
実行使用メモリ 18,048 KB
最終ジャッジ日時 2024-04-14 20:01:29
合計ジャッジ時間 3,823 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 3 ms
6,812 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 4 ms
6,940 KB
testcase_09 AC 5 ms
6,940 KB
testcase_10 AC 4 ms
6,944 KB
testcase_11 AC 3 ms
6,944 KB
testcase_12 AC 4 ms
6,944 KB
testcase_13 AC 26 ms
17,280 KB
testcase_14 AC 22 ms
15,232 KB
testcase_15 AC 25 ms
17,152 KB
testcase_16 AC 23 ms
15,872 KB
testcase_17 AC 25 ms
16,768 KB
testcase_18 AC 22 ms
15,744 KB
testcase_19 AC 25 ms
17,280 KB
testcase_20 AC 21 ms
14,976 KB
testcase_21 AC 24 ms
15,872 KB
testcase_22 AC 25 ms
16,768 KB
testcase_23 AC 26 ms
18,048 KB
testcase_24 AC 27 ms
17,920 KB
testcase_25 AC 26 ms
18,048 KB
testcase_26 AC 27 ms
18,048 KB
testcase_27 AC 26 ms
18,048 KB
testcase_28 AC 26 ms
18,048 KB
testcase_29 AC 26 ms
18,048 KB
testcase_30 AC 27 ms
18,048 KB
testcase_31 AC 27 ms
17,920 KB
testcase_32 AC 27 ms
17,920 KB
testcase_33 AC 26 ms
17,920 KB
testcase_34 AC 27 ms
17,920 KB
testcase_35 AC 26 ms
18,048 KB
testcase_36 AC 26 ms
18,048 KB
testcase_37 AC 27 ms
17,920 KB
testcase_38 AC 26 ms
18,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

pub trait ChangeBest {
    fn chmin(&mut self, v: Self) -> bool;
    fn chmax(&mut self, v: Self) -> bool;
}

impl<T> ChangeBest for T
where
    T: PartialOrd,
{
    fn chmin(&mut self, v: T) -> bool {
        if *self > v {
            *self = v;
            true
        } else {
            false
        }
    }

    fn chmax(&mut self, v: T) -> bool {
        if *self < v {
            *self = v;
            true
        } else {
            false
        }
    }
}

#[allow(unused_macros)]
macro_rules! mat {
    ($e:expr; $d:expr) => { vec![$e; $d] };
    ($e:expr; $d:expr $(; $ds:expr)+) => { vec![mat![$e $(; $ds)*]; $d] };
}

#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

const MOD: i64 = 998244353;

fn main() {
    input! {
        n: usize,
        a: [i64; n],
    };

    let mut sum = 0;
    let offset = n * 100;
    let mut dp = mat![0; n + 1; n * 200 + 1];
    dp[0][offset] = 1;

    for (i, &ai) in a.iter().enumerate() {
        for sum in 0..dp[i].len() {
            if let Some(next) = sum.checked_sub(ai as usize) {
                dp[i + 1][next] += dp[i][sum];
                dp[i + 1][next] %= MOD;
            }

            let next = sum + ai as usize;

            if next < dp[i + 1].len() {
                dp[i + 1][next] += dp[i][sum];
                dp[i + 1][next] %= MOD;
            }
        }
    }

    for (s, &count) in dp[n].iter().enumerate() {
        let value = s as i64 - offset as i64;
        sum += count * value.abs();
        sum %= MOD;
    }

    println!("{}", sum);
}
0