結果

問題 No.3030 Kruskal-Katona
ユーザー atcoder8
提出日時 2025-02-21 23:01:34
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 647 ms / 2,000 ms
コード長 937 bytes
コンパイル時間 14,762 ms
コンパイル使用メモリ 400,508 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2025-02-21 23:01:51
合計ジャッジ時間 16,432 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

use proconio::input;

fn main() {
    input! {
        (n, i): (usize, usize),
    }

    let mut nn = vec![];
    let mut rem = n;
    for j in (1..=i).rev() {
        if rem == 0 {
            break;
        }

        let sub_n = find_max_n(rem, j);
        nn.push(sub_n);
        rem -= calc_combinations(sub_n, j);
    }

    println!(
        "{}",
        nn.iter()
            .map(|n| n.to_string())
            .collect::<Vec<_>>()
            .join(" ")
    );
}

/// Calculates the number of combinations that select `k` elements from `n` elements.
pub fn calc_combinations(n: usize, k: usize) -> usize {
    if k > n {
        return 0;
    }

    let k = k.min(n - k);
    let mut ret = 1;
    for i in 0..k {
        ret *= n - i;
        ret /= i + 1;
    }

    ret
}

fn find_max_n(rem: usize, i: usize) -> usize {
    (i..)
        .take_while(|&j| calc_combinations(j, i) <= rem)
        .last()
        .unwrap()
}
0