結果

問題 No.1956 猫の額
ユーザー koba-e964koba-e964
提出日時 2023-03-27 13:38:36
言語 Rust
(1.77.0 + proconio)
結果
MLE  
実行時間 -
コード長 4,363 bytes
コンパイル時間 13,997 ms
コンパイル使用メモリ 377,600 KB
実行使用メモリ 15,744 KB
最終ジャッジ日時 2024-09-19 10:14:23
合計ジャッジ時間 37,650 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 MLE -
testcase_01 MLE -
testcase_02 MLE -
testcase_03 MLE -
testcase_04 MLE -
testcase_05 MLE -
testcase_06 MLE -
testcase_07 MLE -
testcase_08 AC 1,232 ms
12,800 KB
testcase_09 AC 365 ms
13,824 KB
testcase_10 AC 1,060 ms
11,392 KB
testcase_11 AC 607 ms
8,192 KB
testcase_12 WA -
testcase_13 AC 7 ms
5,376 KB
testcase_14 AC 164 ms
5,376 KB
testcase_15 AC 163 ms
14,848 KB
testcase_16 AC 1,377 ms
14,208 KB
testcase_17 MLE -
testcase_18 MLE -
testcase_19 MLE -
testcase_20 MLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::{Write, BufWriter};
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($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)*}
    };
}

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)*}
    };
}

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:tt ]) => {{
        let len = read_value!($next, usize);
        read_value!($next, [$t; len])
    }};
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

#[inline]
fn zadd(mut a: i32, b: i32, m: i32) -> i32 {
    a += b;
    if a >= m {
        a -= m;
    }
    a
}

fn main() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {($($format:tt)*) => (let _ = write!(out,$($format)*););}
    macro_rules! putvec {
        ($v:expr) => {
            for i in 0..$v.len() {
                puts!("{}{}", $v[i], if i + 1 == $v.len() {"\n"} else {" "});
            }
        }
    }
    input! {
        n: usize, m: i32, c: usize,
        a: [usize; n],
    }
    let s: usize = a.iter().sum();
    let l = 31;
    // 0.3 M entris, 1.2 MB
    let mut res = vec![[0i32; 3]; s + 1];
    // 3.1 M entries, 12.4 MB
    let mut dp = vec![vec![0i32; s + 1]; l];
    let mut tmp = vec![0i32; s + 1];
    dp[0][0] = 1;
    // r = 1
    for i in 0..n {
        for j in 0..s + 1 {
            tmp[j] = dp[0][j];
        }
        for k in 0..s - a[i] + 1 {
            dp[0][k + a[i]] = zadd(dp[0][k + a[i]], dp[l - 1][k], 2 * m);
        }
        for j in (1..l - 1).rev() {
            for k in 0..s - a[i] + 1 {
                dp[j + 1][k + a[i]] = zadd(dp[j + 1][k + a[i]], dp[j][k], 2 * m);
            }
        }
        for k in 0..s - a[i] + 1 {
            dp[1][k + a[i]] = zadd(dp[1][k + a[i]], tmp[k], 2 * m);
        }
    }
    for i in 0..s + 1 {
        res[i][0] = dp[c % l][i];
    }
    for v in &mut dp {
        for v in v {
            *v = 0;
        }
    }
    dp[0][0] = 1;
    // r = -1
    for i in 0..n {
        for j in 0..s + 1 {
            tmp[j] = dp[0][j];
        }
        for k in 0..s - a[i] + 1 {
            dp[0][k + a[i]] = zadd(dp[0][k + a[i]], 2 * m - dp[l - 1][k], 2 * m);
        }
        for j in (1..l - 1).rev() {
            for k in 0..s - a[i] + 1 {
                dp[j + 1][k + a[i]] = zadd(dp[j + 1][k + a[i]], dp[j][k], 2 * m);
            }
        }
        for k in 0..s - a[i] + 1 {
            dp[1][k + a[i]] = zadd(dp[1][k + a[i]], tmp[k], 2 * m);
        }
    }
    for i in 0..s + 1 {
        res[i][1] = dp[c % l][i];
    }
    for v in &mut dp {
        for v in v {
            *v = 0;
        }
    }
    dp[0][0] = 1;
    // r = 0
    for i in 0..n {
        for j in (0..l - 1).rev() {
            for k in 0..s - a[i] + 1 {
                dp[j + 1][k + a[i]] = zadd(dp[j + 1][k + a[i]], dp[j][k], 2 * m);
            }
        }
    }
    for i in 0..s + 1 {
        res[i][2] = dp[c % l][i];
    }
    drop(dp);
    let mut ans = vec![0; s];
    for i in 1..s + 1 {
        let q = c / l;
        ans[i - 1] = if q == 0 {
            res[i][2] % m
        } else {
            let x = zadd(res[i][0], 2 * m - res[i][2], 2 * m);
            let y = zadd(res[i][1], 2 * m - res[i][2], 2 * m);
            if q == 2 {
                (x + y) % (2 * m) / 2
            } else {
                (x + 2 * m - y) % (2 * m) / 2
            }
        };
    }
    putvec!(ans);
}
0