結果

問題 No.2561 みんな大好きmod 998
ユーザー あさくちあさくち
提出日時 2023-12-02 15:40:01
言語 Rust
(1.77.0)
結果
AC  
実行時間 564 ms / 4,000 ms
コード長 2,810 bytes
コンパイル時間 1,897 ms
コンパイル使用メモリ 184,120 KB
実行使用メモリ 319,560 KB
最終ジャッジ日時 2023-12-02 15:40:11
合計ジャッジ時間 9,527 ms
ジャッジサーバーID
(参考情報)
judge14 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 25 ms
14,592 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 545 ms
319,560 KB
testcase_04 AC 556 ms
319,560 KB
testcase_05 AC 558 ms
319,560 KB
testcase_06 AC 558 ms
319,560 KB
testcase_07 AC 1 ms
6,676 KB
testcase_08 AC 1 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 1 ms
6,676 KB
testcase_11 AC 82 ms
43,184 KB
testcase_12 AC 1 ms
6,676 KB
testcase_13 AC 1 ms
6,676 KB
testcase_14 AC 1 ms
6,676 KB
testcase_15 AC 541 ms
319,560 KB
testcase_16 AC 1 ms
6,676 KB
testcase_17 AC 1 ms
6,676 KB
testcase_18 AC 0 ms
6,676 KB
testcase_19 AC 397 ms
227,568 KB
testcase_20 AC 1 ms
6,676 KB
testcase_21 AC 1 ms
6,676 KB
testcase_22 AC 1 ms
6,676 KB
testcase_23 AC 1 ms
6,676 KB
testcase_24 AC 0 ms
6,676 KB
testcase_25 AC 1 ms
6,676 KB
testcase_26 AC 105 ms
58,472 KB
testcase_27 AC 545 ms
319,560 KB
testcase_28 AC 143 ms
78,952 KB
testcase_29 AC 41 ms
22,944 KB
testcase_30 AC 134 ms
76,792 KB
testcase_31 AC 264 ms
161,844 KB
testcase_32 AC 61 ms
34,652 KB
testcase_33 AC 42 ms
22,944 KB
testcase_34 AC 564 ms
319,560 KB
testcase_35 AC 36 ms
21,664 KB
testcase_36 AC 36 ms
21,664 KB
testcase_37 AC 17 ms
10,496 KB
testcase_38 AC 79 ms
43,184 KB
testcase_39 AC 134 ms
76,792 KB
testcase_40 AC 141 ms
76,792 KB
testcase_41 AC 82 ms
43,184 KB
testcase_42 AC 155 ms
78,952 KB
testcase_43 AC 17 ms
10,496 KB
testcase_44 AC 57 ms
32,732 KB
testcase_45 AC 426 ms
227,568 KB
testcase_46 AC 51 ms
27,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// use itertools::Itertools;

const MOD_998: usize = 998;
const MOD_998244353: usize = 998244353;

fn main() {
    let (n, k) = input_tuple::<usize>();
    let a = input_vec::<usize>();

    let mut result = 0_usize;

    // println!("{:?}", combination(&(0..n).collect::<Vec<_>>(), k));

    // for combination in (0..n).combinations(k) {
    for combination in combination(&(0..n).collect::<Vec<_>>(), k) {
        let mut s_998 = 0_usize;
        let mut s_998244353 = 0_usize;

        for i in combination {
            s_998 += a[i];
            s_998 %= MOD_998;

            s_998244353 += a[i];
            s_998244353 %= MOD_998244353;
        }

        if s_998244353 <= s_998 {
            result += 1;
            result %= MOD_998;
        }
    }

    println!("{}", result);
}

fn combination(elems: &Vec<usize>, r: usize) -> Vec<Vec<usize>> {
    // vector<vector<T>> v{};
    let mut v = Vec::new();

    // vector<T> v1(r);
    let mut v1 = vec![0; r];

    // int n = elems.size();
    let n = elems.len();

    rec(n, r, &elems, &mut v, &mut v1, 0, 0, 0);

    return v;
}

fn rec(
    n: usize,
    r: usize,
    elems: &Vec<usize>,
    v: &mut Vec<Vec<usize>>,
    v1: &mut Vec<usize>,
    bit: usize,
    now: usize,
    num_decided: usize,
) {
    if num_decided == r {
        // int index = 0;
        let mut index = 0;
        // for(int i=0; i<n;i++){
        //     if(bit&(1<<i)){
        //         v1[index] = elems[i];
        //         index++;
        //     }
        // }
        for i in 0..n {
            if bit & (1 << i) > 0 {
                v1[index] = elems[i];
                index += 1;
            }
        }

        // v.push_back(v1);
        v.push(v1.clone());
        return;
    }

    // if(now == n)return;
    if now == n {
        return;
    }

    rec(
        n,
        r,
        &elems,
        v,
        v1,
        bit + (1 << now),
        now + 1,
        num_decided + 1,
    );
    rec(n, r, &elems, v, v1, bit, now + 1, num_decided);
}

fn input_tuple<T>() -> (T, T)
where
    T: std::str::FromStr,
    <T as std::str::FromStr>::Err: std::fmt::Debug,
{
    let stdin = std::io::stdin();

    let mut buf = String::new();
    stdin.read_line(&mut buf).unwrap();
    buf = buf.trim_end().to_owned();

    let mut iter = buf.split_whitespace();

    let n = iter.next().unwrap().parse().unwrap();
    let m = iter.next().unwrap().parse().unwrap();

    (n, m)
}

fn input_vec<T>() -> Vec<T>
where
    T: std::str::FromStr,
    <T as std::str::FromStr>::Err: std::fmt::Debug,
{
    let stdin = std::io::stdin();

    let mut buf = String::new();
    stdin.read_line(&mut buf).unwrap();
    buf = buf.trim_end().to_owned();

    let iter = buf.split_whitespace();

    let line = iter.map(|x| x.parse().unwrap()).collect();

    line
}
0