結果

問題 No.7 プライムナンバーゲーム
ユーザー phsplsphspls
提出日時 2020-07-07 16:09:12
言語 Rust
(1.77.0)
結果
AC  
実行時間 1,848 ms / 5,000 ms
コード長 1,218 bytes
コンパイル時間 824 ms
コンパイル使用メモリ 173,444 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-09 04:57:16
合計ジャッジ時間 12,461 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,820 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1,838 ms
6,944 KB
testcase_03 AC 45 ms
6,944 KB
testcase_04 AC 10 ms
6,944 KB
testcase_05 AC 9 ms
6,948 KB
testcase_06 AC 335 ms
6,944 KB
testcase_07 AC 196 ms
6,948 KB
testcase_08 AC 67 ms
6,948 KB
testcase_09 AC 595 ms
6,948 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 200 ms
6,948 KB
testcase_12 AC 1,155 ms
6,944 KB
testcase_13 AC 1,256 ms
6,948 KB
testcase_14 AC 1,848 ms
6,948 KB
testcase_15 AC 1,698 ms
6,948 KB
testcase_16 AC 1,526 ms
6,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn get_primes(n: usize) -> Vec<usize> {
    let mut flgs = vec![true; n+1];
    flgs[0] = false;
    flgs[1] = false;
    let limit = (n as f64).sqrt().ceil() as usize + 1;
    for i in 2..=limit {
        if !flgs[i] { continue; }
        for j in i..=(n/i) {
            flgs[j*i] = false;
        }
    }
    flgs.iter().enumerate().filter(|&pair| *pair.1).map(|pair| pair.0).collect()
}

fn main() {
    let mut n = String::new();
    std::io::stdin().read_line(&mut n).ok();
    let n: usize = n.trim().parse().unwrap();

    let primes: Vec<usize> = get_primes(n);
    let mut result: Vec<Option<usize>> = vec![None; n+1];
    result[2] = Some(0);
    if n > 2 {
        result[3] = Some(0);
    }
    for i in 4..=n {
        let mut temp: Vec<usize> =vec![];
        for p in primes.iter() {
            if *p > i - 2 { break; }
            if temp.contains(&p) { continue; }
            temp.push(result[i - p].unwrap());
        }
        temp.sort();
        result[i] = temp.iter().enumerate()
            .filter(|pair| pair.0 != *pair.1)
            .map(|pair| pair.0)
            .nth(0)
            .or(Some(temp.len()));
    }
    println!("{}", if result[n] == Some(0) { "Lose" } else { "Win" });
}
0