2021/11/14
ABC227G - Divisors of Binomial Coefficient
ついに区間篩を知らなかったことが仇に…
] の素数を求めながら
] の素因数分解もできるの、言われてみればそうなんだけど、すごい。
use crate::mod_int::ModInt; fn main() { let (r, w) = (std::io::stdin(), std::io::stdout()); let mut sc = IO::new(r.lock(), w.lock()); let n: usize = sc.read(); let k: usize = sc.read(); let from = n - k + 1; let to = n; let sq_n = (n as f64).sqrt() as usize + 1; let l = k.max(sq_n); let mut divisors = vec![vec![]; l + 1]; let mut divisors_big = vec![vec![]; k]; for p in 2..=l { if divisors[p].is_empty() { let mut cur = p; while cur <= l { divisors[cur].push(p); cur += p; } let mut cur = (from + p - 1) / p * p; while cur <= to { divisors_big[cur - from].push(p); cur += p; } } } let mut big_primes = 0; let mut count = vec![0; l + 1]; for i in 1..=k { let mut lower = i; for &prime in divisors[i].iter() { while lower % prime == 0 { lower /= prime; count[prime] -= 1; } } let mut upper = n - i + 1; for &prime in divisors_big[n - i + 1 - from].iter() { while upper % prime == 0 { upper /= prime; count[prime] += 1; } } if upper > 1 { big_primes += 1; } } let mut ans = ModInt::from(1); for c in count { assert!(c >= 0, "{}", c); ans *= c + 1; } ans *= ModInt::from(2).pow(big_primes); println!("{}", ans.value()); } pub mod mod_int { type ModInternalNum = i64; thread_local!( static MOD: std::cell::RefCell<ModInternalNum> = std::cell::RefCell::new(0); ); pub fn set_mod_int<T: ToInternalNum>(v: T) { MOD.with(|x| x.replace(v.to_internal_num())); } fn modulo() -> ModInternalNum { 998244353 } pub struct ModInt(ModInternalNum); impl Clone for ModInt { fn clone(&self) -> Self { Self(self.0) } } impl Copy for ModInt {} impl ModInt { fn internal_new(mut v: ModInternalNum) -> Self { let m = modulo(); if v >= m { v %= m; } Self(v) } pub fn internal_pow(&self, mut e: ModInternalNum) -> Self { let mut result = 1; let mut cur = self.0; let modulo = modulo(); while e > 0 { if e & 1 == 1 { result *= cur; result %= modulo; } e >>= 1; cur = (cur * cur) % modulo; } Self(result) } pub fn pow<T>(&self, e: T) -> Self where T: ToInternalNum, { self.internal_pow(e.to_internal_num()) } pub fn value(&self) -> ModInternalNum { self.0 } } pub trait ToInternalNum { fn to_internal_num(&self) -> ModInternalNum; } impl ToInternalNum for ModInt { fn to_internal_num(&self) -> ModInternalNum { self.0 } } macro_rules! impl_primitive { ($primitive:ident) => { impl From<$primitive> for ModInt { fn from(v: $primitive) -> Self { let v = v as ModInternalNum; Self::internal_new(v) } } impl ToInternalNum for $primitive { fn to_internal_num(&self) -> ModInternalNum { *self as ModInternalNum } } }; } impl_primitive!(u8); impl_primitive!(u16); impl_primitive!(u32); impl_primitive!(u64); impl_primitive!(usize); impl_primitive!(i8); impl_primitive!(i16); impl_primitive!(i32); impl_primitive!(i64); impl_primitive!(isize); impl<T: ToInternalNum> std::ops::AddAssign<T> for ModInt { fn add_assign(&mut self, rhs: T) { let mut rhs = rhs.to_internal_num(); let m = modulo(); if rhs >= m { rhs %= m; } self.0 += rhs; if self.0 >= m { self.0 -= m; } } } impl<T: ToInternalNum> std::ops::Add<T> for ModInt { type Output = ModInt; fn add(self, rhs: T) -> Self::Output { let mut res = self; res += rhs; res } } impl<T: ToInternalNum> std::ops::SubAssign<T> for ModInt { fn sub_assign(&mut self, rhs: T) { let mut rhs = rhs.to_internal_num(); let m = modulo(); if rhs >= m { rhs %= m; } if rhs > 0 { self.0 += m - rhs; } if self.0 >= m { self.0 -= m; } } } impl<T: ToInternalNum> std::ops::Sub<T> for ModInt { type Output = Self; fn sub(self, rhs: T) -> Self::Output { let mut res = self; res -= rhs; res } } impl<T: ToInternalNum> std::ops::MulAssign<T> for ModInt { fn mul_assign(&mut self, rhs: T) { let mut rhs = rhs.to_internal_num(); let m = modulo(); if rhs >= m { rhs %= m; } self.0 *= rhs; self.0 %= m; } } impl<T: ToInternalNum> std::ops::Mul<T> for ModInt { type Output = Self; fn mul(self, rhs: T) -> Self::Output { let mut res = self; res *= rhs; res } } impl<T: ToInternalNum> std::ops::DivAssign<T> for ModInt { fn div_assign(&mut self, rhs: T) { let mut rhs = rhs.to_internal_num(); let m = modulo(); if rhs >= m { rhs %= m; } let inv = Self(rhs).internal_pow(m - 2); self.0 *= inv.value(); self.0 %= m; } } impl<T: ToInternalNum> std::ops::Div<T> for ModInt { type Output = Self; fn div(self, rhs: T) -> Self::Output { let mut res = self; res /= rhs; res } } } pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>); impl<R: std::io::Read, W: std::io::Write> IO<R, W> { pub fn new(r: R, w: W) -> IO<R, W> { IO(r, std::io::BufWriter::new(w)) } pub fn write<S: ToString>(&mut self, s: S) { use std::io::Write; self.1.write_all(s.to_string().as_bytes()).unwrap(); } pub fn read<T: std::str::FromStr>(&mut self) -> T { use std::io::Read; let buf = self .0 .by_ref() .bytes() .map(|b| b.unwrap()) .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t') .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t') .collect::<Vec<_>>(); unsafe { std::str::from_utf8_unchecked(&buf) } .parse() .ok() .expect("Parse error.") } pub fn usize0(&mut self) -> usize { self.read::<usize>() - 1 } pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> { (0..n).map(|_| self.read()).collect() } pub fn chars(&mut self) -> Vec<char> { self.read::<String>().chars().collect() } }
ARC067F - Yakiniku Restaurants
[from, to) で利用できるものについて、N x N の2次元平面上にプロットしてみると、長方形領域の集合になっているという問題。直線上を移動する問題を2次元にプロットするところに発想の飛躍があるように感じたが、典型的な考え方かもしれない。ものにしたい。
use crate::sparse_table::SparseTable; use std::collections::VecDeque; fn main() { let (r, w) = (std::io::stdin(), std::io::stdout()); let mut sc = IO::new(r.lock(), w.lock()); let n: usize = sc.read(); let m: usize = sc.read(); let a: Vec<i64> = sc.vec(n - 1); let mut pos = vec![0; n]; for i in 1..n { pos[i] = pos[i - 1] + a[i - 1]; } let b: Vec<Vec<i64>> = (0..n).map(|_| sc.vec(m)).collect(); let mut available = vec![vec![]; n]; for ticket_id in 0..m { let b = (0..n).map(|j| (b[j][ticket_id], j)).collect::<Vec<_>>(); let st = SparseTable::from(&b, (0, n), |a, b| a.max(b)); let mut segments = VecDeque::new(); segments.push_back((0, n)); while let Some((from, to)) = segments.pop_front() { let (x, i) = st.get(from, to); available[from].push((i, ticket_id, x)); if from < i { segments.push_back((from, i)); } if i + 1 < to { segments.push_back((i + 1, to)); } } } let mut ans = 0; let mut events = vec![vec![]; n]; for from in 0..n { for &(i, ticket_id, x) in available[from].iter() { events[i].push((ticket_id, x)); } let mut tickets = vec![0; m]; let mut sum = 0; for i in 0..m { tickets[i] = b[from][i]; sum += tickets[i]; } for to in from..n { for &(i, x) in events[to].iter() { sum -= tickets[i]; tickets[i] = x; sum += tickets[i]; } let d = pos[to] - pos[from]; ans = ans.max(sum - d); } } println!("{}", ans); } pub mod sparse_table { pub struct SparseTable<T, F> { data: Vec<Vec<T>>, op: F, } impl<T, F> SparseTable<T, F> where T: Copy, F: Fn(T, T) -> T, { pub fn from(v: &[T], init: T, op: F) -> Self { let size = v.len().next_power_of_two(); let count = size.trailing_zeros() as usize + 1; let mut data = vec![vec![init; size]; count]; for (i, v) in v.iter().cloned().enumerate() { data[0][i] = v; } for c in 1..count { for i in 0..size { let next = std::cmp::min(size - 1, i + (1 << (c - 1))); data[c][i] = op(data[c - 1][i], data[c - 1][next]); } } Self { data, op } } /// get the result for [l, r) pub fn get(&self, l: usize, r: usize) -> T { assert!(l < r); let length = r - l; if length == 1 { return self.data[0][l]; } let block_size = length.next_power_of_two() >> 1; let c = block_size.trailing_zeros() as usize; let left = self.data[c][l]; let right = self.data[c][r - block_size]; (self.op)(left, right) } } } pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>); impl<R: std::io::Read, W: std::io::Write> IO<R, W> { pub fn new(r: R, w: W) -> IO<R, W> { IO(r, std::io::BufWriter::new(w)) } pub fn write<S: ToString>(&mut self, s: S) { use std::io::Write; self.1.write_all(s.to_string().as_bytes()).unwrap(); } pub fn read<T: std::str::FromStr>(&mut self) -> T { use std::io::Read; let buf = self .0 .by_ref() .bytes() .map(|b| b.unwrap()) .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t') .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t') .collect::<Vec<_>>(); unsafe { std::str::from_utf8_unchecked(&buf) } .parse() .ok() .expect("Parse error.") } pub fn usize0(&mut self) -> usize { self.read::<usize>() - 1 } pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> { (0..n).map(|_| self.read()).collect() } pub fn chars(&mut self) -> Vec<char> { self.read::<String>().chars().collect() } }