結果
| 問題 |
No.3101 Range Eratosthenes Query
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-04-12 17:06:57 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 168 ms / 3,000 ms |
| コード長 | 14,269 bytes |
| コンパイル時間 | 13,798 ms |
| コンパイル使用メモリ | 395,488 KB |
| 実行使用メモリ | 57,476 KB |
| 最終ジャッジ日時 | 2025-04-12 17:08:00 |
| 合計ジャッジ時間 | 18,208 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 24 |
ソースコード
use eratosthenes_sieve::EratosthenesSieve;
use fenwick_tree::FenwickTree;
use proconio::input;
fn main() {
input! {
q: usize,
lr: [(usize, usize); q],
}
let max_r = lr.iter().map(|v| v.1).max().unwrap();
let sieve = EratosthenesSieve::new(max_r);
let calc_second_largest_divisor = |n: usize| {
if n == 1 {
0
} else {
n / sieve.least_factor(n)
}
};
let mut borders = (1..=max_r)
.map(|i| (i, calc_second_largest_divisor(i)))
.collect::<Vec<_>>();
borders.sort_unstable_by_key(|v| v.1);
let mut ft = FenwickTree::<usize>::new(max_r + 1);
let mut ilr = lr
.iter()
.enumerate()
.map(|(i, &(l, r))| (i, l, r))
.collect::<Vec<_>>();
ilr.sort_unstable_by_key(|v| v.1);
let mut ans = vec![0_usize; q];
let mut progress = 0_usize;
for &(qi, l, r) in &ilr {
while progress < borders.len() && borders[progress].1 < l {
ft.add(borders[progress].0, 1);
progress += 1;
}
ans[qi] = ft.sum(l..=r);
}
println!(
"{}",
ans.iter()
.map(|v| v.to_string())
.collect::<Vec<_>>()
.join("\n")
);
}
pub mod eratosthenes_sieve {
//! Implements the Sieve of Eratosthenes.
//!
//! Finds the smallest prime factor of each number placed on the sieve,
//! so it can perform Prime Factorization as well as Primality Test.
#[derive(Debug, Clone)]
pub struct EratosthenesSieve {
sieve: Vec<usize>,
}
impl EratosthenesSieve {
/// Constructs a Sieve of Eratosthenes.
///
/// # Arguments
///
/// * `upper_limit` - The largest number placed on the sieve.
///
/// # Examples
///
/// ```
/// use atcoder8_library::eratosthenes_sieve::EratosthenesSieve;
///
/// let sieve = EratosthenesSieve::new(27);
/// assert_eq!(sieve.prime_factorization(12), vec![(2, 2), (3, 1)]);
/// ```
pub fn new(upper_limit: usize) -> Self {
let mut sieve: Vec<usize> = (0..=upper_limit).collect();
for p in (2..).take_while(|&i| i * i <= upper_limit) {
if sieve[p] != p {
continue;
}
for i in ((p * p)..=upper_limit).step_by(p) {
if sieve[i] == i {
sieve[i] = p;
}
}
}
Self { sieve }
}
/// Returns the least prime factor of `n`.
///
/// However, if `n` is `1`, then `1` is returned.
///
/// # Examples
///
/// ```
/// use atcoder8_library::eratosthenes_sieve::EratosthenesSieve;
///
/// let sieve = EratosthenesSieve::new(27);
/// assert_eq!(sieve.get_least_prime_factor(1), 1);
/// assert_eq!(sieve.get_least_prime_factor(2), 2);
/// assert_eq!(sieve.get_least_prime_factor(6), 2);
/// assert_eq!(sieve.get_least_prime_factor(11), 11);
/// assert_eq!(sieve.get_least_prime_factor(27), 3);
/// ```
pub fn least_factor(&self, n: usize) -> usize {
assert_ne!(n, 0, "`n` must be at least 1.");
self.sieve[n]
}
/// Determines if `n` is prime.
///
/// # Examples
///
/// ```
/// use atcoder8_library::eratosthenes_sieve::EratosthenesSieve;
///
/// let sieve = EratosthenesSieve::new(27);
/// assert!(!sieve.is_prime(1));
/// assert!(sieve.is_prime(2));
/// assert!(!sieve.is_prime(6));
/// assert!(sieve.is_prime(11));
/// assert!(!sieve.is_prime(27));
/// ```
pub fn is_prime(&self, n: usize) -> bool {
n >= 2 && self.sieve[n] == n
}
/// Performs prime factorization of `n`.
///
/// The result of the prime factorization is returned as a
/// list of prime factor and exponent pairs.
///
/// # Examples
///
/// ```
/// use atcoder8_library::eratosthenes_sieve::EratosthenesSieve;
///
/// let sieve = EratosthenesSieve::new(27);
/// assert_eq!(sieve.prime_factorization(1), vec![]);
/// assert_eq!(sieve.prime_factorization(12), vec![(2, 2), (3, 1)]);
/// assert_eq!(sieve.prime_factorization(19), vec![(19, 1)]);
/// assert_eq!(sieve.prime_factorization(27), vec![(3, 3)]);
/// ```
pub fn prime_factorization(&self, n: usize) -> Vec<(usize, usize)> {
assert_ne!(n, 0, "`n` must be at least 1.");
let mut n = n;
let mut factors: Vec<(usize, usize)> = vec![];
while n != 1 {
let p = self.sieve[n];
if factors.is_empty() || factors.last().unwrap().0 != p {
factors.push((p, 1));
} else {
factors.last_mut().unwrap().1 += 1;
}
n /= p;
}
factors
}
/// Creates a list of positive divisors of `n`.
///
/// The positive divisors are listed in ascending order.
///
/// # Examples
///
/// ```
/// use atcoder8_library::eratosthenes_sieve::EratosthenesSieve;
///
/// let sieve = EratosthenesSieve::new(27);
/// assert_eq!(sieve.create_divisor_list(1), vec![1]);
/// assert_eq!(sieve.create_divisor_list(12), vec![1, 2, 3, 4, 6, 12]);
/// assert_eq!(sieve.create_divisor_list(19), vec![1, 19]);
/// assert_eq!(sieve.create_divisor_list(27), vec![1, 3, 9, 27]);
/// ```
pub fn create_divisor_list(&self, n: usize) -> Vec<usize> {
assert_ne!(n, 0, "`n` must be at least 1.");
let prime_factors = self.prime_factorization(n);
let divisor_num: usize = prime_factors.iter().map(|&(_, exp)| exp + 1).product();
let mut divisors = vec![1];
divisors.reserve(divisor_num - 1);
for (p, e) in prime_factors {
let mut add_divisors = vec![];
add_divisors.reserve(divisors.len() * e);
let mut mul = 1;
for _ in 1..=e {
mul *= p;
for &d in divisors.iter() {
add_divisors.push(d * mul);
}
}
divisors.append(&mut add_divisors);
}
divisors.sort_unstable();
divisors
}
/// Calculates the number of positive divisors of `n`.
///
/// # Examples
///
/// ```
/// use atcoder8_library::eratosthenes_sieve::EratosthenesSieve;
///
/// let sieve = EratosthenesSieve::new(27);
/// assert_eq!(sieve.calc_divisor_num(1), 1);
/// assert_eq!(sieve.calc_divisor_num(12), 6);
/// assert_eq!(sieve.calc_divisor_num(19), 2);
/// assert_eq!(sieve.calc_divisor_num(27), 4);
/// ```
pub fn calc_divisor_num(&self, n: usize) -> usize {
assert_ne!(n, 0, "`n` must be at least 1.");
let mut n = n;
let mut divisor_num = 1;
let mut cur_p = None;
let mut cur_exp = 0;
while n != 1 {
let p = self.sieve[n];
if Some(p) == cur_p {
cur_exp += 1;
} else {
divisor_num *= cur_exp + 1;
cur_p = Some(p);
cur_exp = 1;
}
n /= p;
}
divisor_num *= cur_exp + 1;
divisor_num
}
}
}
pub mod fenwick_tree {
//! Processes the following query in `O(log(n))` time
//! for a sequence of numbers with `n` elements:
//! * Update one element
//! * Calculate the sum of the elements of a range
//! * Gets the elements of a number sequence.
use std::ops::{AddAssign, RangeBounds, Sub, SubAssign};
/// # Examples
///
/// ```
/// use atcoder8_library::fenwick_tree::FenwickTree;
///
/// let ft = FenwickTree::from(vec![3, -1, 4, 1, -5, 9, 2]);
/// assert_eq!(ft.sum(2..), 11);
/// ```
#[derive(Debug, Clone)]
pub struct FenwickTree<T>(Vec<T>);
impl<T> From<Vec<T>> for FenwickTree<T>
where
T: Default + Clone + AddAssign<T>,
{
/// # Examples
///
/// ```
/// use atcoder8_library::fenwick_tree::FenwickTree;
///
/// let ft = FenwickTree::from(vec![3, -1, 4, 1, -5, 9, 2]);
/// assert_eq!(ft.sum(2..6), 9);
/// ```
fn from(t: Vec<T>) -> Self {
let mut ft = FenwickTree::new(t.len());
for (i, x) in t.into_iter().enumerate() {
ft.add(i, x);
}
ft
}
}
impl<T> FenwickTree<T> {
/// Constructs a `FenwickTree<T>` with `n` elements.
///
/// Each element is initialized with `T::default()`.
///
/// # Examples
///
/// ```
/// use atcoder8_library::fenwick_tree::FenwickTree;
///
/// let ft = FenwickTree::<i32>::new(5);
/// assert_eq!(ft.sum(..), 0);
/// ```
pub fn new(n: usize) -> Self
where
T: Default + Clone,
{
FenwickTree(vec![T::default(); n])
}
/// Add `x` to the `p`-th element.
///
/// # Examples
///
/// ```
/// use atcoder8_library::fenwick_tree::FenwickTree;
///
/// let mut ft = FenwickTree::from(vec![3, -1, 4, 1, -5, 9, 2]);
/// assert_eq!(ft.sum(2..6), 9);
///
/// ft.add(3, 100);
/// assert_eq!(ft.sum(2..6), 109);
/// ```
pub fn add(&mut self, p: usize, x: T)
where
T: Clone + AddAssign<T>,
{
let FenwickTree(data) = self;
let n = data.len();
assert!(p < n);
let mut p = p + 1;
while p <= n {
data[p - 1] += x.clone();
p += p & p.overflowing_neg().0;
}
}
/// Subtract `x` from the `p`-th element.
///
/// # Examples
///
/// ```
/// use atcoder8_library::fenwick_tree::FenwickTree;
///
/// let mut ft = FenwickTree::<u32>::from(vec![3, 1, 4, 1, 5, 9, 2]);
/// assert_eq!(ft.sum(2..6), 19);
///
/// ft.sub(3, 1);
/// assert_eq!(ft.sum(2..6), 18);
/// ```
pub fn sub(&mut self, p: usize, x: T)
where
T: Clone + SubAssign<T>,
{
let FenwickTree(data) = self;
let n = data.len();
assert!(p < n);
let mut p = p + 1;
while p <= n {
data[p - 1] -= x.clone();
p += p & p.overflowing_neg().0;
}
}
/// Sets `x` to the `p`-th element.
///
/// # Examples
///
/// ```
/// use atcoder8_library::fenwick_tree::FenwickTree;
///
/// let mut ft = FenwickTree::from(vec![3, -1, 4, 1, -5, 9, 2]);
/// assert_eq!(ft.sum(2..6), 9);
///
/// ft.set(3, 100);
/// assert_eq!(ft.sum(2..6), 108);
/// ```
pub fn set(&mut self, p: usize, x: T)
where
T: Default + Clone + AddAssign<T> + Sub<T, Output = T> + SubAssign<T>,
{
let FenwickTree(data) = self;
let n = data.len();
assert!(p < n);
self.sub(p, self.get(p));
self.add(p, x);
}
/// Compute the sum of the range [0, r).
fn inner_sum(&self, r: usize) -> T
where
T: Default + Clone + AddAssign<T>,
{
let mut s = T::default();
let mut r = r;
while r > 0 {
s += self.0[r - 1].clone();
r -= r & r.wrapping_neg();
}
s
}
/// Calculate the total of the range.
///
/// # Examples
///
/// ```
/// use atcoder8_library::fenwick_tree::FenwickTree;
///
/// let ft = FenwickTree::from(vec![3, -1, 4, 1, -5, 9, 2]);
/// assert_eq!(ft.sum(..), 13);
/// assert_eq!(ft.sum(2..), 11);
/// assert_eq!(ft.sum(..6), 11);
/// assert_eq!(ft.sum(2..6), 9);
/// assert_eq!(ft.sum(6..2), 0);
/// ```
pub fn sum<R>(&self, rng: R) -> T
where
T: Default + Clone + AddAssign<T> + Sub<T, Output = T>,
R: RangeBounds<usize>,
{
let n = self.0.len();
let l = match rng.start_bound() {
std::ops::Bound::Included(&start_bound) => start_bound,
std::ops::Bound::Excluded(&start_bound) => start_bound + 1,
std::ops::Bound::Unbounded => 0,
};
let r = match rng.end_bound() {
std::ops::Bound::Included(&end_bound) => end_bound + 1,
std::ops::Bound::Excluded(&end_bound) => end_bound,
std::ops::Bound::Unbounded => n,
};
assert!(l <= n && r <= n);
if l >= r {
T::default()
} else {
self.inner_sum(r) - self.inner_sum(l)
}
}
/// Returns the value of an element in a sequence of numbers.
/// Calculate the total of the range.
///
/// # Examples
///
/// ```
/// use atcoder8_library::fenwick_tree::FenwickTree;
///
/// let ft = FenwickTree::from(vec![3, -1, 4, -1, 5]);
/// assert_eq!(ft.get(2), 4);
/// ```
pub fn get(&self, p: usize) -> T
where
T: Default + Clone + AddAssign<T> + Sub<T, Output = T>,
{
assert!(p < self.0.len());
self.sum(p..=p)
}
}
}