結果
| 問題 |
No.6 使いものにならないハッシュ
|
| コンテスト | |
| ユーザー |
cra77756176
|
| 提出日時 | 2022-12-17 19:37:02 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 4 ms / 5,000 ms |
| コード長 | 1,402 bytes |
| コンパイル時間 | 24,322 ms |
| コンパイル使用メモリ | 377,032 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-17 04:43:55 |
| 合計ジャッジ時間 | 14,655 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 32 |
ソースコード
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}");
}
cra77756176