結果

問題 No.7 プライムナンバーゲーム
ユーザー cra77756176cra77756176
提出日時 2022-11-20 00:00:01
言語 Rust
(1.77.0)
結果
AC  
実行時間 135 ms / 5,000 ms
コード長 1,008 bytes
コンパイル時間 3,495 ms
コンパイル使用メモリ 191,688 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 03:36:46
合計ジャッジ時間 5,352 ms
ジャッジサーバーID
(参考情報)
judge10 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 135 ms
4,348 KB
testcase_03 AC 10 ms
4,348 KB
testcase_04 AC 4 ms
4,348 KB
testcase_05 AC 3 ms
4,348 KB
testcase_06 AC 40 ms
4,348 KB
testcase_07 AC 27 ms
4,348 KB
testcase_08 AC 12 ms
4,348 KB
testcase_09 AC 60 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 27 ms
4,348 KB
testcase_12 AC 96 ms
4,348 KB
testcase_13 AC 102 ms
4,348 KB
testcase_14 AC 134 ms
4,348 KB
testcase_15 AC 128 ms
4,348 KB
testcase_16 AC 118 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::{collections::HashSet, io};

fn get_primes(max: usize) -> Vec<usize> {
    let mut is_prime = vec![true; max + 1];
    is_prime[0] = false;
    is_prime[1] = false;

    for n in 2..=max {
        if !is_prime[n] {
            continue;
        }
        if n * n > max {
            break;
        }
        for i in ((n * n)..=max).step_by(n) {
            is_prime[i] = false;
        }
    }

    (0..=max).filter(|&n| is_prime[n]).collect()
}

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

    let primes = get_primes(n);
    let mut table = vec![1; n + 1];

    for i in 2..=n {
        let mut reachable = HashSet::new();
        for &p in &primes {
            if p > i {
                break;
            }
            reachable.insert(table[i - p]);
        }
        table[i] = (0..=i).find(|n| !reachable.contains(n)).unwrap();
    }

    println!("{}", if table[n] > 0 { "Win" } else { "Lose" });
}
0