結果
問題 | No.1036 Make One With GCD 2 |
ユーザー | くれちー |
提出日時 | 2020-04-25 19:15:36 |
言語 | Rust (1.77.0 + proconio) |
結果 |
CE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 16,992 bytes |
コンパイル時間 | 14,580 ms |
コンパイル使用メモリ | 382,512 KB |
最終ジャッジ日時 | 2024-11-14 22:28:13 |
合計ジャッジ時間 | 17,449 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。
ただし、clay言語の場合は開発者のデバッグのため、公開されます。
コンパイルメッセージ
error[E0433]: failed to resolve: use of undeclared crate or module `lib` --> src/main.rs:284:11 | 284 | use lib::algebra::structures::Monoid; | ^^^ use of undeclared crate or module `lib` For more information about this error, try `rustc --explain E0433`. error: could not compile `main` (bin "main") due to 1 previous error
ソースコード
// The main code is at the very bottom. #[allow(unused_imports)] use { lib::byte::ByteChar, std::cell::{Cell, RefCell}, std::cmp::{ self, Ordering::{self, *}, Reverse, }, std::collections::*, std::convert::identity, std::fmt::{self, Debug, Display, Formatter}, std::io::prelude::*, std::iter::{self, FromIterator}, std::marker::PhantomData, std::mem, std::num::Wrapping, std::ops::{Range, RangeFrom, RangeInclusive, RangeTo, RangeToInclusive}, std::process, std::rc::Rc, std::thread, std::time::{Duration, Instant}, std::{char, f32, f64, i128, i16, i32, i64, i8, isize, str, u128, u16, u32, u64, u8, usize}, }; #[allow(unused_imports)] #[macro_use] pub mod lib { pub mod algebra { pub mod structures { pub use self::associative_magma::*; pub use self::magma::*; pub use self::monoid::*; pub use self::semigroup::*; pub use self::unital_magma::*; mod associative_magma { use super::Magma; pub trait AssociativeMagma: Magma {} impl AssociativeMagma for () {} impl<T> AssociativeMagma for Option<T> where T: AssociativeMagma {} } mod magma { pub trait Magma: Clone { fn op(&self, rhs: &Self) -> Self; fn op_assign_right(&mut self, rhs: &Self) { *self = self.op(rhs); } fn op_assign_left(&mut self, lhs: &Self) { *self = lhs.op(self); } } impl Magma for () { #[allow(clippy::unused_unit)] fn op(&self, _rhs: &Self) -> Self { () } } impl<T> Magma for Option<T> where T: Magma, { fn op(&self, rhs: &Self) -> Self { match (self, rhs) { (Some(lhs), Some(rhs)) => Some(lhs.op(rhs)), (Some(x), None) | (None, Some(x)) => Some(x.clone()), (None, None) => None, } } } } mod monoid { use super::{AssociativeMagma, UnitalMagma}; pub trait Monoid: AssociativeMagma + UnitalMagma {} impl<T: AssociativeMagma + UnitalMagma> Monoid for T {} } mod semigroup { use super::AssociativeMagma; pub trait Semigroup: AssociativeMagma {} impl<T: AssociativeMagma> Semigroup for T {} } mod unital_magma { use super::Magma; pub trait UnitalMagma: Magma { fn identity() -> Self; } impl UnitalMagma for () { #[allow(clippy::unused_unit)] fn identity() -> Self { () } } impl<T> UnitalMagma for Option<T> where T: Magma, { fn identity() -> Self { None } } } } } pub mod byte { pub use self::byte_char::*; mod byte_char { use std::error::Error; use std::fmt::{self, Debug, Display, Formatter}; use std::str::FromStr; #[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] #[repr(transparent)] pub struct ByteChar(pub u8); impl Debug for ByteChar { fn fmt(&self, f: &mut Formatter) -> fmt::Result { write!(f, "b'{}'", self.0 as char) } } impl Display for ByteChar { fn fmt(&self, f: &mut Formatter) -> fmt::Result { write!(f, "{}", self.0 as char) } } impl FromStr for ByteChar { type Err = ParseByteCharError; fn from_str(s: &str) -> Result<ByteChar, ParseByteCharError> { match s.as_bytes().len() { 1 => Ok(ByteChar(s.as_bytes()[0])), 0 => Err(ParseByteCharErrorKind::EmptyStr.into()), _ => Err(ParseByteCharErrorKind::TooManyBytes.into()), } } } #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] pub struct ParseByteCharError { kind: ParseByteCharErrorKind, } impl Display for ParseByteCharError { fn fmt(&self, f: &mut Formatter) -> fmt::Result { f.write_str(match self.kind { ParseByteCharErrorKind::EmptyStr => "empty string", ParseByteCharErrorKind::TooManyBytes => "too many bytes", }) } } impl Error for ParseByteCharError {} #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] enum ParseByteCharErrorKind { EmptyStr, TooManyBytes, } impl From<ParseByteCharErrorKind> for ParseByteCharError { fn from(kind: ParseByteCharErrorKind) -> ParseByteCharError { ParseByteCharError { kind } } } } } pub mod io { pub use self::scanner::*; mod scanner { use std::io::{self, BufRead}; use std::iter; use std::str::FromStr; #[derive(Debug)] pub struct Scanner<R> { reader: R, buf: String, pos: usize, } impl<R: BufRead> Scanner<R> { pub fn new(reader: R) -> Self { Scanner { reader: reader, buf: String::new(), pos: 0, } } pub fn next(&mut self) -> io::Result<&str> { let start = loop { match self.rest().find(|c| c != ' ') { Some(i) => break i, None => self.fill_buf()?, } }; self.pos += start; let len = self.rest().find(' ').unwrap_or(self.rest().len()); let s = &self.buf[self.pos..][..len]; // self.rest()[..len] self.pos += len; Ok(s) } pub fn parse_next<T>(&mut self) -> io::Result<Result<T, T::Err>> where T: FromStr, { Ok(self.next()?.parse()) } pub fn parse_next_n<T>(&mut self, n: usize) -> io::Result<Result<Vec<T>, T::Err>> where T: FromStr, { iter::repeat_with(|| self.parse_next()).take(n).collect() } pub fn map_next_bytes<T, F>(&mut self, mut f: F) -> io::Result<Vec<T>> where F: FnMut(u8) -> T, { Ok(self.next()?.bytes().map(&mut f).collect()) } pub fn map_next_bytes_n<T, F>(&mut self, n: usize, mut f: F) -> io::Result<Vec<Vec<T>>> where F: FnMut(u8) -> T, { iter::repeat_with(|| self.map_next_bytes(&mut f)) .take(n) .collect() } fn rest(&self) -> &str { &self.buf[self.pos..] } fn fill_buf(&mut self) -> io::Result<()> { self.buf.clear(); self.pos = 0; let read = self.reader.read_line(&mut self.buf)?; if read == 0 { return Err(io::ErrorKind::UnexpectedEof.into()); } if *self.buf.as_bytes().last().unwrap() == b'\n' { self.buf.pop(); } Ok(()) } } } } pub mod sequences { pub use self::segment_tree::SegmentTree; pub mod segment_tree { use super::*; use lib::algebra::structures::Monoid; use std::collections::VecDeque; use std::iter::{self, FromIterator}; use std::mem; use std::ops::{Deref, DerefMut, Range}; #[derive(Clone, PartialEq, Eq, Debug, Hash)] pub struct SegmentTree<T> { vec: Vec<T>, // virtual length, which is power of two base_len: usize, // avaliable length len: usize, } impl<M: Monoid> FromIterator<M> for SegmentTree<M> { fn from_iter<I>(iter: I) -> Self where I: IntoIterator<Item = M>, { let iter = iter.into_iter(); let min_len = iter.size_hint().0; let (min_base_len, min_vec_len) = Self::extend_len(min_len); let mut deque = VecDeque::with_capacity(min_vec_len); if min_base_len > 1 { // inner nodes deque.extend(iter::repeat(M::identity()).take(min_base_len - 1)); } // leaf nodes deque.extend(iter); let len = deque.len() - min_base_len.saturating_sub(1); let (base_len, _) = Self::extend_len(len); if base_len > min_base_len { for identity in iter::repeat(M::identity()).take(base_len - min_base_len) { deque.push_front(identity); } // for buggy iterator } else if min_base_len > base_len { deque.drain(..min_base_len - base_len); } let mut tree = SegmentTree { vec: deque.into(), base_len, len, }; for node in (1..base_len).rev() { tree.recalc(node); } tree } } impl<M: Monoid> SegmentTree<M> { pub fn new(len: usize) -> Self { let (base_len, vec_len) = Self::extend_len(len); let vec = if vec_len == 0 { vec![] } else { vec![M::identity(); vec_len] }; SegmentTree { vec, base_len, len } } pub fn len(&self) -> usize { self.len } pub fn get(&self, index: usize) -> &M { assert_index(index, self.len()); self.node(self.node_index(index)) } pub fn get_mut(&mut self, index: usize) -> GetMut<M> { assert_index(index, self.len()); GetMut { node: self.node_index(index), tree: self, } } pub fn fold(&self, index: Range<usize>) -> M { assert_index_range(&index, self.len()); let mut start = self.node_index(index.start); let mut end = self.node_index(index.end); let mut lacc = M::identity(); let mut racc = M::identity(); while start < end { if start & 1 == 1 { lacc.op_assign_right(self.node(start)); // [ 010 ] [ 011 ] // [ 100 ] [ 101 ] [ 110 ] [ 111 ] // * --> * start += 1; } if end & 1 == 1 { // [ 010 ] [ 011 ] // [ 100 ] [ 101 ] [ 110 ] [ 111 ] // * <-- * end -= 1; racc.op_assign_left(self.node(end)); } // move to parents start >>= 1; end >>= 1; } lacc.op(&racc) } // (base_len, vec_len) fn extend_len(len: usize) -> (usize, usize) { if len == 0 { (0, 0) } else { len .checked_next_power_of_two() .and_then(|base_len| { (base_len - 1) .checked_add(len) .map(|vec_len| (base_len, vec_len)) }) .unwrap_or_else(|| panic!("length too large: {:?}", len)) } } fn node_index(&self, index: usize) -> usize { self.base_len + index } fn recalc(&mut self, node: usize) { let l = node << 1; let r = (node << 1) | 1; // inclusive let last = self.vec.len(); debug_assert_eq!(last, self.node_index(self.len() - 1)); if l <= last { *self.node_mut(node) = if r <= last { self.node(l).op(&self.node(r)) } else { self.node(l).clone() }; } } fn rebuild(&mut self, mut node: usize) { #[allow(clippy::while_immutable_condition)] while { node >>= 1; node > 0 } { self.recalc(node); } } fn node(&self, node: usize) -> &M { &self.vec[node - 1] } fn node_mut(&mut self, node: usize) -> &mut M { &mut self.vec[node - 1] } } pub struct GetMut<'a, M: 'a + Monoid> { tree: &'a mut SegmentTree<M>, node: usize, } impl<'a, M: Monoid> Drop for GetMut<'a, M> { fn drop(&mut self) { self.tree.rebuild(self.node); } } impl<'a, M: Monoid> Deref for GetMut<'a, M> { type Target = M; fn deref(&self) -> &M { self.tree.node(self.node) } } impl<'a, M: Monoid> DerefMut for GetMut<'a, M> { fn deref_mut(&mut self) -> &mut M { self.tree.node_mut(self.node) } } } use std::ops::{Range, RangeTo}; macro_rules! assert_index { ($cond:expr, $index:expr, $len:expr) => { assert!( $cond, "index out of bounds: the len is {:?} but the index is {:?}", $len, $index ) }; } fn assert_index(index: usize, len: usize) { assert_index!(index < len, index, len); } fn assert_index_range(index: &Range<usize>, len: usize) { assert!( index.start <= index.end, "range start is greater than range end: {:?}", index ); assert_index!(index.end <= len, index, len); } } } const CUSTOM_STACK_SIZE_MIB: Option<usize> = None; const INTERACTIVE: bool = false; fn main() -> std::io::Result<()> { match CUSTOM_STACK_SIZE_MIB { Some(stack_size_mib) => std::thread::Builder::new() .name("run_solver".to_owned()) .stack_size(stack_size_mib * 1024 * 1024) .spawn(run_solver)? .join() .unwrap(), None => run_solver(), } } fn run_solver() -> std::io::Result<()> { let stdin = std::io::stdin(); let reader = stdin.lock(); let stdout = std::io::stdout(); let writer = stdout.lock(); macro_rules! with_wrapper { ($($wrapper:expr)?) => {{ let mut writer = $($wrapper)?(writer); solve(reader, &mut writer)?; writer.flush() }}; } if cfg!(debug_assertions) { with_wrapper!() } else if INTERACTIVE { with_wrapper!(std::io::LineWriter::new) } else { with_wrapper!(std::io::BufWriter::new) } } fn solve<R, W>(reader: R, mut writer: W) -> std::io::Result<()> where R: BufRead, W: Write, { let mut _scanner = lib::io::Scanner::new(reader); #[allow(unused_macros)] macro_rules! scan { ($T:ty) => { _scanner.parse_next::<$T>()?.unwrap() }; ($($T:ty),+) => { ($(scan!($T)),+) }; ($T:ty; $n:expr) => { _scanner.parse_next_n::<$T>($n)?.unwrap() }; ($($T:ty),+; $n:expr) => { iter::repeat_with(|| -> std::io::Result<_> { Ok(($(scan!($T)),+)) }) .take($n) .collect::<std::io::Result<Vec<_>>>()? }; } #[allow(unused_macros)] macro_rules! scan_bytes_map { ($f:expr) => { _scanner.map_next_bytes($f)? }; ($f:expr; $n:expr) => { _scanner.map_next_bytes_n($n, $f)? }; } #[allow(unused_macros)] macro_rules! print { ($($arg:tt)*) => { write!(writer, $($arg)*)? }; } #[allow(unused_macros)] macro_rules! println { ($($arg:tt)*) => { writeln!(writer, $($arg)*)? }; } #[allow(unused_macros)] macro_rules! eprint { ($($arg:tt)*) => { if cfg!(debug_assertions) { std::eprint!($($arg)*) } }; } #[allow(unused_macros)] macro_rules! eprintln { ($($arg:tt)*) => { if cfg!(debug_assertions) { std::eprintln!($($arg)*) } }; } #[allow(unused_macros)] macro_rules! dbg { ($($arg:tt)*) => { if cfg!(debug_assertions) { std::dbg!($($arg)*) } else { ($($arg)*) } }; } { fn gcd(mut x: u64, mut y: u64) -> u64 { if x == 0 || y == 0 { return cmp::max(x, y); } let k = { let a = x.trailing_zeros(); let b = y.trailing_zeros(); x >>= a; y >>= b; cmp::min(a, b) }; while x != y { if x < y { mem::swap(&mut x, &mut y); } x -= y; x >>= x.trailing_zeros(); } x << k } use lib::algebra::structures::*; use lib::sequences::SegmentTree; #[derive(Clone, Copy, Debug)] pub struct Gcd(u64); impl Magma for Gcd { fn op(&self, rhs: &Self) -> Self { Gcd(gcd(self.0, rhs.0)) } } impl AssociativeMagma for Gcd {} impl UnitalMagma for Gcd { fn identity() -> Self { Gcd(0) } } let n = scan!(usize); let a = scan!(u64; n); let segtree = SegmentTree::<Gcd>::from_iter(a.iter().copied().map(Gcd)); let mut cnt = 0; { let mut l = 0; let mut r = 1; while l < n { while r < n && segtree.fold(l..r).0 != 1 { r += 1; } if r == n && segtree.fold(l..r).0 != 1 { break; } let m = 1 + n - r; cnt += m; while { l += 1; l < r && segtree.fold(l..r).0 == 1 } { cnt += m; } } } println!("{}", cnt); } Ok(()) }