結果

問題 No.7 プライムナンバーゲーム
ユーザー Maricom_tkg
提出日時 2018-11-06 10:55:37
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,377 bytes
コンパイル時間 13,736 ms
コンパイル使用メモリ 379,192 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-10-01 16:16:25
合計ジャッジ時間 13,528 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::Read;

fn gen_p_nums(limit: usize) -> Vec<usize> {
    if limit < 2 {
        panic!();
    }
    
    let mut is_prime: Vec<bool> = vec![true; limit+1];
    is_prime[0] = false;
    is_prime[1] = false;
    
    let thresh: usize = (limit as f64).sqrt() as usize;

    for num in 2..=thresh {
        if is_prime[num] {
            let mut idx: usize = num+num;
            while idx <= limit {
                is_prime[idx] = false;
                idx += num;
            }
        }
    }

    is_prime.into_iter()
        .enumerate()
        .filter_map(|(n, is_p)| {
            if is_p {
                Some(n)
            } else {
                None
            }
        })
        .collect()
}

fn main() {
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf).unwrap();
    
    let n: usize = buf.trim().parse().unwrap();
    if n < 2 {
        panic!();
    }
    
    let primes: Vec<usize> = gen_p_nums(n);
    let mut can_win: Vec<bool> = vec![false; n+1];
    can_win[0] = true;
    can_win[1] = true;
    
    for num in 2..=n {
        for i in 0..primes.len() {
            let next_num: usize = num.saturating_sub(primes[i]);
            if !can_win[next_num] {
                can_win[num] = true;
                break
            }
        }
    }
    
    println!("{}", if can_win[n] {"Win"} else {"Lose"});
}
0