結果
| 問題 |
No.3127 Multiple of Twin Prime
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-04-25 21:54:28 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 235 ms / 2,500 ms |
| コード長 | 3,696 bytes |
| コンパイル時間 | 14,548 ms |
| コンパイル使用メモリ | 388,960 KB |
| 実行使用メモリ | 24,772 KB |
| 最終ジャッジ日時 | 2025-04-25 21:54:56 |
| 合計ジャッジ時間 | 18,187 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 12 |
ソースコード
#![allow(unused_imports)]
#![allow(dead_code)]
use std::cell::RefCell;
use std::cmp::{Ordering, Reverse};
use std::collections::btree_map::Entry as BTreeMapEntry;
use std::collections::hash_map::{Entry as HashMapEntry, Entry};
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
use std::fmt::{Debug, Display, Formatter, Write};
use std::hash::Hash;
use std::io::{stdin, BufRead, BufWriter, Write as _};
use std::ops::{Bound, ControlFlow, Range, RangeBounds, RangeInclusive};
use std::rc::Rc;
use std::str::FromStr;
use std::sync::atomic;
use std::sync::atomic::AtomicUsize;
use std::thread::LocalKey;
use std::time::{Duration, Instant};
use std::{array, convert, io, iter, mem, ptr};
pub fn solve(InputObject { cin }: InputObject) {
let mut map = vec![true; 20_000_000];
map[0] = false;
map[1] = false;
for i in 2..map.len() {
if !map[i] {
continue;
}
let mut j = i * i;
while j < map.len() {
map[j] = false;
j += i;
}
}
let kotonoha = map
.windows(3)
.enumerate()
.filter_map(|(i, w)| (w[0] && w[2]).then_some((i, i + 2)))
.collect::<Vec<_>>();
let t: usize = cin.next();
let mut out = String::new();
for _ in 0..t {
let n: usize = cin.next();
let result = kotonoha
.binary_search_by_key(&n, |&(aoi, akane)| aoi * akane)
.unwrap_or_else(|i| i.wrapping_sub(1));
if let Some((akane, aoi)) = kotonoha.get(result) {
writeln!(out, "{}", aoi * akane).unwrap();
} else {
writeln!(out, "-1").unwrap();
}
}
print!("{out}");
}
pub struct InputObject<'a> {
cin: &'a mut ConsoleInput,
}
fn input(cin: &mut ConsoleInput) -> InputObject {
InputObject { cin }
}
pub fn main() {
let mut cin = ConsoleInput::new();
let input = input(&mut cin);
solve(input);
}
// libraries below
pub struct ConsoleInput {
queue: VecDeque<String>,
}
pub trait Input {
fn next<T: FromStr>(&mut self) -> T;
fn next_vec<T: FromStr>(&mut self, n: usize) -> Vec<T> {
(0..n).map(|_| self.next()).collect()
}
}
pub trait Output {
fn write(&self);
}
impl Output for () {
fn write(&self) {}
}
impl Input for ConsoleInput {
fn next<T: FromStr>(&mut self) -> T {
match self.read().parse() {
Ok(value) => value,
_ => panic!("parse error"),
}
}
}
impl ConsoleInput {
fn new() -> Self {
Self {
queue: VecDeque::new(),
}
}
fn read(&mut self) -> String {
loop {
if let Some(s) = self.queue.pop_front() {
return s;
}
let mut s = String::new();
stdin().read_line(&mut s).expect("failed to read");
for s in s.split_ascii_whitespace() {
self.queue.push_back(String::from(s));
}
}
}
}
fn binary_search(range: Range<usize>, f: impl Fn(usize) -> Ordering) -> Result<usize, usize> {
let mut start = range.start;
let mut len = range.len();
if len == 0 {
return Err(0);
}
while len > 1 {
let half = len / 2;
let mid = start + half;
if f(mid) != Ordering::Greater {
start = mid;
}
len -= half;
}
match f(start) {
Ordering::Equal => Ok(start),
Ordering::Less => Err(start + 1),
Ordering::Greater => Err(start),
}
}
fn all_bits() -> impl Iterator<Item = impl Iterator<Item = (usize, bool)>> {
(0usize..).map(|i| (0usize..).map(move |j| (j, (i >> j) & 1 != 0)))
}
#[cfg(test)]
mod tests;