結果

問題 No.12 限定された素数
ユーザー tonyu0tonyu0
提出日時 2019-12-22 14:54:46
言語 Rust
(1.72.1)
結果
AC  
実行時間 45 ms / 5,000 ms
コード長 1,714 bytes
コンパイル時間 1,361 ms
コンパイル使用メモリ 146,888 KB
実行使用メモリ 9,556 KB
最終ジャッジ日時 2023-08-16 01:04:11
合計ジャッジ時間 4,049 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
9,532 KB
testcase_01 AC 44 ms
9,480 KB
testcase_02 AC 44 ms
9,484 KB
testcase_03 AC 42 ms
9,476 KB
testcase_04 AC 43 ms
9,524 KB
testcase_05 AC 45 ms
9,548 KB
testcase_06 AC 45 ms
9,524 KB
testcase_07 AC 45 ms
9,516 KB
testcase_08 AC 44 ms
9,552 KB
testcase_09 AC 44 ms
9,528 KB
testcase_10 AC 44 ms
9,556 KB
testcase_11 AC 44 ms
9,548 KB
testcase_12 AC 45 ms
9,492 KB
testcase_13 AC 44 ms
9,536 KB
testcase_14 AC 44 ms
9,476 KB
testcase_15 AC 45 ms
9,556 KB
testcase_16 AC 44 ms
9,540 KB
testcase_17 AC 41 ms
9,536 KB
testcase_18 AC 42 ms
9,492 KB
testcase_19 AC 43 ms
9,540 KB
testcase_20 AC 43 ms
9,536 KB
testcase_21 AC 43 ms
9,452 KB
testcase_22 AC 41 ms
9,532 KB
testcase_23 AC 44 ms
9,528 KB
testcase_24 AC 44 ms
9,536 KB
testcase_25 AC 42 ms
9,524 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::cmp::max;
use std::io::Read;

fn main() {
    let mut s: String = String::new();
    std::io::stdin().read_to_string(&mut s).ok();
    let mut itr = s.trim().split_whitespace();
    let n: usize = itr.next().unwrap().parse().unwrap();
    let a: Vec<usize> = (0..n)
        .map(|_| itr.next().unwrap().parse().unwrap())
        .collect();
    // 1 <= K <= 5000000
    let mut need: Vec<bool> = vec![false; 10];
    for &i in a.iter() {
        need[i] = true;
    }

    let mut check = vec![false; 5000010];
    let mut prime: Vec<usize> = Vec::new();
    for i in 2..5000001 {
        if !check[i] {
            prime.push(i);
            let mut j = i * 2;
            while j <= 5000000 {
                check[j] = true;
                j += i;
            }
        }
    }
    prime.push(5000001);
    // 足りないチェックと余分チェックが必要。
    let mut l: usize = 1;
    let mut state: Vec<bool> = vec![false; 10];
    let mut ans: i32 = -1;
    for i in 0..prime.len() - 1 {
        let mut tmp = prime[i];
        while tmp > 0 {
            state[tmp % 10 as usize] = true;
            tmp /= 10;
        }

        let mut tarinai = false;
        let mut yobun = false;
        for j in 0..10 {
            if state[j] && !need[j] {
                yobun = true;
            }
            if !state[j] && need[j] {
                tarinai = true;
            }
        }
        if yobun {
            l = prime[i] + 1;
            for j in 0..10 {
                state[j] = false;
            }
            continue;
        }
        if tarinai {
            continue;
        }
        ans = max(ans, (prime[i + 1] - 1 - l) as i32);
    }
    println!("{}", ans);
}
0