結果

問題 No.36 素数が嫌い!
ユーザー sinosino
提出日時 2020-04-13 11:09:34
言語 Rust
(1.77.0)
結果
AC  
実行時間 64 ms / 5,000 ms
コード長 1,925 bytes
コンパイル時間 4,618 ms
コンパイル使用メモリ 162,488 KB
実行使用メモリ 17,860 KB
最終ジャッジ日時 2023-10-24 20:29:44
合計ジャッジ時間 6,266 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
8,700 KB
testcase_01 AC 49 ms
13,256 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 20 ms
6,636 KB
testcase_12 AC 64 ms
17,860 KB
testcase_13 AC 63 ms
17,832 KB
testcase_14 AC 38 ms
10,972 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 0 ms
4,348 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 1 ms
4,348 KB
testcase_19 AC 24 ms
7,740 KB
testcase_20 AC 63 ms
17,832 KB
testcase_21 AC 47 ms
13,112 KB
testcase_22 AC 49 ms
13,336 KB
testcase_23 AC 29 ms
8,780 KB
testcase_24 AC 31 ms
9,552 KB
testcase_25 AC 31 ms
9,468 KB
testcase_26 AC 37 ms
10,836 KB
testcase_27 AC 54 ms
14,196 KB
testcase_28 AC 36 ms
10,460 KB
testcase_29 AC 61 ms
17,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::collections::HashMap;
use std::collections::HashSet;

#[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()
}

fn main() {
    let n = read!(u64);

    let sqrtn = (n as f64).sqrt() as usize;
    let primes = primes(sqrtn);

    let num = primes
        .into_iter()
        .fold(0, |acc, p| {
            let mut c = 1;
            let mut pp = p as u64;
            loop {
                if (n % pp == 0) && (n != pp) {
                    c += 1;
                    pp *= p as u64;
                }
                else {
                    return acc + c - 1;
                }
            }
        });

    if num >= 2 {
        println!("YES");
    }
    else {
        println!("NO");
    }
}

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()
}
0