結果

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

テストケース

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

ソースコード

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() >= 6 {
        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 {
            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