結果
| 問題 |
No.7 プライムナンバーゲーム
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-06-26 19:09:04 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 20 ms / 5,000 ms |
| コード長 | 1,799 bytes |
| コンパイル時間 | 11,824 ms |
| コンパイル使用メモリ | 378,796 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-10-01 16:35:52 |
| 合計ジャッジ時間 | 12,784 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 17 |
ソースコード
use std::io::{stdin, Read, StdinLock};
use std::str::FromStr;
struct Input {
n: usize,
}
fn read_input(cin_lock: &mut StdinLock) -> Input {
let n = next_token(cin_lock);
Input { n }
}
fn generate_prime_numbers(n: usize) -> Vec<usize> {
let mut res = vec![true; n + 1];
res[0] = false;
res[1] = false;
let limit = (n as f64).sqrt() as usize + 1;
for i in 2..limit {
if !res[i] {
continue;
}
for j in 2..n {
if i * j > n {
break;
}
res[i * j] = false;
}
}
return res
.iter()
.enumerate()
.filter(|(_, v)| **v)
.map(|(i, _)| i)
.collect();
}
fn solve1(input: Input) -> bool {
let mut res = vec![false; input.n + 1];
res[0] = true;
res[1] = true;
let prime_numbers = generate_prime_numbers(input.n);
for i in 0..input.n {
for j in 0..prime_numbers.len() {
let p = prime_numbers[j];
if i + p > input.n {
break;
}
if res[i + p] {
continue;
}
if !res[i] {
res[i + p] = true
}
}
}
return res[input.n];
}
fn solve(input: Input) {
println!("{}", if solve1(input) { "Win" } else { "Lose" })
}
fn next_token<T: FromStr>(cin_lock: &mut StdinLock) -> T {
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>()
.parse::<T>()
.ok()
.unwrap()
}
fn main() {
let cin = stdin();
let mut cin_lock = cin.lock();
let input = read_input(&mut cin_lock);
solve(input);
}