結果
| 問題 |
No.7 プライムナンバーゲーム
|
| コンテスト | |
| ユーザー |
sino
|
| 提出日時 | 2020-03-25 21:53:44 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 5 ms / 5,000 ms |
| コード長 | 1,950 bytes |
| コンパイル時間 | 11,370 ms |
| コンパイル使用メモリ | 378,652 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-10-01 16:32:08 |
| 合計ジャッジ時間 | 12,229 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 17 |
ソースコード
#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::collections::VecDeque;
#[allow(unused_macros)]
macro_rules! read {
([$t:ty] ; $n:expr) =>
((0..$n).map(|_| read!([$t])).collect::<Vec<_>>());
($($t:ty),+ ; $n:expr) =>
((0..$n).map(|_| read!($($t),+)).collect::<Vec<_>>());
([$t:ty]) =>
(rl().split_whitespace().map(|w| w.parse().unwrap()).collect::<Vec<$t>>());
($t:ty) =>
(rl().parse::<$t>().unwrap());
($($t:ty),*) => {{
let buf = rl();
let mut w = buf.split_whitespace();
($(w.next().unwrap().parse::<$t>().unwrap()),*)
}};
}
#[allow(dead_code)]
fn rl() -> String {
let mut buf = String::new();
std::io::stdin().read_line(&mut buf).unwrap();
buf.trim_end().to_owned()
}
trait IteratorExt: Iterator + Sized {
fn vec(self) -> Vec<Self::Item> {
self.collect()
}
}
impl<T: Iterator> IteratorExt for T {}
fn main() {
let n = read!(usize);
let mut table = vec![false; n+1];
let primes = primes(n);
for i in 3..=n {
let x = primes
.iter()
.filter(|&&e| i-1 > e)
.map(|e| i - e)
.filter(|&e| table[e] == false)
.next();
if let Some(_) = x {
table[i] = true;
}
}
println!("{}", match table[n] {
true => "Win",
false => "Lose",
});
}
// n以下の素数一覧
fn primes(n: usize) -> Vec::<usize> {
if n < 2 {
return vec![];
}
let mut table = vec![true; n+1];
table[0] = false;
table[1] = false;
for i in 2..=(n as f64).sqrt() as usize {
if table[i] == false {
continue;
}
let mut j = i*i;
while j <= n {
table[j] = false;
j += i;
}
}
table
.iter()
.enumerate()
.filter(|(_, &flg)| flg)
.map(|(num, _)| num)
.collect()
}
sino