結果

問題 No.843 Triple Primes
ユーザー guriceringuricerin
提出日時 2019-07-19 11:21:56
言語 Rust
(1.72.1)
結果
AC  
実行時間 1,262 ms / 2,000 ms
コード長 3,470 bytes
コンパイル時間 699 ms
コンパイル使用メモリ 154,140 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-08-26 21:14:02
合計ジャッジ時間 26,067 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1,262 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 823 ms
4,376 KB
testcase_08 AC 972 ms
4,376 KB
testcase_09 AC 1,235 ms
4,376 KB
testcase_10 AC 908 ms
4,376 KB
testcase_11 AC 1,077 ms
4,376 KB
testcase_12 AC 1,185 ms
4,376 KB
testcase_13 AC 1,144 ms
4,380 KB
testcase_14 AC 1,150 ms
4,380 KB
testcase_15 AC 857 ms
4,376 KB
testcase_16 AC 894 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 121 ms
4,380 KB
testcase_21 AC 41 ms
4,376 KB
testcase_22 AC 436 ms
4,376 KB
testcase_23 AC 464 ms
4,376 KB
testcase_24 AC 130 ms
4,376 KB
testcase_25 AC 80 ms
4,380 KB
testcase_26 AC 1,246 ms
4,376 KB
testcase_27 AC 9 ms
4,380 KB
testcase_28 AC 1,158 ms
4,376 KB
testcase_29 AC 150 ms
4,376 KB
testcase_30 AC 1,213 ms
4,380 KB
testcase_31 AC 17 ms
4,380 KB
testcase_32 AC 7 ms
4,376 KB
testcase_33 AC 172 ms
4,504 KB
testcase_34 AC 367 ms
4,376 KB
testcase_35 AC 1,243 ms
4,380 KB
testcase_36 AC 65 ms
4,376 KB
testcase_37 AC 759 ms
4,380 KB
testcase_38 AC 580 ms
4,376 KB
testcase_39 AC 1,178 ms
4,376 KB
testcase_40 AC 1 ms
4,380 KB
testcase_41 AC 1 ms
4,376 KB
testcase_42 AC 956 ms
4,376 KB
testcase_43 AC 1,090 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// Original: https://github.com/tanakh/competitive-rs
#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, [ $t:tt ]) => {
        {
            let len = read_value!($next, usize);
            (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
        }
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(dead_code)]
fn chmin<T>(x: &mut T, y: T) -> bool
where
    T: PartialOrd + Copy,
{
    *x > y && {
        *x = y;
        true
    }
}

#[allow(dead_code)]
fn chmax<T>(x: &mut T, y: T) -> bool
where
    T: PartialOrd + Copy,
{
    *x < y && {
        *x = y;
        true
    }
}

#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, VecDeque};

fn main() {
    input! {
        n:usize
    }
    let sieve = eratosthenes_sieve(n);
    let mut primes = vec![];
    for (i, &v) in sieve.iter().enumerate() {
        if v {
            primes.push(i as i64);
        }
    }

    // 愚直解
    let mut cnt = 0;
    let lim = primes.len();
    for i in 0..lim {
        let r = primes[i];
        if n < r as usize {
            break;
        }
        for j in 0..lim {
            let p = primes[j];
            if p > n as i64 {
                break;
            }
            let q = r * r - p;
            if 0 <= q && q <= n as i64 && sieve[q as usize] {
                cnt += 1;
            }
        }
    }
    println!("{}", cnt);
}

pub fn eratosthenes_sieve(n: usize) -> Vec<bool> {
    let n = n + 1;
    let mut res = vec![true; n];
    res[0] = false;
    res[1] = false;
    for i in 2..n {
        if !res[i] {
            continue;
        }
        for j in 2..n {
            if i * j >= n {
                break;
            }
            // 素数の倍数は素数ではない
            res[i * j] = false;
        }
    }
    res
}
0