結果

問題 No.36 素数が嫌い!
コンテスト
ユーザー sino
提出日時 2020-04-13 11:09:34
言語 Rust
(1.94.0 + proconio + num + itertools)
コンパイル:
/usr/bin/rustc_custom
実行:
./target/release/main
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,925 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,449 ms
コンパイル使用メモリ 152,384 KB
最終ジャッジ日時 2026-04-11 18:58:18
合計ジャッジ時間 4,370 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
error: cannot explicitly dereference within an implicitly-borrowing pattern
  --> src/main.rs:84:22
   |
84 |         .filter(|(_, &flg)| flg)
   |                      ^ reference pattern not allowed when implicitly borrowing
   |
   = note: for more information, see <https://doc.rust-lang.org/reference/patterns.html#binding-modes>
note: matching on a reference type with a non-reference pattern implicitly borrows the contents
  --> src/main.rs:84:18
   |
84 |         .filter(|(_, &flg)| flg)
   |                  ^^^^^^^^^ this non-reference pattern matches on a reference type `&_`
help: match on the reference with a reference pattern to avoid implicitly borrowing
   |
84 |         .filter(|&(_, &flg)| flg)
   |                  +

error: could not compile `main` (bin "main") due to 1 previous error

ソースコード

diff #
raw source code

#![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