結果

問題 No.843 Triple Primes
ユーザー guriceringuricerin
提出日時 2019-07-19 11:19:46
言語 Rust
(1.72.1)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,436 bytes
コンパイル時間 1,261 ms
コンパイル使用メモリ 154,240 KB
実行使用メモリ 4,508 KB
最終ジャッジ日時 2023-08-26 21:13:24
合計ジャッジ時間 45,688 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 TLE -
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 3 ms
4,384 KB
testcase_07 AC 1,583 ms
4,380 KB
testcase_08 AC 1,897 ms
4,384 KB
testcase_09 TLE -
testcase_10 AC 1,707 ms
4,384 KB
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 AC 1,665 ms
4,376 KB
testcase_16 AC 1,705 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 211 ms
4,376 KB
testcase_21 AC 71 ms
4,376 KB
testcase_22 AC 799 ms
4,380 KB
testcase_23 AC 859 ms
4,380 KB
testcase_24 AC 238 ms
4,376 KB
testcase_25 AC 141 ms
4,376 KB
testcase_26 TLE -
testcase_27 AC 14 ms
4,380 KB
testcase_28 TLE -
testcase_29 AC 274 ms
4,380 KB
testcase_30 TLE -
testcase_31 AC 31 ms
4,380 KB
testcase_32 AC 12 ms
4,384 KB
testcase_33 AC 335 ms
4,380 KB
testcase_34 AC 690 ms
4,384 KB
testcase_35 TLE -
testcase_36 AC 118 ms
4,376 KB
testcase_37 AC 1,496 ms
4,380 KB
testcase_38 AC 1,089 ms
4,380 KB
testcase_39 TLE -
testcase_40 AC 1 ms
4,376 KB
testcase_41 AC 0 ms
4,376 KB
testcase_42 AC 1,773 ms
4,376 KB
testcase_43 TLE -
権限があれば一括ダウンロードができます

ソースコード

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 {
        for j in 0..lim {
            let r = primes[i];
            let p = primes[j];
            if r > n as i64 || p > n as i64 {
                continue;
            }
            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