結果

問題 No.1058 素敵な数
ユーザー yukyuyukyu
提出日時 2021-03-14 17:20:52
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 1,517 bytes
コンパイル時間 1,518 ms
コンパイル使用メモリ 175,360 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-24 00:28:17
合計ジャッジ時間 2,124 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::*;
use std::str::FromStr;
use std::collections::HashSet;

fn main() {
    exec(read());
}

fn exec(i: usize) {
    let a = get_prime_list();
    println!("{}", get(a)[i - 1]);
}

fn get_prime_list() -> Vec<i64> {
    let acc: Vec<i64> = vec![];
    let start: i64 = 100001;
    rec_prime(start, acc)
}

fn rec_prime(mut current: i64, mut acc: Vec<i64>) -> Vec<i64> {
    if acc.len() >= 5 {
        return acc;
    } else {
        if is_prime(current) {
            acc.push(current);
        }
        current = current + 1;
        rec_prime(current, acc)
    }
}

fn is_prime(current: i64) -> bool {
    let target = (current as f64).sqrt() as i64 + 1;
    let mut i: i64 = 2;
    while i <= target {
        if current % i == 0 {
            return false;
        }
        i += 1;
    }
    true
}

fn get(l: Vec<i64>) -> Vec<i64> {
    let mut a: Vec<i64> = vec![];
    a.push(1);
    for i in &l {
        for j in &l {
            if i != j {
                a.push((i * j) as i64);
            }
        }
    }
    let v: HashSet<i64> = a.iter().cloned().collect();
    let mut r: Vec<i64> = v.into_iter().collect();
    r.sort();
    r
}

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}
0