結果

問題 No.7 プライムナンバーゲーム
ユーザー YoshihitoYoshihito
提出日時 2020-12-17 14:30:45
言語 Rust
(1.72.1)
結果
AC  
実行時間 22 ms / 5,000 ms
コード長 2,234 bytes
コンパイル時間 950 ms
コンパイル使用メモリ 152,680 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-07-24 21:42:25
合計ジャッジ時間 1,983 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

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

pub struct SievePrimes {
    sieve: Vec<bool>,
    current: usize,
}

impl SievePrimes {
    pub fn new(max: u32) -> SievePrimes {
        let mut sieve = vec![true; max as usize + 1];
        sieve[0] = false;
        sieve[1] = false;

        SievePrimes { sieve, current: 1 }
    }
}

impl Iterator for SievePrimes {
    type Item = u32;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            self.current += 1;
            if self.current >= self.sieve.len() || self.sieve[self.current] {
                break;
            }
        }

        if self.current < self.sieve.len() {
            if self.current * self.current <= self.sieve.len() {
                for i in (self.current..self.sieve.len())
                    .step_by(self.current)
                    .skip(1)
                {
                    self.sieve[i] = false;
                }
            }

            Some(self.current as u32)
        } else {
            None
        }
    }
}

#[derive(Debug)]
struct Input {
    n: usize,
}

fn next_token(cin_lock: &mut io::StdinLock) -> String {
    cin_lock
        .by_ref()
        .bytes()
        .map(|c| c.unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect::<String>()
}

fn read_input(cin_lock: &mut io::StdinLock) -> Input {
    Input {
        n: next_token(cin_lock).parse().unwrap(),
    }
}

fn can_win(n: usize) -> bool {
    let primes = SievePrimes::new(n as u32).collect::<Vec<_>>();

    let mut win = vec![false; n + 1];
    // 非素数。ここに来る直前の手を負けとするため、この2つは勝利扱いにする
    win[0] = true;
    win[1] = true;

    for i in 2..=n {
        for &p in primes.iter().filter(|&&p| p as usize <= i) {
            if !win[i - p as usize] {
                win[i] = true;
            }
        }
    }

    win[n]
}

fn solve(input: Input, _cin_lock: &mut io::StdinLock) {
    println!("{}", if can_win(input.n) {
        "Win"
    } else {
        "Lose"
    });
}

fn main() {
    let cin = io::stdin();
    let mut cin_lock = cin.lock();

    let input = read_input(&mut cin_lock);
    solve(input, &mut cin_lock);
}
0