結果
問題 | No.1411 Hundreds of Conditions Sequences |
ユーザー | ngtkana |
提出日時 | 2020-12-02 03:53:32 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 647 ms / 2,000 ms |
コード長 | 43,009 bytes |
コンパイル時間 | 14,010 ms |
コンパイル使用メモリ | 380,312 KB |
実行使用メモリ | 57,552 KB |
最終ジャッジ日時 | 2024-09-22 11:12:34 |
合計ジャッジ時間 | 52,981 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 404 ms
40,984 KB |
testcase_01 | AC | 413 ms
40,984 KB |
testcase_02 | AC | 403 ms
40,996 KB |
testcase_03 | AC | 415 ms
40,916 KB |
testcase_04 | AC | 439 ms
40,944 KB |
testcase_05 | AC | 411 ms
40,920 KB |
testcase_06 | AC | 414 ms
40,920 KB |
testcase_07 | AC | 403 ms
40,944 KB |
testcase_08 | AC | 405 ms
40,968 KB |
testcase_09 | AC | 397 ms
40,976 KB |
testcase_10 | AC | 403 ms
40,860 KB |
testcase_11 | AC | 398 ms
40,976 KB |
testcase_12 | AC | 584 ms
52,068 KB |
testcase_13 | AC | 528 ms
49,352 KB |
testcase_14 | AC | 475 ms
45,416 KB |
testcase_15 | AC | 566 ms
49,252 KB |
testcase_16 | AC | 472 ms
41,668 KB |
testcase_17 | AC | 544 ms
49,192 KB |
testcase_18 | AC | 600 ms
52,164 KB |
testcase_19 | AC | 599 ms
52,948 KB |
testcase_20 | AC | 494 ms
45,400 KB |
testcase_21 | AC | 615 ms
53,908 KB |
testcase_22 | AC | 477 ms
45,828 KB |
testcase_23 | AC | 579 ms
52,176 KB |
testcase_24 | AC | 428 ms
41,008 KB |
testcase_25 | AC | 589 ms
52,176 KB |
testcase_26 | AC | 555 ms
49,124 KB |
testcase_27 | AC | 497 ms
45,612 KB |
testcase_28 | AC | 549 ms
50,188 KB |
testcase_29 | AC | 430 ms
42,332 KB |
testcase_30 | AC | 538 ms
47,852 KB |
testcase_31 | AC | 531 ms
47,300 KB |
testcase_32 | AC | 631 ms
53,860 KB |
testcase_33 | AC | 615 ms
53,956 KB |
testcase_34 | AC | 617 ms
53,980 KB |
testcase_35 | AC | 647 ms
53,968 KB |
testcase_36 | AC | 620 ms
54,024 KB |
testcase_37 | AC | 614 ms
53,988 KB |
testcase_38 | AC | 623 ms
53,964 KB |
testcase_39 | AC | 607 ms
53,956 KB |
testcase_40 | AC | 612 ms
53,900 KB |
testcase_41 | AC | 622 ms
53,960 KB |
testcase_42 | AC | 596 ms
53,976 KB |
testcase_43 | AC | 603 ms
53,968 KB |
testcase_44 | AC | 602 ms
53,984 KB |
testcase_45 | AC | 589 ms
53,896 KB |
testcase_46 | AC | 596 ms
53,900 KB |
testcase_47 | AC | 592 ms
53,980 KB |
testcase_48 | AC | 636 ms
53,956 KB |
testcase_49 | AC | 617 ms
54,072 KB |
testcase_50 | AC | 610 ms
53,940 KB |
testcase_51 | AC | 613 ms
53,964 KB |
testcase_52 | AC | 598 ms
53,980 KB |
testcase_53 | AC | 600 ms
53,964 KB |
testcase_54 | AC | 647 ms
54,020 KB |
testcase_55 | AC | 617 ms
54,120 KB |
testcase_56 | AC | 619 ms
54,188 KB |
testcase_57 | AC | 614 ms
54,100 KB |
testcase_58 | AC | 606 ms
54,100 KB |
testcase_59 | AC | 590 ms
53,980 KB |
testcase_60 | AC | 589 ms
54,024 KB |
testcase_61 | AC | 601 ms
54,004 KB |
testcase_62 | AC | 569 ms
48,424 KB |
testcase_63 | AC | 635 ms
57,552 KB |
コンパイルメッセージ
warning: unused import: `accumulate` --> src/main.rs:321:32 | 321 | pub use self::accumulate::{accumulate, Accumulate}; | ^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default warning: unused import: `cartesian_product` --> src/main.rs:323:39 | 323 | pub use self::cartesian_product::{cartesian_product, CartesianProduct}; | ^^^^^^^^^^^^^^^^^ warning: unused import: `self::format_intersparse::format_intersparse` --> src/main.rs:324:13 | 324 | pub use self::format_intersparse::format_intersparse; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ warning: unused import: `intersperse` --> src/main.rs:326:33 | 326 | pub use self::intersperse::{intersperse, Intersperse}; | ^^^^^^^^^^^ warning: unused imports: `MulStep`, `mul_step` --> src/main.rs:327:30 | 327 | pub use self::mul_step::{mul_step, MulStep}; | ^^^^^^^^ ^^^^^^^ warning: unused imports: `RepeatWith`, `repeat_with` --> src/main.rs:328:33 | 328 | pub use self::repeat_with::{repeat_with, RepeatWith}; | ^^^^^^^^^^^ ^^^^^^^^^^ warning: unused imports: `Step`, `step` --> src/main.rs:329:26 | 329 | pub use self::step::{step, Step}; | ^^^^ ^^^^ warning: unused imports: `Leaf`, `Tuple`, `VecLen` --> src/main.rs:1176:37 | 1176 | pub use self::multi_token::{Leaf, Parser, ParserTuple, RawTuple, Tuple, VecLen}; | ^^^^ ^^^^^ ^^^^^^ warning: unused import: `with_str` --> src/main.rs:1431:35 | 1431 | pub use self::i::{with_stdin, with_str}; | ^^^^^^^^ warning: unused imports: `ParserTuple`, `Parser`, `RawTuple`, `Token`, `Usize1` --> src/main.rs:1434:28 |
ソースコード
use std::collections::HashMap; type Fp = fp::F1000000007; fn main() { let mut buf = ngtio::with_stdin(); let n = buf.usize(); let a = buf.vec::<u32>(n); let sp = small_fp::SmallestPrimeFactors::new(10_000_001); let mut map = HashMap::new(); a.iter() .map(|&x| sp.factorize(x)) .enumerate() .map(|(i, div)| div.into_iter().map(move |(p, e)| (i, p, e))) .flatten() .for_each(|(i, p, e)| map.entry(p).or_insert_with(Vec::new).push((e, i))); let cnt = { let mut cnt = vec![Fp::new(1); n]; let mut orig_cnt = Fp::new(1); for (&p, v) in map.iter_mut() { let (pos, &(first, i)) = v.iter().enumerate().max_by_key(|(_, &(e, _))| e).unwrap(); orig_cnt *= Fp::new(p as i64).pow(first as u64); let second = v[..pos] .iter() .chain(v[pos + 1..].iter()) .map(|&(e, _)| e) .max() .unwrap_or(0); if first != second { cnt[i] /= Fp::new(p as i64).pow((first - second) as u64); } } cnt.iter_mut().for_each(|x| *x *= orig_cnt); cnt }; let mut b = std::iter::once(1) .chain(a.iter().copied()) .map(|x| Fp::new(x as i64)) .collect::<Vec<_>>(); let mut c = std::iter::once(1) .chain(a.iter().rev().copied()) .map(|x| Fp::new(x as i64)) .collect::<Vec<_>>(); accum::mul(&mut b); accum::mul(&mut c); for i in 0..n { let ans = b[i] * c[n - 1 - i] - cnt[i]; println!("{}", ans); } } // dbg {{{ #[allow(dead_code)] mod dbg { #[macro_export] macro_rules! lg { () => { $crate::eprintln!("[{}:{}]", $crate::file!(), $crate::line!()); }; ($val:expr) => { match $val { tmp => { eprintln!("[{}:{}] {} = {:?}", file!(), line!(), stringify!($val), &tmp); tmp } } }; ($val:expr,) => { $crate::lg!($val) }; ($($val:expr),+ $(,)?) => { ($($crate::lg!($val)),+,) }; } #[macro_export] macro_rules! lg_nl { () => { eprintln!("[{}:{}]", $crate::file!(), $crate::line!()); }; ($val:expr) => { match $val { tmp => { eprintln!("[{}:{}] {}:\n{:?}", file!(), line!(), stringify!($val), tmp); tmp } }; }; } #[macro_export] macro_rules! msg { () => { compile_error!(); }; ($msg:expr) => { $crate::eprintln!("[{}:{}][{}]", $crate::file!(), $crate::line!(), $msg); }; ($msg:expr, $val:expr) => { match $val { tmp => { eprintln!("[{}:{}][{}] {} = {:?}", file!(), line!(), $msg, stringify!($val), &tmp); tmp } } }; ($msg:expr, $val:expr,) => { msg!($msg, $val) }; ($msg:expr, $($val:expr),+ $(,)?) => { ($(msg!($msg, $val)),+,) }; } #[macro_export] macro_rules! tabular { ($val:expr) => { $crate::lg_nl!($crate::Tabular($val)) }; } #[macro_export] macro_rules! boolean_table { ($val:expr) => { $crate::lg_nl!($crate::BooleanTable($val)); }; } #[macro_export] macro_rules! boolean_slice { ($val:expr) => { $crate::lg!($crate::BooleanSlice($val)); }; } use std::fmt::{Debug, Formatter}; #[derive(Clone)] pub struct Tabular<'a, T: Debug>(pub &'a [T]); impl<'a, T: Debug> Debug for Tabular<'a, T> { fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { for i in 0..self.0.len() { writeln!(f, "{:2} | {:?}", i, &self.0[i])?; } Ok(()) } } #[derive(Clone)] pub struct BooleanTable<'a>(pub &'a [Vec<bool>]); impl<'a> Debug for BooleanTable<'a> { fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { for i in 0..self.0.len() { writeln!(f, "{:2} | {:?}", i, BooleanSlice(&self.0[i]))?; } Ok(()) } } #[derive(Clone)] pub struct BooleanSlice<'a>(pub &'a [bool]); impl<'a> Debug for BooleanSlice<'a> { fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { write!( f, "{}", self.0 .iter() .map(|&b| if b { "1 " } else { "0 " }) .collect::<String>() )?; Ok(()) } } } // }}} // small_fp {{{ #[allow(dead_code)] mod small_fp { use crate::fp::{Fp, Mod}; pub struct SmallInversions<T>(Vec<Fp<T>>); impl<T: Mod> SmallInversions<T> { pub fn new(n: u32) -> Self { let mut vec = vec![Fp::new(1); n as usize]; for x in (2..n).map(|x| x as i64) { let q = Fp::<T>::r#mod() / x; let r = Fp::<T>::r#mod() % x; vec[x as usize] = -Fp::new(q) * vec[r as usize]; } Self(vec) } pub fn inv_small(&self, n: u32) -> Fp<T> { self.0[n as usize] } pub fn inv_large(&self, n: i64) -> Fp<T> { if 0 <= n && n < self.0.len() as i64 { self.inv_small(n as u32) } else { let n = Fp::<T>::new(n).into_inner(); let m = Fp::<T>::r#mod(); let q = m / n; let r = m % n; -Fp::new(q) * self.inv_large(r) } } } pub struct SmallestPrimeFactors(Vec<u32>); impl SmallestPrimeFactors { pub fn new(n: u32) -> Self { let mut vec = (0..n).collect::<Vec<_>>(); for p in (2..).take_while(|&p| p * p < n) { let mut i = 2 * p; while i < n { if vec[i as usize] == i { vec[i as usize] = p } i += p; } } Self(vec) } pub fn get(&self, n: u32) -> u32 { assert!(n != 0 && n < self.0.len() as u32); self.0[n as usize] } pub fn factorize(&self, mut n: u32) -> Vec<(u32, u32)> { let mut ans = Vec::new(); while n != 1 { let p = self.0[n as usize]; if ans.last().map_or(true, |&(p1, _)| p1 != p) { ans.push((p, 0)); } ans.last_mut().unwrap().1 += 1; n /= p; } ans } } } // }}} // accum {{{ #[allow(dead_code)] mod accum { use std::{ cmp::Ord, ops::{ AddAssign, BitAndAssign, BitOrAssign, BitXorAssign, DivAssign, MulAssign, SubAssign, }, }; pub fn add<T: Copy + AddAssign>(a: &mut [T]) { for_each_mut(a, |&mut x, y| *y += x); } pub fn add_inv<T: Copy + SubAssign>(a: &mut [T]) { rfor_each_mut(a, |&mut x, y| *y -= x); } pub fn mul<T: Copy + MulAssign>(a: &mut [T]) { for_each_mut(a, |&mut x, y| *y *= x); } pub fn mul_inv<T: Copy + DivAssign>(a: &mut [T]) { rfor_each_mut(a, |&mut x, y| *y /= x); } // -- ord pub fn min<T: Copy + Ord>(a: &mut [T]) { for_each_mut(a, |&mut x, y| *y = x.min(*y)); } pub fn max<T: Copy + Ord>(a: &mut [T]) { for_each_mut(a, |&mut x, y| *y = x.max(*y)); } // -- bit pub fn xor<T: Copy + BitXorAssign>(a: &mut [T]) { for_each_mut(a, |&mut x, y| *y ^= x); } pub fn xor_inv<T: Copy + BitXorAssign>(a: &mut [T]) { rfor_each_mut(a, |&mut x, y| *y ^= x); } pub fn or<T: Copy + BitOrAssign>(a: &mut [T]) { for_each_mut(a, |&mut x, y| *y |= x); } pub fn and<T: Copy + BitAndAssign>(a: &mut [T]) { for_each_mut(a, |&mut x, y| *y &= x); } // -- for_each pub fn for_each<T>(a: &[T], mut f: impl FnMut(&T, &T)) { if !a.is_empty() { for i in 1..a.len() { let (left, right) = a.split_at(i); f(left.last().unwrap(), right.first().unwrap()); } } } pub fn rfor_each<T>(a: &[T], mut f: impl FnMut(&T, &T)) { if !a.is_empty() { for i in (1..a.len()).rev() { let (left, right) = a.split_at(i); f(left.last().unwrap(), right.first().unwrap()); } } } pub fn for_each_mut<T>(a: &mut [T], mut f: impl FnMut(&mut T, &mut T)) { if !a.is_empty() { for i in 1..a.len() { let (left, right) = a.split_at_mut(i); f(left.last_mut().unwrap(), right.first_mut().unwrap()); } } } pub fn rfor_each_mut<T>(a: &mut [T], mut f: impl FnMut(&mut T, &mut T)) { if !a.is_empty() { for i in (1..a.len()).rev() { let (left, right) = a.split_at_mut(i); f(left.last_mut().unwrap(), right.first_mut().unwrap()); } } } } // }}} // seq {{{ #[allow(dead_code)] mod seq { #![warn(missing_docs, missing_doc_code_examples)] pub use self::accumulate::{accumulate, Accumulate}; pub use self::adjacent::{adjacent, Adjacent}; pub use self::cartesian_product::{cartesian_product, CartesianProduct}; pub use self::format_intersparse::format_intersparse; pub use self::grid_next::{grid_next, GridNext}; pub use self::intersperse::{intersperse, Intersperse}; pub use self::mul_step::{mul_step, MulStep}; pub use self::repeat_with::{repeat_with, RepeatWith}; pub use self::step::{step, Step}; use std::{fmt, ops}; impl<I: Iterator> Seq for I {} pub trait Seq: Iterator + Sized { fn adjacent(self) -> Adjacent<Self, Self::Item> where Self::Item: Clone, { adjacent(self) } fn grid_next(self, ij: (usize, usize), h: usize, w: usize) -> GridNext<Self> where Self: Iterator<Item = (i64, i64)>, { grid_next(self, ij, h, w) } fn cartesian_product<J>(self, other: J) -> CartesianProduct<Self, J::IntoIter> where Self: Sized, Self::Item: Clone, J: IntoIterator, J::IntoIter: Clone, { cartesian_product::cartesian_product(self, other.into_iter()) } fn accumulate<T>(self, init: T) -> Accumulate<Self, T> where T: Clone + ops::AddAssign<Self::Item>, { accumulate::accumulate(self, init) } fn intersperse(self, elt: Self::Item) -> Intersperse<Self> { intersperse::intersperse(self, elt) } fn format_intersparse<T>(self, separator: T) -> String where Self::Item: fmt::Display, T: fmt::Display, { self.map(|x| format!("{}", x)) .intersperse(format!("{}", separator)) .collect::<String>() } } mod adjacent { #[allow(missing_docs)] pub fn adjacent<I, T>(mut iter: I) -> Adjacent<I, T> where I: Iterator<Item = T>, T: Clone, { if let Some(first) = iter.next() { Adjacent { iter, prv: Some(first), } } else { Adjacent { iter, prv: None } } } #[allow(missing_docs)] pub struct Adjacent<I, T> where I: Iterator<Item = T>, { iter: I, prv: Option<T>, } impl<I, T> Iterator for Adjacent<I, T> where I: Iterator<Item = T>, T: Clone, { type Item = (T, T); fn next(&mut self) -> Option<(T, T)> { self.prv.as_ref().cloned().and_then(|first| { self.iter.next().map(|second| { self.prv = Some(second.clone()); (first, second) }) }) } } } mod grid_next { #[allow(missing_docs)] pub fn grid_next<T>(difference: T, ij: (usize, usize), h: usize, w: usize) -> GridNext<T> where T: Iterator<Item = (i64, i64)>, { GridNext { i: ij.0 as i64, j: ij.1 as i64, h: h as i64, w: w as i64, difference, } } #[allow(missing_docs)] #[derive(Debug, Clone)] pub struct GridNext<T> { i: i64, j: i64, h: i64, w: i64, difference: T, } impl<T> Iterator for GridNext<T> where T: Iterator<Item = (i64, i64)>, { type Item = (usize, usize); fn next(&mut self) -> Option<(usize, usize)> { while let Some((di, dj)) = self.difference.next() { let ni = self.i + di; let nj = self.j + dj; if 0 <= ni && ni < self.h && 0 <= nj && nj < self.w { return Some((ni as usize, nj as usize)); } } None } } } mod step { #[allow(missing_docs)] pub fn step<T, U>(init: T, step: U) -> Step<T, U> where T: Copy, U: Copy, T: ::std::ops::Add<U, Output = T>, { Step { now: init, step } } #[allow(missing_docs)] #[derive(Debug, Clone)] pub struct Step<T, U> { now: T, step: U, } #[allow(missing_docs)] impl<T, U> Iterator for Step<T, U> where T: Copy, U: Copy, T: ::std::ops::Add<U, Output = T>, { type Item = T; fn next(&mut self) -> Option<T> { let next = self.now + self.step; Some(::std::mem::replace(&mut self.now, next)) } } } mod mul_step { #[allow(missing_docs)] pub fn mul_step<T, U>(init: T, step: U) -> MulStep<T, U> where T: Copy, U: Copy, T: ::std::ops::Mul<U, Output = T>, { MulStep { now: init, step } } #[allow(missing_docs)] #[derive(Debug, Clone)] pub struct MulStep<T, U> { now: T, step: U, } #[allow(missing_docs)] impl<T, U> Iterator for MulStep<T, U> where T: Copy, U: Copy, T: ::std::ops::Mul<U, Output = T>, { type Item = T; fn next(&mut self) -> Option<T> { let next = self.now * self.step; Some(::std::mem::replace(&mut self.now, next)) } } } mod repeat_with { #[allow(missing_docs)] pub fn repeat_with<A, F: FnMut() -> A>(repeater: F) -> RepeatWith<F> { RepeatWith { repeater } } #[allow(missing_docs)] #[derive(Debug, Clone)] pub struct RepeatWith<F> { repeater: F, } impl<A, F: FnMut() -> A> Iterator for RepeatWith<F> { type Item = A; #[inline] fn next(&mut self) -> Option<A> { Some((self.repeater)()) } #[inline] fn size_hint(&self) -> (usize, Option<usize>) { (::std::usize::MAX, None) } } } mod accumulate { use super::*; #[allow(missing_docs)] #[derive(Debug, Clone)] pub struct Accumulate<I, T> { prev: Option<T>, iter: I, } #[allow(missing_docs)] pub fn accumulate<I, T>(iter: I, init: T) -> Accumulate<I, T> where I: Iterator, T: Clone + ops::AddAssign<I::Item>, { Accumulate { prev: Some(init), iter, } } impl<I, T> Iterator for Accumulate<I, T> where I: Iterator, T: Clone + ops::AddAssign<I::Item>, { type Item = T; fn next(&mut self) -> Option<T> { let res = self.prev.clone(); if let Some(prev) = self.prev.as_mut() { if let Some(next) = self.iter.next() { *prev += next; } else { self.prev = None; } } res } fn size_hint(&self) -> (usize, Option<usize>) { size_hint::add_scalar(self.iter.size_hint(), 1) } } } mod cartesian_product { #[allow(missing_docs)] #[derive(Debug, Clone)] pub struct CartesianProduct<I, J> where I: Iterator, { a: I, a_cur: Option<I::Item>, b: J, b_orig: J, } #[allow(missing_docs)] pub fn cartesian_product<I, J>(mut i: I, j: J) -> CartesianProduct<I, J> where I: Iterator, J: Clone + Iterator, I::Item: Clone, { CartesianProduct { a_cur: i.next(), a: i, b_orig: j.clone(), b: j, } } impl<I, J> Iterator for CartesianProduct<I, J> where I: Iterator, J: Clone + Iterator, I::Item: Clone, { type Item = (I::Item, J::Item); fn next(&mut self) -> Option<(I::Item, J::Item)> { let elt_b = match self.b.next() { None => { self.b = self.b_orig.clone(); match self.b.next() { None => return None, Some(x) => { self.a_cur = self.a.next(); x } } } Some(x) => x, }; match self.a_cur { None => None, Some(ref a) => Some((a.clone(), elt_b)), } } fn size_hint(&self) -> (usize, Option<usize>) { let has_cur = self.a_cur.is_some() as usize; // Not ExactSizeIterator because size may be larger than usize let (b_min, b_max) = self.b.size_hint(); // Compute a * b_orig + b for both lower and upper bound super::size_hint::add( super::size_hint::mul(self.a.size_hint(), self.b_orig.size_hint()), (b_min * has_cur, b_max.map(move |x| x * has_cur)), ) } fn fold<Acc, G>(mut self, mut accum: Acc, mut f: G) -> Acc where G: FnMut(Acc, Self::Item) -> Acc, { if let Some(mut a) = self.a_cur.take() { let mut b = self.b; loop { accum = b.fold(accum, |acc, elt| f(acc, (a.clone(), elt))); // we can only continue iterating a if we had a first element; if let Some(next_a) = self.a.next() { b = self.b_orig.clone(); a = next_a; } else { break; } } } accum } } } #[allow(missing_docs)] mod intersperse { use super::size_hint; use std::iter; #[derive(Debug, Clone)] #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] pub struct Intersperse<I> where I: Iterator, { element: I::Item, iter: iter::Fuse<I>, peek: Option<I::Item>, } pub fn intersperse<I>(iter: I, elt: I::Item) -> Intersperse<I> where I: Iterator, { let mut iter = iter.fuse(); Intersperse { peek: iter.next(), iter, element: elt, } } impl<I> Iterator for Intersperse<I> where I: Iterator, I::Item: Clone, { type Item = I::Item; #[inline] fn next(&mut self) -> Option<I::Item> { if self.peek.is_some() { self.peek.take() } else { self.peek = self.iter.next(); if self.peek.is_some() { Some(self.element.clone()) } else { None } } } fn size_hint(&self) -> (usize, Option<usize>) { // 2 * SH + { 1 or 0 } let has_peek = self.peek.is_some() as usize; let sh = self.iter.size_hint(); size_hint::add_scalar(size_hint::add(sh, sh), has_peek) } fn fold<B, F>(mut self, init: B, mut f: F) -> B where Self: Sized, F: FnMut(B, Self::Item) -> B, { let mut accum = init; if let Some(x) = self.peek.take() { accum = f(accum, x); } let element = &self.element; self.iter.fold(accum, |accum, x| { let accum = f(accum, element.clone()); f(accum, x) }) } } } #[allow(missing_docs)] mod format_intersparse { use super::Seq; use std::fmt; pub fn format_intersparse<I, T>(iter: I, separator: T) -> String where I: Iterator, I::Item: fmt::Display, T: fmt::Display, { iter.map(|x| format!("{}", x)) .intersperse(format!("{}", separator)) .collect::<String>() } } mod size_hint { use std::cmp; use std::usize; pub type SizeHint = (usize, Option<usize>); #[inline] pub fn add(a: SizeHint, b: SizeHint) -> SizeHint { let min = a.0.saturating_add(b.0); let max = match (a.1, b.1) { (Some(x), Some(y)) => x.checked_add(y), _ => None, }; (min, max) } #[inline] #[allow(dead_code)] pub fn add_scalar(sh: SizeHint, x: usize) -> SizeHint { let (mut low, mut hi) = sh; low = low.saturating_add(x); hi = hi.and_then(|elt| elt.checked_add(x)); (low, hi) } #[inline] #[allow(dead_code)] pub fn sub_scalar(sh: SizeHint, x: usize) -> SizeHint { let (mut low, mut hi) = sh; low = low.saturating_sub(x); hi = hi.map(|elt| elt.saturating_sub(x)); (low, hi) } #[inline] #[allow(dead_code)] pub fn mul(a: SizeHint, b: SizeHint) -> SizeHint { let low = a.0.saturating_mul(b.0); let hi = match (a.1, b.1) { (Some(x), Some(y)) => x.checked_mul(y), (Some(0), None) | (None, Some(0)) => Some(0), _ => None, }; (low, hi) } #[inline] #[allow(dead_code)] pub fn mul_scalar(sh: SizeHint, x: usize) -> SizeHint { let (mut low, mut hi) = sh; low = low.saturating_mul(x); hi = hi.and_then(|elt| elt.checked_mul(x)); (low, hi) } #[inline] #[allow(dead_code)] pub fn max(a: SizeHint, b: SizeHint) -> SizeHint { let (a_lower, a_upper) = a; let (b_lower, b_upper) = b; let lower = cmp::max(a_lower, b_lower); let upper = match (a_upper, b_upper) { (Some(x), Some(y)) => Some(cmp::max(x, y)), _ => None, }; (lower, upper) } #[inline] #[allow(dead_code)] pub fn min(a: SizeHint, b: SizeHint) -> SizeHint { let (a_lower, a_upper) = a; let (b_lower, b_upper) = b; let lower = cmp::min(a_lower, b_lower); let upper = match (a_upper, b_upper) { (Some(u1), Some(u2)) => Some(cmp::min(u1, u2)), _ => a_upper.or(b_upper), }; (lower, upper) } } } // }}} // fp {{{ #[allow(dead_code)] mod fp { mod arith { use super::{Fp, Mod}; use std::ops::*; impl<T: Mod> Add for Fp<T> { type Output = Self; fn add(self, rhs: Self) -> Self { let res = self.0 + rhs.0; Self::unchecked(if T::MOD <= res { res - T::MOD } else { res }) } } impl<T: Mod> Sub for Fp<T> { type Output = Self; fn sub(self, rhs: Self) -> Self { let res = self.0 - rhs.0; Self::unchecked(if res < 0 { res + T::MOD } else { res }) } } impl<T: Mod> Mul for Fp<T> { type Output = Self; fn mul(self, rhs: Self) -> Self { Self::new(self.0 * rhs.0) } } #[allow(clippy::suspicious_arithmetic_impl)] impl<T: Mod> Div for Fp<T> { type Output = Self; fn div(self, rhs: Self) -> Self { self * rhs.inv() } } impl<M: Mod> Neg for Fp<M> { type Output = Self; fn neg(self) -> Self { if self.0 == 0 { Self::unchecked(0) } else { Self::unchecked(M::MOD - self.0) } } } impl<M: Mod> Neg for &Fp<M> { type Output = Fp<M>; fn neg(self) -> Self::Output { if self.0 == 0 { Fp::unchecked(0) } else { Fp::unchecked(M::MOD - self.0) } } } macro_rules! forward_assign_biop { ($(impl $trait:ident, $fn_assign:ident, $fn:ident)*) => { $( impl<M: Mod> $trait for Fp<M> { fn $fn_assign(&mut self, rhs: Self) { *self = self.$fn(rhs); } } )* }; } forward_assign_biop! { impl AddAssign, add_assign, add impl SubAssign, sub_assign, sub impl MulAssign, mul_assign, mul impl DivAssign, div_assign, div } macro_rules! forward_ref_binop { ($(impl $imp:ident, $method:ident)*) => { $( impl<'a, T: Mod> $imp<Fp<T>> for &'a Fp<T> { type Output = Fp<T>; fn $method(self, other: Fp<T>) -> Self::Output { $imp::$method(*self, other) } } impl<'a, T: Mod> $imp<&'a Fp<T>> for Fp<T> { type Output = Fp<T>; fn $method(self, other: &Fp<T>) -> Self::Output { $imp::$method(self, *other) } } impl<'a, T: Mod> $imp<&'a Fp<T>> for &'a Fp<T> { type Output = Fp<T>; fn $method(self, other: &Fp<T>) -> Self::Output { $imp::$method(*self, *other) } } )* }; } forward_ref_binop! { impl Add, add impl Sub, sub impl Mul, mul impl Div, div } } use std::{ fmt::{Debug, Display}, hash::Hash, iter, marker::PhantomData, ops, }; // NOTE: `crate::` がないとうまく展開できません。 crate::define_fp!(pub F998244353, Mod998244353, 998244353); crate::define_fp!(pub F1000000007, Mod1000000007, 1000000007); #[derive(Clone, PartialEq, Copy, Eq, Hash)] pub struct Fp<T>(i64, PhantomData<T>); pub trait Mod: Debug + Clone + PartialEq + Copy + Eq + Hash { const MOD: i64; } impl<T: Mod> Fp<T> { pub fn new(mut x: i64) -> Self { x %= T::MOD; Self::unchecked(if x < 0 { x + T::MOD } else { x }) } pub fn into_inner(self) -> i64 { self.0 } pub fn r#mod() -> i64 { T::MOD } pub fn inv(self) -> Self { assert_ne!(self.0, 0, "Zero division"); let (sign, x) = if self.0 * 2 < T::MOD { (1, self.0) } else { (-1, T::MOD - self.0) }; let (g, _a, b) = ext_gcd(T::MOD, x); let ans = sign * b; assert_eq!(g, 1); Self::unchecked(if ans < 0 { ans + T::MOD } else { ans }) } pub fn frac(x: i64, y: i64) -> Self { Fp::new(x) / Fp::new(y) } pub fn pow(mut self, mut p: u64) -> Self { let mut ans = Fp::new(1); while p != 0 { if p % 2 == 1 { ans *= self; } self *= self; p /= 2; } ans } fn unchecked(x: i64) -> Self { Self(x, PhantomData) } } impl<T: Mod> iter::Sum<Fp<T>> for Fp<T> { fn sum<I>(iter: I) -> Self where I: iter::Iterator<Item = Fp<T>>, { iter.fold(Fp::new(0), ops::Add::add) } } impl<'a, T: 'a + Mod> iter::Sum<&'a Fp<T>> for Fp<T> { fn sum<I>(iter: I) -> Self where I: iter::Iterator<Item = &'a Fp<T>>, { iter.fold(Fp::new(0), ops::Add::add) } } impl<T: Mod> iter::Product<Fp<T>> for Fp<T> { fn product<I>(iter: I) -> Self where I: iter::Iterator<Item = Fp<T>>, { iter.fold(Self::new(1), ops::Mul::mul) } } impl<'a, T: 'a + Mod> iter::Product<&'a Fp<T>> for Fp<T> { fn product<I>(iter: I) -> Self where I: iter::Iterator<Item = &'a Fp<T>>, { iter.fold(Self::new(1), ops::Mul::mul) } } impl<T: Mod> Debug for Fp<T> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { let (x, y, _z) = reduce(self.0, T::MOD); let (x, y) = match y.signum() { 1 => (x, y), -1 => (-x, -y), _ => unreachable!(), }; if y == 1 { write!(f, "{}", x) } else { write!(f, "{}/{}", x, y) } } } impl<T: Mod> Display for Fp<T> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { write!(f, "{}", self.0) } } // ax + by = gcd(x, y) なる、互いに素な (a, b) を一組探して、(g, a, b) を返します。 // // | 0 -x | | y -x | | x 0 | // | 1 b | = | a b | | y 1 | fn ext_gcd(x: i64, y: i64) -> (i64, i64, i64) { let (b, g) = { let mut x = x; let mut y = y; let mut u = 0; let mut v = 1; while x != 0 { let q = y / x; y -= q * x; v -= q * u; std::mem::swap(&mut x, &mut y); std::mem::swap(&mut u, &mut v); } (v, y) }; assert_eq!((g - b * y) % x, 0); let a = (g - b * y) / x; (g, a, b) } fn reduce(a: i64, m: i64) -> (i64, i64, i64) { if a.abs() < 10_000 { (a, 1, 0) } else { let mut q = m.div_euclid(a); let mut r = m.rem_euclid(a); if a <= 2 * r { q += 1; r -= a; } let (x, z, y) = reduce(r, a); (x, y - q * z, z) } } #[macro_export] macro_rules! define_fp { ($vis:vis $fp:ident, $t:ident, $mod:expr) => { #[derive(Debug, Clone, PartialEq, Copy, Eq, Hash)] $vis struct $t; // NOTE: `$crate::` があるとうまく展開できません。 impl Mod for $t { const MOD: i64 = $mod; } // NOTE: `$crate::` があるとうまく展開できません。 $vis type $fp = Fp<$t>; } } } // }}} // ngtio {{{ #[allow(dead_code)] mod ngtio { #![warn(missing_docs)] mod i { use std::{ io::{self, BufRead}, iter, }; pub use self::multi_token::{Leaf, Parser, ParserTuple, RawTuple, Tuple, VecLen}; pub use self::token::{Token, Usize1}; pub fn with_stdin() -> Tokenizer<io::BufReader<io::Stdin>> { io::BufReader::new(io::stdin()).tokenizer() } pub fn with_str(src: &str) -> Tokenizer<&[u8]> { src.as_bytes().tokenizer() } pub struct Tokenizer<S: BufRead> { queue: Vec<String>, // FIXME: String のみにすると速そうです。 scanner: S, } macro_rules! prim_method { ($name:ident: $T:ty) => { #[allow(missing_docs)] pub fn $name(&mut self) -> $T { <$T>::leaf().parse(self) } }; ($name:ident) => { prim_method!($name: $name); }; } macro_rules! prim_methods { ($name:ident: $T:ty; $($rest:tt)*) => { prim_method!($name:$T); prim_methods!($($rest)*); }; ($name:ident; $($rest:tt)*) => { prim_method!($name); prim_methods!($($rest)*); }; () => () } impl<S: BufRead> Tokenizer<S> { pub fn token(&mut self) -> String { self.load(); self.queue.pop().expect("入力が終了したのですが。") } pub fn new(scanner: S) -> Self { Self { queue: Vec::new(), scanner, } } fn load(&mut self) { while self.queue.is_empty() { let mut s = String::new(); let length = self.scanner.read_line(&mut s).unwrap(); // 入力が UTF-8 でないときにエラーだそうです。 if length == 0 { break; } self.queue = s.split_whitespace().rev().map(str::to_owned).collect(); } } pub fn skip_line(&mut self) { assert!( self.queue.is_empty(), "行の途中で呼ばないでいただきたいです。現在のトークンキュー: {:?}", &self.queue ); self.load(); } pub fn end(&mut self) { self.load(); assert!(self.queue.is_empty(), "入力はまだあります!"); } pub fn parse<T: Token>(&mut self) -> T::Output { T::parse(&self.token()) } pub fn parse_collect<T: Token, B>(&mut self, n: usize) -> B where B: iter::FromIterator<T::Output>, { iter::repeat_with(|| self.parse::<T>()).take(n).collect() } pub fn tuple<T: RawTuple>(&mut self) -> <T::LeafTuple as Parser>::Output { T::leaf_tuple().parse(self) } pub fn vec<T: Token>(&mut self, len: usize) -> Vec<T::Output> { T::leaf().vec(len).parse(self) } pub fn vec_tuple<T: RawTuple>( &mut self, len: usize, ) -> Vec<<T::LeafTuple as Parser>::Output> { T::leaf_tuple().vec(len).parse(self) } pub fn vec2<T: Token>(&mut self, height: usize, width: usize) -> Vec<Vec<T::Output>> { T::leaf().vec(width).vec(height).parse(self) } pub fn vec2_tuple<T>( &mut self, height: usize, width: usize, ) -> Vec<Vec<<T::LeafTuple as Parser>::Output>> where T: RawTuple, { T::leaf_tuple().vec(width).vec(height).parse(self) } prim_methods! { u8; u16; u32; u64; u128; usize; i8; i16; i32; i64; i128; isize; char; string: String; } } mod token { use super::multi_token::Leaf; use std::{any, fmt, marker, str}; pub trait Token: Sized { type Output; fn parse(s: &str) -> Self::Output; fn leaf() -> Leaf<Self> { Leaf(marker::PhantomData) } } impl<T> Token for T where T: str::FromStr, <T as str::FromStr>::Err: fmt::Debug, { type Output = T; fn parse(s: &str) -> Self::Output { s.parse().unwrap_or_else(|_| { panic!("Parse error!: ({}: {})", s, any::type_name::<T>(),) }) } } pub struct Usize1 {} impl Token for Usize1 { type Output = usize; fn parse(s: &str) -> Self::Output { usize::parse(s) .checked_sub(1) .expect("Parse error! (Zero substruction error of Usize1)") } } } mod multi_token { use super::{Token, Tokenizer}; use std::{io::BufRead, iter, marker}; pub trait Parser: Sized { type Output; fn parse<S: BufRead>(&self, server: &mut Tokenizer<S>) -> Self::Output; fn vec(self, len: usize) -> VecLen<Self> { VecLen { len, elem: self } } } pub struct Leaf<T>(pub(super) marker::PhantomData<T>); impl<T: Token> Parser for Leaf<T> { type Output = T::Output; fn parse<S: BufRead>(&self, server: &mut Tokenizer<S>) -> T::Output { server.parse::<T>() } } pub struct VecLen<T> { pub len: usize, pub elem: T, } impl<T: Parser> Parser for VecLen<T> { type Output = Vec<T::Output>; fn parse<S: BufRead>(&self, server: &mut Tokenizer<S>) -> Self::Output { iter::repeat_with(|| self.elem.parse(server)) .take(self.len) .collect() } } pub trait RawTuple { type LeafTuple: Parser; fn leaf_tuple() -> Self::LeafTuple; } pub trait ParserTuple { type Tuple: Parser; fn tuple(self) -> Self::Tuple; } pub struct Tuple<T>(pub T); macro_rules! impl_tuple { ($($t:ident: $T:ident),*) => { impl<$($T),*> Parser for Tuple<($($T,)*)> where $($T: Parser,)* { type Output = ($($T::Output,)*); #[allow(unused_variables)] fn parse<S: BufRead >(&self, server: &mut Tokenizer<S>) -> Self::Output { match self { Tuple(($($t,)*)) => { ($($t.parse(server),)*) } } } } impl<$($T: Token),*> RawTuple for ($($T,)*) { type LeafTuple = Tuple<($(Leaf<$T>,)*)>; fn leaf_tuple() -> Self::LeafTuple { Tuple(($($T::leaf(),)*)) } } impl<$($T: Parser),*> ParserTuple for ($($T,)*) { type Tuple = Tuple<($($T,)*)>; fn tuple(self) -> Self::Tuple { Tuple(self) } } }; } impl_tuple!(); impl_tuple!(t1: T1); impl_tuple!(t1: T1, t2: T2); impl_tuple!(t1: T1, t2: T2, t3: T3); impl_tuple!(t1: T1, t2: T2, t3: T3, t4: T4); impl_tuple!(t1: T1, t2: T2, t3: T3, t4: T4, t5: T5); impl_tuple!(t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6); impl_tuple!(t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6, t7: T7); impl_tuple!( t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6, t7: T7, t8: T8 ); } trait Scanner: BufRead + Sized { fn tokenizer(self) -> Tokenizer<Self> { Tokenizer::new(self) } } impl<R: BufRead> Scanner for R {} } pub use self::i::{with_stdin, with_str}; pub mod prelude { pub use super::i::{Parser, ParserTuple, RawTuple, Token, Usize1}; } } // }}}