結果
| 問題 |
No.7 プライムナンバーゲーム
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-10-19 23:47:08 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 5 ms / 5,000 ms |
| コード長 | 2,318 bytes |
| コンパイル時間 | 11,915 ms |
| コンパイル使用メモリ | 379,228 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-10-01 16:25:59 |
| 合計ジャッジ時間 | 12,772 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 17 |
ソースコード
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);
}
}