結果

問題 No.6 使いものにならないハッシュ
ユーザー cra77756176cra77756176
提出日時 2022-12-17 19:37:02
言語 Rust
(1.77.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,402 bytes
コンパイル時間 12,909 ms
コンパイル使用メモリ 382,008 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-28 12:58:58
合計ジャッジ時間 14,385 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 3 ms
6,812 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 3 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 3 ms
6,940 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 3 ms
6,944 KB
testcase_13 AC 3 ms
6,940 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 3 ms
6,944 KB
testcase_18 AC 4 ms
6,940 KB
testcase_19 AC 3 ms
6,940 KB
testcase_20 AC 3 ms
6,940 KB
testcase_21 AC 1 ms
6,940 KB
testcase_22 AC 3 ms
6,940 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 3 ms
6,940 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 3 ms
6,940 KB
testcase_27 AC 3 ms
6,944 KB
testcase_28 AC 2 ms
6,940 KB
testcase_29 AC 3 ms
6,944 KB
testcase_30 AC 2 ms
6,940 KB
testcase_31 AC 3 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

const fn hash(mut n: usize) -> usize {
    while n >= 10 {
        let mut new_n = 0;
        while n > 0 {
            new_n += n % 10;
            n /= 10;
        }
        n = new_n;
    }
    n
}

fn get_primes(max: usize) -> Vec<usize> {
    let mut is_prime = vec![true; max + 1];
    is_prime[0] = false;
    is_prime[1] = false;

    for n in 2..=max {
        if !is_prime[n] {
            continue;
        }
        if n * n > max {
            break;
        }
        for i in ((n * n)..=max).step_by(n) {
            is_prime[i] = false;
        }
    }

    (0..=max).filter(|&n| is_prime[n]).collect()
}

fn main() {
    let mut xx = String::new();
    std::io::Read::read_to_string(&mut std::io::stdin(), &mut xx).ok();
    let xx: Vec<usize> = xx.split_whitespace().flat_map(str::parse).collect();

    let primes: Vec<usize> = get_primes(xx[1])
        .into_iter()
        .filter(|&n| n >= xx[0])
        .collect();
    let hashes: Vec<usize> = primes.iter().map(|&n| hash(n)).collect();

    let mut right = 0;
    let mut max_len = 0;
    let mut max_start = 0;

    for left in 0..hashes.len() {
        while right < hashes.len() && !&hashes[left..right].contains(&hashes[right]) {
            right += 1;
        }
        if right - left >= max_len {
            max_len = right - left;
            max_start = primes[left];
        }
    }

    println!("{max_start}");
}
0