結果

問題 No.7 プライムナンバーゲーム
ユーザー wesriverywesrivery
提出日時 2019-06-29 18:38:17
言語 Rust
(1.77.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 1,367 bytes
コンパイル時間 1,570 ms
コンパイル使用メモリ 164,788 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-09 04:38:32
合計ジャッジ時間 2,110 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#[allow(unused_macros)]
macro_rules! input {
    ( $($t:ty),* ) => {{
        let mut s = String::new();
        std::io::stdin().read_line(&mut s);
        let mut splits = s.trim().split_whitespace();
        ($( { splits.next().unwrap().parse::<$t>().unwrap() },)*)
    }}
}

fn is_prime(n: &u64) -> bool {
    if *n == 2 {
        return true;
    }
    let root = (*n as f64).sqrt().ceil() as usize + 1;
    for i in 2..root {
        if *n as usize % i == 0 {
            return false;
        }
    }
    return true;
}

fn primes(n: u64) -> Vec<u64> {
    (2..n).filter(&is_prime).collect()
}

#[allow(unused_must_use)]
#[allow(unused_variables)]
fn solve() {
    let (n,) = input!(usize);
    
    let primes = primes(n as u64);

    let mut res = Vec::with_capacity(n);
    res.push(true); // 0
    res.push(true); // 1

    for i in 2..=n {
        let mut first_win = false;
        for p in &primes {
            let p = *p as usize;
            if res.len() <= p && i < p {
                break;
            }

            if !res[i - p] {
                first_win = true;
                break;
            }
        }

        if first_win {
            res.push(true);
        } else {
            res.push(false);
        }
    }

    if res[n] {
        println!("Win");
    } else {
        println!("Lose");
    }
}

fn main() {
    solve();
}
0