結果

問題 No.7 プライムナンバーゲーム
ユーザー mtwtkmanmtwtkman
提出日時 2019-10-19 23:47:08
言語 Rust
(1.77.0)
結果
AC  
実行時間 6 ms / 5,000 ms
コード長 2,318 bytes
コンパイル時間 972 ms
コンパイル使用メモリ 173,640 KB
実行使用メモリ 6,696 KB
最終ジャッジ日時 2024-04-09 04:44:02
合計ジャッジ時間 1,928 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,692 KB
testcase_01 AC 1 ms
6,692 KB
testcase_02 AC 6 ms
6,688 KB
testcase_03 AC 1 ms
6,692 KB
testcase_04 AC 1 ms
6,688 KB
testcase_05 AC 1 ms
6,692 KB
testcase_06 AC 2 ms
6,696 KB
testcase_07 AC 2 ms
6,688 KB
testcase_08 AC 1 ms
6,688 KB
testcase_09 AC 3 ms
6,692 KB
testcase_10 AC 1 ms
6,688 KB
testcase_11 AC 2 ms
6,692 KB
testcase_12 AC 4 ms
6,688 KB
testcase_13 AC 4 ms
6,688 KB
testcase_14 AC 6 ms
6,692 KB
testcase_15 AC 5 ms
6,688 KB
testcase_16 AC 5 ms
6,692 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::{self, Read};

fn read_stdin() -> Vec<String> {
    let mut buffer = String::new();
    io::stdin().read_to_string(&mut buffer).ok();
    buffer.trim().split('\n').map(|s| s.to_string()).collect()
}

fn calc_primes(n: usize) -> Vec<usize> {
    let mut search_list = (2..n + 1).collect::<Vec<usize>>();
    let mut prime_list: Vec<usize> = Vec::new();
    while (search_list[0] as f64) < (n as f64).sqrt() {
        let head = search_list[0];
        prime_list.push(head);
        search_list = search_list.into_iter().filter(|x| *x % head > 0).collect::<Vec<usize>>();
    }
    prime_list.append(&mut search_list);
    prime_list
}

#[derive(Debug, Eq, PartialEq)]
enum RunResult {
    Win,
    Lose,
}

impl RunResult {
    fn s(&self) -> String {
        match self {
            Self::Win => "Win",
            Self::Lose => "Lose",
        }.to_string()
    }
}

fn run(n: usize) -> RunResult {
    let primes = calc_primes(n);
    let mut nums = vec![0; n + 1];
    nums[0] = 1;
    nums[1] = 1;
    for i in 3..=n {
        if primes.clone().into_iter().take_while(|p| *p < i).find(|p| nums[i - p] == 0).is_some() {
            nums[i] = 1;
        }
    }
    if nums[n] == 0 {
        RunResult::Lose
    } else {
        RunResult::Win
    }
}

fn main() {
    let n = read_stdin()[0].parse::<usize>().unwrap();
    let result = run(n);
    println!("{}", result.s());
}

#[cfg(test)]
mod tests {
    use super::{calc_primes, run, RunResult};

    #[test]
    fn test_calc_primes() {
        assert_eq!(calc_primes(11), vec![2, 3, 5, 7, 11]);
        assert_eq!(calc_primes(10), vec![2, 3, 5, 7]);
        assert_eq!(calc_primes(2), vec![2]);
    }

    #[test]
    fn test_expensive_calc_primes() {
        calc_primes(1000000);
        assert_eq!(1, 1);
    }

    #[test]
    fn test_run_lose() {
        let loses = [2, 3, 11, 12];
        for i in loses.iter() {
            println!("test num: {}", i);
            assert_eq!(run(*i), RunResult::Lose);
        }
    }

    #[test]
    fn test_run_win() {
        let wins = [4, 5, 6, 7, 8, 9, 10, 13];
        for i in wins.iter() {
            println!("test num: {}", i);
            assert_eq!(run(*i), RunResult::Win);
        }
    }

    #[test]
    fn test_expensive_run() {
        run(10000);
        assert_eq!(1, 1);
    }
}
0