// codesnip-guard: linear_diophantine #[cfg_attr(any(),rustfmt::skip)]pub use self::linear_diophantine::solve_linear_diophantine;#[cfg_attr(any(),rustfmt::skip)]mod linear_diophantine{use super::{ExtendedGcd,RangeBoundsExt,Signed,Unsigned};use std::ops::RangeInclusive;#[doc=" ax + b"]#[derive(Clone,Copy,Debug)]struct Linearwhere T:Signed{a:T,b:T}implLinearwhere T:Signed{fn new(a:T,b:T)->Self{Self{a,b}}fn eval(&self,x:T)->T{self.a*x+self.b}}#[doc=" Solution of ax + by = c"]#[derive(Clone,Copy,Debug)]pub struct LinearDiophantineSolutionwhere T:Signed{x:Linear,y:Linear,k_range:(T,T)}macro_rules!with_range{($this:ident,$x:ident,$range:expr)=>{let range=$range.to_range_inclusive();let l=*range.start();let r=*range.end();if!l.is_minimum(){if$this.$x.a.is_positive(){let t=(l-$this.$x.b+$this.$x.a-T::one()).div_euclid($this.$x.a);$this.k_range.0=$this.k_range.0 .max(t);}else{let t=($this.$x.b-l).div_euclid(-$this.$x.a);$this.k_range.1=$this.k_range.1 .min(t);}}if!r.is_maximum(){if$this.$x.a.is_positive(){let t=(r-$this.$x.b).div_euclid($this.$x.a);$this.k_range.1=$this.k_range.1 .min(t);}else{let t=($this.$x.b-r-$this.$x.a-T::one()).div_euclid(-$this.$x.a);$this.k_range.0=$this.k_range.0 .max(t);}}};}implLinearDiophantineSolutionwhere T:Signed{pub fn eval(&self,k:T)->(T,T){(self.x.eval(k),self.y.eval(k))}pub fn with_x_range(mut self,range:R)->Self where R:RangeBoundsExt{with_range!(self,x,range);self}pub fn with_y_range(mut self,range:R)->Self where R:RangeBoundsExt{with_range!(self,y,range);self}pub fn with_x_order(mut self)->Self{if self.x.a.is_negative(){self.x.a=-self.x.a;self.y.a=-self.y.a;self.k_range=(if self.k_range.1==T::maximum(){T::minimum()}else{-self.k_range.1},if self.k_range.0==T::minimum(){T::maximum()}else{-self.k_range.0});}self}pub fn with_y_order(mut self)->Self{if self.y.a.is_negative(){self.x.a=-self.x.a;self.y.a=-self.y.a;self.k_range=(if self.k_range.1==T::maximum(){T::minimum()}else{-self.k_range.1},if self.k_range.0==T::minimum(){T::maximum()}else{-self.k_range.0});}self}pub fn k_range(&self)->RangeInclusive{self.k_range.0 ..=self.k_range.1}}#[doc=" Solve ax + by = c"]pub fn solve_linear_diophantine(a:T,b:T,c:T)->Option>where T:Signed{assert!(!a.is_zero(),"a must be non-zero");assert!(!b.is_zero(),"b must be non-zero");let ExtendedGcd{g,x:x0,y:y0}=a.extgcd(b);let g=g.signed();let a=a/g;let b=b/g;if c.is_zero(){return Some(LinearDiophantineSolution{x:Linear::new(b,T::zero()),y:Linear::new(-a,T::zero()),k_range:(T::minimum(),T::maximum())});}if!(c%g).is_zero(){return None;}let c=c/g;let x=Linear::new(b,x0*c);let y=Linear::new(-a,y0*c);Some(LinearDiophantineSolution{x,y,k_range:(T::minimum(),T::maximum())})}} // codesnip-guard: array #[cfg_attr(any(),rustfmt::skip)]mod array{#[macro_export]macro_rules!array{[@inner$data:ident=[$init:expr;$len:expr]]=>{{use::std::mem::{ManuallyDrop,MaybeUninit};let mut$data:[MaybeUninit<_>;$len]=unsafe{MaybeUninit::uninit().assume_init()};$init;#[repr(C)]union __Transmuter{src:ManuallyDrop<[MaybeUninit;N]>,dst:ManuallyDrop<[T;N]>,}ManuallyDrop::into_inner(unsafe{__Transmuter{src:ManuallyDrop::new($data)}.dst})}};[||$e:expr;$len:expr]=>{$crate::array![@inner data=[data.iter_mut().for_each(|item|*item=MaybeUninit::new($e));$len]]};[|$i:pat_param|$e:expr;$len:expr]=>{$crate::array![@inner data=[data.iter_mut().enumerate().for_each(|($i,item)|*item=MaybeUninit::new($e));$len]]};[$e:expr;$len:expr]=>{{let e=$e;$crate::array![||Clone::clone(&e);$len]}};}} // codesnip-guard: bounded #[cfg_attr(any(),rustfmt::skip)]pub use self::bounded::Bounded;#[cfg_attr(any(),rustfmt::skip)]mod bounded{#[doc=" Trait for max/min bounds"]pub trait Bounded:Sized+PartialOrd{fn maximum()->Self;fn minimum()->Self;fn is_maximum(&self)->bool{self==&Self::maximum()}fn is_minimum(&self)->bool{self==&Self::minimum()}fn set_maximum(&mut self){*self=Self::maximum()}fn set_minimum(&mut self){*self=Self::minimum()}}macro_rules!impl_bounded_num{($($t:ident)*)=>{$(impl Bounded for$t{fn maximum()->Self{$t::MAX}fn minimum()->Self{$t::MIN}})*};}impl_bounded_num!(u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize f32 f64);macro_rules!impl_bounded_tuple{(@impl$($T:ident)*)=>{impl<$($T:Bounded),*>Bounded for($($T,)*){fn maximum()->Self{($(<$T as Bounded>::maximum(),)*)}fn minimum()->Self{($(<$T as Bounded>::minimum(),)*)}}};(@inner$($T:ident)*,)=>{impl_bounded_tuple!(@impl$($T)*);};(@inner$($T:ident)*,$U:ident$($Rest:ident)*)=>{impl_bounded_tuple!(@impl$($T)*);impl_bounded_tuple!(@inner$($T)*$U,$($Rest)*);};($T:ident$($Rest:ident)*)=>{impl_bounded_tuple!(@inner$T,$($Rest)*);};}impl_bounded_tuple!(A B C D E F G H I J);impl Bounded for(){fn maximum()->Self{}fn minimum()->Self{}}impl Bounded for bool{fn maximum()->Self{true}fn minimum()->Self{false}}implBounded for Optionwhere T:Bounded{fn maximum()->Self{Some(::maximum())}fn minimum()->Self{None}}implBounded for std::cmp::Reversewhere T:Bounded{fn maximum()->Self{std::cmp::Reverse(::minimum())}fn minimum()->Self{std::cmp::Reverse(::maximum())}}} // codesnip-guard: discrete_steps #[cfg_attr(any(),rustfmt::skip)]pub use self::discrete_steps::{DiscreteSteps,RangeBoundsExt};#[cfg_attr(any(),rustfmt::skip)]mod discrete_steps{use super::Bounded;use std::{convert::TryFrom,ops::{Bound,Range,RangeBounds,RangeInclusive}};pub trait DiscreteSteps:Clone{fn delta()->Delta;fn steps_between(start:&Self,end:&Self)->Option;fn forward_checked(start:Self,delta:Delta)->Option;fn backward_checked(start:Self,delta:Delta)->Option;fn forward(start:Self,delta:Delta)->Self{Self::forward_checked(start,delta).expect("overflow in `DiscreteSteps::forward`")}fn backward(start:Self,delta:Delta)->Self{Self::backward_checked(start,delta).expect("overflow in `DiscreteSteps::backward`")}fn forward_delta_checked(start:Self)->Option{Self::forward_checked(start,Self::delta())}fn backward_delta_checked(start:Self)->Option{Self::backward_checked(start,Self::delta())}fn forward_delta(start:Self)->Self{Self::forward(start,Self::delta())}fn backward_delta(start:Self)->Self{Self::backward(start,Self::delta())}}macro_rules!impl_discrete_steps_integer{(@common$u_source:ident)=>{fn delta()->$u_source{1}fn forward(start:Self,delta:$u_source)->Self{assert!(Self::forward_checked(start,delta).is_some(),"attempt to add with overflow");start.wrapping_add(delta as Self)}fn backward(start:Self,delta:$u_source)->Self{assert!(Self::backward_checked(start,delta).is_some(),"attempt to subtract with overflow");start.wrapping_sub(delta as Self)}};($u_source:ident$i_source:ident;$($u_narrower:ident$i_narrower:ident),*;$($u_wider:ident$i_wider:ident),*)=>{$(impl DiscreteSteps<$u_source>for$u_narrower{impl_discrete_steps_integer!(@common$u_source);fn steps_between(start:&Self,end:&Self)->Option<$u_source>{if*start<=*end{$u_source::try_from(*end-*start).ok()}else{None}}fn forward_checked(start:Self,delta:$u_source)->Option{Self::try_from(delta).ok().and_then(|delta|start.checked_add(delta))}fn backward_checked(start:Self,delta:$u_source)->Option{Self::try_from(delta).ok().and_then(|delta|start.checked_sub(delta))}}impl DiscreteSteps<$u_source>for$i_narrower{impl_discrete_steps_integer!(@common$u_source);fn steps_between(start:&Self,end:&Self)->Option<$u_source>{if*start<=*end{$u_source::try_from(end.abs_diff(*start)).ok()}else{None}}fn forward_checked(start:Self,delta:$u_source)->Option{$u_narrower::try_from(delta).ok().and_then(|delta|{let wrapped=start.wrapping_add(delta as Self);if wrapped>=start{Some(wrapped)}else{None}})}fn backward_checked(start:Self,delta:$u_source)->Option{$u_narrower::try_from(delta).ok().and_then(|delta|{let wrapped=start.wrapping_sub(delta as Self);if wrapped<=start{Some(wrapped)}else{None}})}})*$(impl DiscreteSteps<$u_source>for$u_wider{impl_discrete_steps_integer!(@common$u_source);fn steps_between(start:&Self,end:&Self)->Option<$u_source>{if*start<=*end{$u_source::try_from(*end-*start).ok()}else{None}}fn forward_checked(start:Self,delta:$u_source)->Option{start.checked_add(delta as Self)}fn backward_checked(start:Self,delta:$u_source)->Option{start.checked_sub(delta as Self)}}impl DiscreteSteps<$u_source>for$i_wider{impl_discrete_steps_integer!(@common$u_source);fn steps_between(start:&Self,end:&Self)->Option<$u_source>{if*start<=*end{end.checked_sub(*start).and_then(|result|$u_source::try_from(result).ok())}else{None}}fn forward_checked(start:Self,delta:$u_source)->Option{start.checked_add(delta as Self)}fn backward_checked(start:Self,delta:$u_source)->Option{start.checked_sub(delta as Self)}})*};}impl_discrete_steps_integer!(u16 i16;u8 i8,u16 i16,usize isize;u32 i32,u64 i64,u128 i128);impl_discrete_steps_integer!(u32 i32;u8 i8,u16 i16,u32 i32,usize isize;u64 i64,u128 i128);impl_discrete_steps_integer!(u64 i64;u8 i8,u16 i16,u32 i32,u64 i64,usize isize;u128 i128);impl_discrete_steps_integer!(u128 i128;u8 i8,u16 i16,u32 i32,u64 i64,u128 i128,usize isize;);impl_discrete_steps_integer!(usize isize;u8 i8,u16 i16,u32 i32,u64 i64,usize isize;u128 i128);pub trait RangeBoundsExt{fn start_bound_included_checked(&self)->Option;fn start_bound_excluded_checked(&self)->Option;fn end_bound_included_checked(&self)->Option;fn end_bound_excluded_checked(&self)->Option;fn start_bound_included(&self)->T;fn start_bound_excluded(&self)->T;fn end_bound_included(&self)->T;fn end_bound_excluded(&self)->T;fn start_bound_included_bounded(&self,lb:T)->Optionwhere T:Ord;fn start_bound_excluded_bounded(&self,lb:T)->Optionwhere T:Ord;fn end_bound_included_bounded(&self,ub:T)->Optionwhere T:Ord;fn end_bound_excluded_bounded(&self,ub:T)->Optionwhere T:Ord;fn to_range_checked(&self)->Option>{match(self.start_bound_included_checked(),self.end_bound_excluded_checked()){(Some(start),Some(end))=>Some(start..end),_=>None,}}fn to_range(&self)->Range{self.start_bound_included()..self.end_bound_excluded()}fn to_range_bounded(&self,min:T,max:T)->Option>where T:Ord{Some(self.start_bound_included_bounded(min)?..self.end_bound_excluded_bounded(max)?)}fn to_range_inclusive_checked(&self)->Option>{match(self.start_bound_included_checked(),self.end_bound_included_checked()){(Some(start),Some(end))=>Some(start..=end),_=>None,}}fn to_range_inclusive(&self)->RangeInclusive{self.start_bound_included()..=self.end_bound_included()}fn to_range_inclusive_bounded(&self,min:T,max:T)->Option>where T:Ord{Some(self.start_bound_included_bounded(min)?..=self.end_bound_included_bounded(max)?)}}macro_rules!impl_range_bounds_ext{($($source:ident=>$($target:ident)+);*$(;)?)=>{$($(implRangeBoundsExt<$target>for R where R:RangeBounds<$target>,{fn start_bound_included_checked(&self)->Option<$target>{match self.start_bound(){Bound::Included(x)=>Some(*x),Bound::Excluded(x)=>DiscreteSteps::<$source>::forward_delta_checked(*x),Bound::Unbounded=>Some(Bounded::minimum()),}}fn start_bound_excluded_checked(&self)->Option<$target>{match self.start_bound(){Bound::Included(x)=>DiscreteSteps::<$source>::backward_delta_checked(*x),Bound::Excluded(x)=>Some(*x),Bound::Unbounded=>None,}}fn end_bound_included_checked(&self)->Option<$target>{match self.end_bound(){Bound::Included(x)=>Some(*x),Bound::Excluded(x)=>DiscreteSteps::<$source>::backward_delta_checked(*x),Bound::Unbounded=>Some(Bounded::maximum()),}}fn end_bound_excluded_checked(&self)->Option<$target>{match self.end_bound(){Bound::Included(x)=>DiscreteSteps::<$source>::forward_delta_checked(*x),Bound::Excluded(x)=>Some(*x),Bound::Unbounded=>None,}}fn start_bound_included(&self)->$target{match self.start_bound(){Bound::Included(x)=>*x,Bound::Excluded(x)=>DiscreteSteps::<$source>::forward_delta(*x),Bound::Unbounded=>Bounded::minimum(),}}fn start_bound_excluded(&self)->$target{match self.start_bound(){Bound::Included(x)=>DiscreteSteps::<$source>::backward_delta(*x),Bound::Excluded(x)=>*x,Bound::Unbounded=>DiscreteSteps::<$source>::backward_delta(Bounded::minimum()),}}fn end_bound_included(&self)->$target{match self.end_bound(){Bound::Included(x)=>*x,Bound::Excluded(x)=>DiscreteSteps::<$source>::backward_delta(*x),Bound::Unbounded=>Bounded::maximum(),}}fn end_bound_excluded(&self)->$target{match self.end_bound(){Bound::Included(x)=>DiscreteSteps::<$source>::forward_delta(*x),Bound::Excluded(x)=>*x,Bound::Unbounded=>DiscreteSteps::<$source>::forward_delta(Bounded::maximum()),}}fn start_bound_included_bounded(&self,lb:$target)->Option<$target>where$target:Ord{match self.start_bound(){Bound::Included(x)=>Some(*x).filter(|&x|lb<=x),Bound::Excluded(x)=>DiscreteSteps::<$source>::forward_delta_checked(*x).filter(|&x|lb<=x),Bound::Unbounded=>Some(lb),}}fn start_bound_excluded_bounded(&self,lb:$target)->Option<$target>where$target:Ord{match self.start_bound(){Bound::Included(x)=>DiscreteSteps::<$source>::backward_delta_checked(*x).filter(|&x|lb<=x),Bound::Excluded(x)=>Some(*x).filter(|&x|lb<=x),Bound::Unbounded=>Some(lb),}}fn end_bound_included_bounded(&self,ub:$target)->Option<$target>where$target:Ord{match self.end_bound(){Bound::Included(x)=>Some(*x).filter(|&x|x<=ub),Bound::Excluded(x)=>DiscreteSteps::<$source>::backward_delta_checked(*x).filter(|&x|x<=ub),Bound::Unbounded=>Some(ub),}}fn end_bound_excluded_bounded(&self,ub:$target)->Option<$target>where$target:Ord{match self.end_bound(){Bound::Included(x)=>DiscreteSteps::<$source>::forward_delta_checked(*x).filter(|&x|x<=ub),Bound::Excluded(x)=>Some(*x).filter(|&x|x<=ub),Bound::Unbounded=>Some(ub),}}})+)*};}impl_range_bounds_ext!(u16=>u8 i8 u16 i16;u32=>u32 i32;u64=>u64 i64;u128=>u128 i128;usize=>isize usize;);} // codesnip-guard: integer #[cfg_attr(any(),rustfmt::skip)]pub use self::integer::{BinaryRepr,ExtendedGcd,IntBase,Saturating,Saturatingable,Signed,Unsigned,Wrapping,Wrappingable};#[cfg_attr(any(),rustfmt::skip)]mod integer{use super::{Bounded,One,Scan,ScanSource,Zero};use std::{convert::TryFrom,fmt::{self,Display},iter::{Product,Sum},ops::{Add,AddAssign,BitAnd,BitAndAssign,BitOr,BitOrAssign,BitXor,BitXorAssign,Div,DivAssign,Mul,MulAssign,Neg,Not,Rem,RemAssign,Shl,ShlAssign,Shr,ShrAssign,Sub,SubAssign},str::FromStr};#[doc=" Trait for basic primitive integer operations."]pub trait IntBase:Copy+Bounded+Zero+One+Eq+Ord+Default+FromStr+Display+Add+Sub+Mul+Div+Rem+AddAssign+SubAssign+MulAssign+DivAssign+RemAssign+Sum+Product{type Error;fn div_euclid(self,rhs:Self)->Self;fn rem_euclid(self,rhs:Self)->Self;fn pow(self,exp:u32)->Self;fn from_str_radix(src:&str,radix:u32)->Result;fn ilog(self,base:Self)->u32;fn ilog2(self)->u32;fn ilog10(self)->u32;fn isqrt(self)->Self;fn midpoint(self,rhs:Self)->Self;}macro_rules!impl_int_base{($($t:ty)*)=>{$(impl IntBase for$t{type Error=std::num::ParseIntError;fn div_euclid(self,rhs:Self)->Self{self.div_euclid(rhs)}fn rem_euclid(self,rhs:Self)->Self{self.rem_euclid(rhs)}fn pow(self,exp:u32)->Self{self.pow(exp)}fn from_str_radix(src:&str,radix:u32)->Result{Self::from_str_radix(src,radix)}fn ilog(self,base:Self)->u32{self.ilog(base)}fn ilog2(self)->u32{self.ilog2()}fn ilog10(self)->u32{self.ilog10()}fn isqrt(self)->Self{self.isqrt()}fn midpoint(self,rhs:Self)->Self{self.midpoint(rhs)}})*};}impl_int_base!(u8 i8 u16 i16 u32 i32 u64 i64 u128 i128 usize isize);#[doc=" extended_gcd(a,b): ax + by = g = gcd(a,b)"]pub struct ExtendedGcd{#[doc=" gcd"]pub g:T::Unsigned,pub x:T,pub y:T}#[doc=" Trait for unsigned integer operations."]pub trait Unsigned:IntBase{type Signed:Signed;fn signed(self)->Self::Signed;fn abs_diff(self,other:Self)->Self;fn div_ceil(self,rhs:Self)->Self;fn is_power_of_two(self)->bool;fn next_power_of_two(self)->Self;fn is_multiple_of(self,rhs:Self)->bool;fn next_multiple_of(self,rhs:Self)->Self;fn gcd(self,other:Self)->Self;fn lcm(self,other:Self)->Self{if self.is_zero()&&other.is_zero(){Self::zero()}else{self/self.gcd(other)*other}}fn mod_inv(self,modulo:Self)->Self{debug_assert!(!modulo.is_zero(),"modulo must be non-zero");if modulo.is_one(){return Self::zero();}let(mut a,mut b)=(self,modulo);let(mut u,mut v)=(Self::one(),Self::zero());let mut negative=true;while!a.is_zero(){let k=b/a;v+=k*u;b-=k*a;std::mem::swap(&mut u,&mut v);std::mem::swap(&mut b,&mut a);negative=!negative;}debug_assert!(b.is_one(),"not coprime");if negative{v.mod_neg(modulo)}else{v}}fn mod_add(self,rhs:Self,modulo:Self)->Self;fn mod_sub(self,rhs:Self,modulo:Self)->Self;fn mod_mul(self,rhs:Self,modulo:Self)->Self;fn mod_neg(self,modulo:Self)->Self{debug_assert!(!modulo.is_zero(),"modulo must be non-zero");debug_assert!(self{type Unsigned:Unsigned;fn unsigned(self)->Self::Unsigned;fn abs(self)->Self;fn abs_diff(self,other:Self)->Self::Unsigned;fn is_negative(self)->bool;fn is_positive(self)->bool;fn signum(self)->Self;fn extgcd(self,other:Self)->ExtendedGcd{let(mut a,mut b)=(self,other);let(mut u,mut v,mut x,mut y)=(Self::one(),Self::zero(),Self::zero(),Self::one());while!a.is_zero(){let k=b/a;x-=k*u;y-=k*v;b-=k*a;std::mem::swap(&mut x,&mut u);std::mem::swap(&mut y,&mut v);std::mem::swap(&mut b,&mut a);}if b.is_negative(){b=-b;x=-x;y=-y;}ExtendedGcd{g:b.unsigned(),x,y}}}macro_rules!impl_unsigned_signed{($([$($tt:tt)*])*)=>{$(impl_unsigned_signed!($($tt)*);)*};($unsigned:ident$signed:ident$upperty:ident)=>{impl_unsigned_signed!(@inner$unsigned$signed fn mod_mul(self,rhs:Self,modulo:Self)->Self{debug_assert!(!modulo.is_zero(),"modulo must be non-zero");(self as$upperty*rhs as$upperty%modulo as$upperty)as$unsigned});};(u128 i128)=>{impl_unsigned_signed!(@inner u128 i128 fn mod_mul(self,rhs:Self,modulo:Self)->Self{debug_assert!(!modulo.is_zero(),"modulo must be non-zero");const MASK64:u128=0xffff_ffff_ffff_ffff;let(au,ad)=(self>>64,self&MASK64);let(bu,bd)=(rhs>>64,rhs&MASK64);let p0=ad*bd%modulo;let p2=au*bu%modulo;let mut x=[p0 as u64,(p0>>64)as u64,p2 as u64,(p2>>64)as u64,];let p1=(au*bd%modulo).mod_add(ad*bu%modulo,modulo);let(p1_lo,p1_hi)=((p1&MASK64)as u64,(p1>>64)as u64);let(s1,c1)=x[1].overflowing_add(p1_lo);x[1]=s1 as u64;let(s2,c2)=x[2].overflowing_add(p1_hi+c1 as u64);x[2]=s2 as u64;let(s3,_)=x[3].overflowing_add(c2 as u64);x[3]=s3 as u64;rem_u256_by_u128(x,modulo)});};(@inner$unsigned:ident$signed:ident$mod_mul:item)=>{impl Unsigned for$unsigned{type Signed=$signed;fn signed(self)->Self::Signed{self as Self::Signed}fn abs_diff(self,other:Self)->Self{self.abs_diff(other)}fn div_ceil(self,rhs:Self)->Self{self.div_ceil(rhs)}fn is_power_of_two(self)->bool{self.is_power_of_two()}fn next_power_of_two(self)->Self{self.next_power_of_two()}fn is_multiple_of(self,rhs:Self)->bool{self.is_multiple_of(rhs)}fn next_multiple_of(self,rhs:Self)->Self{self.next_multiple_of(rhs)}fn gcd(self,other:Self)->Self{let(mut a,mut b)=(self,other);if a.is_zero()||b.is_zero(){return a|b;}let u=a.trailing_zeros();let v=b.trailing_zeros();a>>=u;b>>=v;let k=u.min(v);while a!=b{if a>=a.trailing_zeros();}a<Self{debug_assert!(!modulo.is_zero(),"modulo must be non-zero");debug_assert!(self=modulo){s.wrapping_sub(modulo)}else{s}}fn mod_sub(self,rhs:Self,modulo:Self)->Self{debug_assert!(!modulo.is_zero(),"modulo must be non-zero");debug_assert!(selfSelf::Unsigned{self as Self::Unsigned}fn abs_diff(self,other:Self)->Self::Unsigned{self.abs_diff(other)}fn abs(self)->Self{self.abs()}fn is_negative(self)->bool{self.is_negative()}fn is_positive(self)->bool{self.is_positive()}fn signum(self)->Self{self.signum()}}};}impl_unsigned_signed!([u8 i8 u16][u16 i16 u32][u32 i32 u64][u64 i64 u128][u128 i128][usize isize u128]);fn rem_u256_by_u128(u:[u64;4],v:u128)->u128{#[inline(always)]fn sub_with_borrow_u64(lhs:u64,rhs:u64,borrow:bool)->(u64,bool){let(res,overflow)=lhs.overflowing_sub(rhs);if borrow{let(res,overflow_borrow)=res.overflowing_sub(1);(res,overflow|overflow_borrow)}else{(res,overflow)}}debug_assert!(v!=0);let v_hi=(v>>64)as u64;if v_hi==0{let d=v as u64 as u128;let mut rem:u128=0;for&w in u.iter().rev(){rem=(rem<<64|w as u128)%d;}return rem;}let v_lo=v as u64;let v_shift=v_hi.leading_zeros();let(vn1,vn0)=if v_shift==0{(v_hi,v_lo)}else{let hi=v_hi<>(64-v_shift);let lo=v_lo<>(64-v_shift);un[2]=u[2]<>(64-v_shift);un[3]=u[3]<>(64-v_shift);un[4]=u[3]>>(64-v_shift);}for j in(0..=2).rev(){let num=(un[j+2]as u128)<<64|un[j+1]as u128;let(mut qhat,mut rhat)={let d=vn1 as u128;let q=(num/d).min(u64::MAX as u128);(q as u64,(num-q*d).min(u64::MAX as u128)as u64)};while qhat as u128*vn0 as u128>(rhat as u128)<<64|un[j]as u128{qhat-=1;let t=rhat as u128+vn1 as u128;if t>=1u128<<64{break;}rhat=t as u64;}let p0=qhat as u128*vn0 as u128;let p1=qhat as u128*vn1 as u128;let(p0_hi,p0_lo)=((p0>>64)as u64,p0 as u64);let(p1_hi,p1_lo)=((p1>>64)as u64,p1 as u64);let(r0,borrow)=sub_with_borrow_u64(un[j],p0_lo,false);un[j]=r0;let(r1,borrow1)=sub_with_borrow_u64(un[j+1],p0_hi,borrow);let(r1,borrow2)=sub_with_borrow_u64(r1,p1_lo,false);let borrow=borrow1||borrow2;un[j+1]=r1;let(r2,borrow)=sub_with_borrow_u64(un[j+2],p1_hi,borrow);un[j+2]=r2;assert!(!borrow);}((un[1]as u128)<<64|un[0]as u128)>>v_hi.leading_zeros()}#[doc=" Trait for operations of integer in binary representation."]pub trait BinaryRepr:Sized+Not+BitAnd+BitOr+BitXor+Shl+Shr+BitAndAssign+BitOrAssign+BitXorAssign+ShlAssign+ShrAssign{fn count_ones(self)->Size;fn count_zeros(self)->Size;fn leading_ones(self)->Size;fn leading_zeros(self)->Size;fn reverse_bits(self)->Self;fn rotate_left(self,n:Size)->Self;fn rotate_right(self,n:Size)->Self;fn swap_bytes(self)->Self;fn trailing_ones(self)->Size;fn trailing_zeros(self)->Size;}macro_rules!impl_binary_repr{($($t:ty)*)=>{$(impl BinaryRepr for$t{fn count_ones(self)->u32{self.count_ones()}fn count_zeros(self)->u32{self.count_zeros()}fn leading_ones(self)->u32{self.leading_ones()}fn leading_zeros(self)->u32{self.leading_zeros()}fn reverse_bits(self)->Self{self.reverse_bits()}fn rotate_left(self,n:u32)->Self{self.rotate_left(n)}fn rotate_right(self,n:u32)->Self{self.rotate_right(n)}fn swap_bytes(self)->Self{self.swap_bytes()}fn trailing_ones(self)->u32{self.trailing_ones()}fn trailing_zeros(self)->u32{self.trailing_zeros()}})*};}impl_binary_repr!(u8 i8 u16 i16 u32 i32 u64 i64 u128 i128 usize isize);macro_rules!impl_binop{(impl<$T:ident>$Trait:ident$impl:ident for$t:ty)=>{impl<$T>$Trait for$t where$T:$Trait,{type Output=Self;fn$impl(self,rhs:Self)->Self::Output{Self($Trait::$impl(self.0,rhs.0))}}impl<$T>$Trait<$T>for$t where$T:$Trait,{type Output=Self;fn$impl(self,rhs:$T)->Self::Output{Self($Trait::$impl(self.0,rhs))}}};}macro_rules!impl_opassign{(impl<$T:ident>$Trait:ident$impl:ident for$t:ty)=>{impl<$T>$Trait for$t where$T:$Trait,{fn$impl(&mut self,rhs:Self){$Trait::$impl(&mut self.0,rhs.0)}}impl<$T>$Trait<$T>for$t where$T:$Trait,{fn$impl(&mut self,rhs:$T){$Trait::$impl(&mut self.0,rhs)}}};(impl<$T:ident>$Trait:ident$impl:ident for$t:ty=>$F:ident$f:ident)=>{impl<$T>$Trait for$t where$t:$F+Copy,{fn$impl(&mut self,rhs:Self){*self=$F::$f(*self,rhs);}}impl<$T>$Trait<$T>for$t where$t:$F<$T,Output=$t>+Copy,{fn$impl(&mut self,rhs:$T){*self=$F::$f(*self,rhs);}}};}#[derive(Default,PartialEq,Eq,PartialOrd,Ord,Hash,Clone,Copy)]#[repr(transparent)]#[doc=" Wrapper type of arithmetic `saturating_*` operations."]pub struct Saturating(pub T);pub trait Saturatingable:Sized where Saturating:Copy+Bounded+Zero+One+Eq+Ord+Default+FromStr+Display+Add>+Sub>+Mul>+Div>+Rem>+BitAnd>+BitOr>+BitXor>+Shl>+Shr>+AddAssign+SubAssign+MulAssign+DivAssign+RemAssign+BitAndAssign+BitOrAssign+BitXorAssign+ShlAssign+ShrAssign+Not>+Add>+Sub>+Mul>+Div>+Rem>+BitAnd>+BitOr>+BitXor>+AddAssign+SubAssign+MulAssign+DivAssign+RemAssign+BitAndAssign+BitOrAssign+BitXorAssign{fn to_saturating(self)->Saturating{Saturating(self)}fn from_saturating(s:Saturating)->Self{s.0}}implfmt::Debug for Saturatingwhere T:fmt::Debug{fn fmt(&self,f:&mut fmt::Formatter<'_>)->fmt::Result{T::fmt(&self.0,f)}}implBounded for Saturatingwhere T:Bounded{fn maximum()->Self{Self(T::maximum())}fn minimum()->Self{Self(T::minimum())}}implZero for Saturatingwhere T:Zero{fn zero()->Self{Self(T::zero())}}implOne for Saturatingwhere T:One{fn one()->Self{Self(T::one())}}implFromStr for Saturatingwhere T:FromStr{type Err=T::Err;fn from_str(s:&str)->Result{T::from_str(s).map(Self)}}implDisplay for Saturatingwhere T:Display{fn fmt(&self,f:&mut fmt::Formatter<'_>)->fmt::Result{T::fmt(&self.0,f)}}implScan for Saturatingwhere T:Scan{type Output=Self;fn scan(iter:&mut I)->Option{T::scan(iter).map(Self)}}impl_binop!(implDiv div for Saturating);impl_binop!(implRem rem for Saturating);impl_binop!(implBitAnd bitand for Saturating);impl_binop!(implBitOr bitor for Saturating);impl_binop!(implBitXor bitxor for Saturating);impl_opassign!(implAddAssign add_assign for Saturating =>Add add);impl_opassign!(implSubAssign sub_assign for Saturating =>Sub sub);impl_opassign!(implMulAssign mul_assign for Saturating =>Mul mul);impl_opassign!(implDivAssign div_assign for Saturating);impl_opassign!(implRemAssign rem_assign for Saturating);impl_opassign!(implBitAndAssign bitand_assign for Saturating);impl_opassign!(implBitOrAssign bitor_assign for Saturating);impl_opassign!(implBitXorAssign bitxor_assign for Saturating);implNot for Saturatingwhere T:Not{type Output=Self;fn not(self)->Self::Output{Self(Not::not(self.0))}}macro_rules!impl_int_base_for_saturating{($($t:ty)*)=>{$(impl Saturatingable for$t{}impl Add for Saturating<$t>{type Output=Self;fn add(self,rhs:Self)->Self::Output{Self(self.0 .saturating_add(rhs.0))}}impl Add<$t>for Saturating<$t>{type Output=Self;fn add(self,rhs:$t)->Self::Output{Self(self.0 .saturating_add(rhs))}}impl Sub for Saturating<$t>{type Output=Self;fn sub(self,rhs:Self)->Self::Output{Self(self.0 .saturating_sub(rhs.0))}}impl Sub<$t>for Saturating<$t>{type Output=Self;fn sub(self,rhs:$t)->Self::Output{Self(self.0 .saturating_sub(rhs))}}impl Mul for Saturating<$t>{type Output=Self;fn mul(self,rhs:Self)->Self::Output{Self(self.0 .saturating_mul(rhs.0))}}impl Mul<$t>for Saturating<$t>{type Output=Self;fn mul(self,rhs:$t)->Self::Output{Self(self.0 .saturating_mul(rhs))}}impl Sum for Saturating<$t>{fn sum>(iter:I)->Self{iter.fold(Self::zero(),|acc,x|acc+x)}}impl Product for Saturating<$t>{fn product>(iter:I)->Self{iter.fold(Self::one(),|acc,x|acc*x)}}impl IntBase for Saturating<$t>{type Error=<$t as IntBase>::Error;fn div_euclid(self,rhs:Self)->Self{Self(self.0 .div_euclid(rhs.0))}fn rem_euclid(self,rhs:Self)->Self{Self(self.0 .rem_euclid(rhs.0))}fn pow(self,exp:u32)->Self{Self(self.0 .saturating_pow(exp))}fn from_str_radix(src:&str,radix:u32)->Result{<$t as IntBase>::from_str_radix(src,radix).map(Self)}fn ilog(self,base:Self)->u32{self.0 .ilog(base.0)}fn ilog2(self)->u32{self.0 .ilog2()}fn ilog10(self)->u32{self.0 .ilog10()}fn isqrt(self)->Self{Self(self.0 .isqrt())}fn midpoint(self,rhs:Self)->Self{Self(self.0 .midpoint(rhs.0))}}impl From<$t>for Saturating<$t>{fn from(t:$t)->Self{Self(t)}})*};}impl_int_base_for_saturating!(u8 i8 u16 i16 u32 i32 u64 i64 u128 i128 usize isize);macro_rules!impl_unsigned_signed_for_saturating{($($unsigned:ident$signed:ident)*)=>{$(impl Unsigned for Saturating<$unsigned>{type Signed=Saturating<$signed>;fn signed(self)->Self::Signed{Saturating(TryFrom::try_from(self.0).ok().unwrap_or_else($signed::maximum))}fn abs_diff(self,other:Self)->Self{Self(self.0 .abs_diff(other.0))}fn div_ceil(self,rhs:Self)->Self{Self(self.0 .div_ceil(rhs.0))}fn is_power_of_two(self)->bool{self.0 .is_power_of_two()}fn next_power_of_two(self)->Self{Self(self.0 .next_power_of_two())}fn is_multiple_of(self,rhs:Self)->bool{self.0 .is_multiple_of(rhs.0)}fn next_multiple_of(self,rhs:Self)->Self{Self(self.0 .next_multiple_of(rhs.0))}fn gcd(self,other:Self)->Self{Self(self.0 .gcd(other.0))}fn mod_add(self,rhs:Self,modulo:Self)->Self{Self(self.0 .mod_add(rhs.0,modulo.0))}fn mod_sub(self,rhs:Self,modulo:Self)->Self{Self(self.0 .mod_sub(rhs.0,modulo.0))}fn mod_mul(self,rhs:Self,modulo:Self)->Self{Self(self.0 .mod_mul(rhs.0,modulo.0))}}impl Signed for Saturating<$signed>{type Unsigned=Saturating<$unsigned>;fn unsigned(self)->Self::Unsigned{Saturating(TryFrom::try_from(self.0).ok().unwrap_or_else($unsigned::minimum))}fn abs(self)->Self{Self(self.0 .saturating_abs())}fn abs_diff(self,other:Self)->Self::Unsigned{Saturating(self.0 .abs_diff(other.0))}fn is_negative(self)->bool{self.0 .is_negative()}fn is_positive(self)->bool{self.0 .is_positive()}fn signum(self)->Self{Self(self.0 .signum())}}impl Neg for Saturating<$signed>{type Output=Self;fn neg(self)->Self::Output{Self(self.0 .saturating_neg())}})*};}impl_unsigned_signed_for_saturating!(u8 i8 u16 i16 u32 i32 u64 i64 u128 i128 usize isize);macro_rules!impl_binary_repr_for_saturating{($($t:ty)*)=>{$(impl Shlfor Saturating<$t>{type Output=Self;fn shl(self,rhs:u32)->Self::Output{Self(self.0 .checked_shl(rhs).unwrap_or(0))}}impl Shrfor Saturating<$t>{type Output=Self;fn shr(self,rhs:u32)->Self::Output{Self(self.0 .checked_shr(rhs).unwrap_or(0))}}impl ShlAssignfor Saturating<$t>{fn shl_assign(&mut self,rhs:u32){*self=Shl::shl(*self,rhs);}}impl ShrAssignfor Saturating<$t>{fn shr_assign(&mut self,rhs:u32){*self=Shr::shr(*self,rhs);}}impl BinaryRepr for Saturating<$t>{fn count_ones(self)->u32{self.0 .count_ones()}fn count_zeros(self)->u32{self.0 .count_zeros()}fn leading_ones(self)->u32{self.0 .leading_ones()}fn leading_zeros(self)->u32{self.0 .leading_zeros()}fn reverse_bits(self)->Self{Self(self.0 .reverse_bits())}fn rotate_left(self,n:u32)->Self{Self(self.0 .rotate_left(n))}fn rotate_right(self,n:u32)->Self{Self(self.0 .rotate_right(n))}fn swap_bytes(self)->Self{Self(self.0 .swap_bytes())}fn trailing_ones(self)->u32{self.0 .trailing_ones()}fn trailing_zeros(self)->u32{self.0 .trailing_zeros()}})*};}impl_binary_repr_for_saturating!(u8 i8 u16 i16 u32 i32 u64 i64 u128 i128 usize isize);#[derive(Default,PartialEq,Eq,PartialOrd,Ord,Hash,Clone,Copy)]#[repr(transparent)]#[doc=" Wrapper type of arithmetic `wrapping_*` operations."]pub struct Wrapping(pub T);pub trait Wrappingable:Sized where Wrapping:Copy+Bounded+Zero+One+Eq+Ord+Default+FromStr+Display+Add>+Sub>+Mul>+Div>+Rem>+BitAnd>+BitOr>+BitXor>+Shl>+Shr>+AddAssign+SubAssign+MulAssign+DivAssign+RemAssign+BitAndAssign+BitOrAssign+BitXorAssign+ShlAssign+ShrAssign+Not>+Add>+Sub>+Mul>+Div>+Rem>+BitAnd>+BitOr>+BitXor>+AddAssign+SubAssign+MulAssign+DivAssign+RemAssign+BitAndAssign+BitOrAssign+BitXorAssign{fn to_wrapping(self)->Wrapping{Wrapping(self)}fn from_wrapping(w:Wrapping)->Self{w.0}}implfmt::Debug for Wrappingwhere T:fmt::Debug{fn fmt(&self,f:&mut fmt::Formatter<'_>)->fmt::Result{T::fmt(&self.0,f)}}implBounded for Wrappingwhere T:Bounded{fn maximum()->Self{Self(T::maximum())}fn minimum()->Self{Self(T::minimum())}}implZero for Wrappingwhere T:Zero{fn zero()->Self{Self(T::zero())}}implOne for Wrappingwhere T:One{fn one()->Self{Self(T::one())}}implFromStr for Wrappingwhere T:FromStr{type Err=T::Err;fn from_str(s:&str)->Result{T::from_str(s).map(Self)}}implDisplay for Wrappingwhere T:Display{fn fmt(&self,f:&mut fmt::Formatter<'_>)->fmt::Result{T::fmt(&self.0,f)}}implScan for Wrappingwhere T:Scan{type Output=Self;fn scan(iter:&mut I)->Option{T::scan(iter).map(Self)}}impl_binop!(implBitAnd bitand for Wrapping);impl_binop!(implBitOr bitor for Wrapping);impl_binop!(implBitXor bitxor for Wrapping);impl_opassign!(implAddAssign add_assign for Wrapping =>Add add);impl_opassign!(implSubAssign sub_assign for Wrapping =>Sub sub);impl_opassign!(implMulAssign mul_assign for Wrapping =>Mul mul);impl_opassign!(implDivAssign div_assign for Wrapping =>Div div);impl_opassign!(implRemAssign rem_assign for Wrapping =>Rem rem);impl_opassign!(implBitAndAssign bitand_assign for Wrapping);impl_opassign!(implBitOrAssign bitor_assign for Wrapping);impl_opassign!(implBitXorAssign bitxor_assign for Wrapping);implNot for Wrappingwhere T:Not{type Output=Self;fn not(self)->Self::Output{Self(Not::not(self.0))}}macro_rules!impl_int_base_for_wrapping{($($t:ty)*)=>{$(impl Wrappingable for$t{}impl Add for Wrapping<$t>{type Output=Self;fn add(self,rhs:Self)->Self::Output{Self(self.0 .wrapping_add(rhs.0))}}impl Add<$t>for Wrapping<$t>{type Output=Self;fn add(self,rhs:$t)->Self::Output{Self(self.0 .wrapping_add(rhs))}}impl Sub for Wrapping<$t>{type Output=Self;fn sub(self,rhs:Self)->Self::Output{Self(self.0 .wrapping_sub(rhs.0))}}impl Sub<$t>for Wrapping<$t>{type Output=Self;fn sub(self,rhs:$t)->Self::Output{Self(self.0 .wrapping_sub(rhs))}}impl Mul for Wrapping<$t>{type Output=Self;fn mul(self,rhs:Self)->Self::Output{Self(self.0 .wrapping_mul(rhs.0))}}impl Mul<$t>for Wrapping<$t>{type Output=Self;fn mul(self,rhs:$t)->Self::Output{Self(self.0 .wrapping_mul(rhs))}}impl Div for Wrapping<$t>{type Output=Self;fn div(self,rhs:Self)->Self::Output{Self(self.0 .wrapping_div(rhs.0))}}impl Div<$t>for Wrapping<$t>{type Output=Self;fn div(self,rhs:$t)->Self::Output{Self(self.0 .wrapping_div(rhs))}}impl Rem for Wrapping<$t>{type Output=Self;fn rem(self,rhs:Self)->Self::Output{Self(self.0 .wrapping_rem(rhs.0))}}impl Rem<$t>for Wrapping<$t>{type Output=Self;fn rem(self,rhs:$t)->Self::Output{Self(self.0 .wrapping_rem(rhs))}}impl Sum for Wrapping<$t>{fn sum>(iter:I)->Self{iter.fold(Self::zero(),|acc,x|acc+x)}}impl Product for Wrapping<$t>{fn product>(iter:I)->Self{iter.fold(Self::one(),|acc,x|acc*x)}}impl IntBase for Wrapping<$t>{type Error=<$t as IntBase>::Error;fn div_euclid(self,rhs:Self)->Self{Self(self.0 .wrapping_div_euclid(rhs.0))}fn rem_euclid(self,rhs:Self)->Self{Self(self.0 .wrapping_rem_euclid(rhs.0))}fn pow(self,exp:u32)->Self{Self(self.0 .wrapping_pow(exp))}fn from_str_radix(src:&str,radix:u32)->Result{<$t as IntBase>::from_str_radix(src,radix).map(Self)}fn ilog(self,base:Self)->u32{self.0 .ilog(base.0)}fn ilog2(self)->u32{self.0 .ilog2()}fn ilog10(self)->u32{self.0 .ilog10()}fn isqrt(self)->Self{Self(self.0 .isqrt())}fn midpoint(self,rhs:Self)->Self{Self(self.0 .midpoint(rhs.0))}}impl From<$t>for Wrapping<$t>{fn from(t:$t)->Self{Self(t)}})*};}impl_int_base_for_wrapping!(u8 i8 u16 i16 u32 i32 u64 i64 u128 i128 usize isize);macro_rules!impl_unsigned_signed_for_wrapping{($($unsigned:ident$signed:ident)*)=>{$(impl Unsigned for Wrapping<$unsigned>{type Signed=Wrapping<$signed>;fn signed(self)->Self::Signed{Wrapping(self.0 .signed())}fn abs_diff(self,other:Self)->Self{Self(self.0 .abs_diff(other.0))}fn div_ceil(self,rhs:Self)->Self{Self(self.0 .div_ceil(rhs.0))}fn is_power_of_two(self)->bool{self.0 .is_power_of_two()}fn next_power_of_two(self)->Self{Self(self.0 .next_power_of_two())}fn is_multiple_of(self,rhs:Self)->bool{self.0 .is_multiple_of(rhs.0)}fn next_multiple_of(self,rhs:Self)->Self{Self(self.0 .next_multiple_of(rhs.0))}fn gcd(self,other:Self)->Self{Self(self.0 .gcd(other.0))}fn mod_add(self,rhs:Self,modulo:Self)->Self{Self(self.0 .mod_add(rhs.0,modulo.0))}fn mod_sub(self,rhs:Self,modulo:Self)->Self{Self(self.0 .mod_sub(rhs.0,modulo.0))}fn mod_mul(self,rhs:Self,modulo:Self)->Self{Self(self.0 .mod_mul(rhs.0,modulo.0))}}impl Signed for Wrapping<$signed>{type Unsigned=Wrapping<$unsigned>;fn unsigned(self)->Self::Unsigned{Wrapping(self.0 .unsigned())}fn abs(self)->Self{Self(self.0 .wrapping_abs())}fn abs_diff(self,other:Self)->Self::Unsigned{Wrapping(self.0 .abs_diff(other.0))}fn is_negative(self)->bool{self.0 .is_negative()}fn is_positive(self)->bool{self.0 .is_positive()}fn signum(self)->Self{Self(self.0 .signum())}}impl Neg for Wrapping<$signed>{type Output=Self;fn neg(self)->Self::Output{Self(self.0 .wrapping_neg())}})*};}impl_unsigned_signed_for_wrapping!(u8 i8 u16 i16 u32 i32 u64 i64 u128 i128 usize isize);macro_rules!impl_binary_repr_for_wrapping{($($t:ty)*)=>{$(impl Shlfor Wrapping<$t>{type Output=Self;fn shl(self,rhs:u32)->Self::Output{Self(self.0 .wrapping_shl(rhs))}}impl Shrfor Wrapping<$t>{type Output=Self;fn shr(self,rhs:u32)->Self::Output{Self(self.0 .wrapping_shr(rhs))}}impl ShlAssignfor Wrapping<$t>{fn shl_assign(&mut self,rhs:u32){*self=Shl::shl(*self,rhs);}}impl ShrAssignfor Wrapping<$t>{fn shr_assign(&mut self,rhs:u32){*self=Shr::shr(*self,rhs);}}impl BinaryRepr for Wrapping<$t>{fn count_ones(self)->u32{self.0 .count_ones()}fn count_zeros(self)->u32{self.0 .count_zeros()}fn leading_ones(self)->u32{self.0 .leading_ones()}fn leading_zeros(self)->u32{self.0 .leading_zeros()}fn reverse_bits(self)->Self{Self(self.0 .reverse_bits())}fn rotate_left(self,n:u32)->Self{Self(self.0 .rotate_left(n))}fn rotate_right(self,n:u32)->Self{Self(self.0 .rotate_right(n))}fn swap_bytes(self)->Self{Self(self.0 .swap_bytes())}fn trailing_ones(self)->u32{self.0 .trailing_ones()}fn trailing_zeros(self)->u32{self.0 .trailing_zeros()}})*};}impl_binary_repr_for_wrapping!(u8 i8 u16 i16 u32 i32 u64 i64 u128 i128 usize isize);} // codesnip-guard: scanner #[cfg_attr(any(),rustfmt::skip)]pub use self::scanner::*;#[cfg_attr(any(),rustfmt::skip)]mod scanner{use std::{iter::{FromIterator,from_fn,repeat_with},marker::PhantomData};pub fn read_stdin_all()->String{use std::io::Read as _;let mut s=String::new();std::io::stdin().read_to_string(&mut s).expect("io error");s}pub fn read_stdin_all_unchecked()->String{use std::io::Read as _;let mut buf=Vec::new();std::io::stdin().read_to_end(&mut buf).expect("io error");unsafe{String::from_utf8_unchecked(buf)}}pub fn read_all(mut reader:impl std::io::Read)->String{let mut s=String::new();reader.read_to_string(&mut s).expect("io error");s}pub fn read_all_unchecked(mut reader:impl std::io::Read)->String{let mut buf=Vec::new();reader.read_to_end(&mut buf).expect("io error");unsafe{String::from_utf8_unchecked(buf)}}pub fn read_stdin_line()->String{let mut s=String::new();std::io::stdin().read_line(&mut s).expect("io error");s}pub trait Scan:Sized{type Output;fn scan(source:&mut S)->Option;fn scan_vec(source:&mut S,size:usize,capacity:usize)->Vec{let mut values=Vec::with_capacity(capacity.max(size));values.extend((0..size).map(|_|source.scan::()));values}}pub trait MarkedScan:Sized{type Output;fn mscan(self,source:&mut S)->Option;}pub trait ScanSource:Sized{#[doc=" Reads a token under the source's input requirements."]#[doc=" Checked sources return `None` at EOF; unchecked sources require available input."]fn next_token(&mut self)->Option<&str>;#[doc=" Skips separators without consuming the next token."]#[inline]fn skip_whitespace(&mut self){}#[inline]fn read_u8(&mut self)->Option{self.next_token()?.parse().ok()}#[inline]fn read_u16(&mut self)->Option{self.next_token()?.parse().ok()}#[inline]fn read_u32(&mut self)->Option{self.next_token()?.parse().ok()}fn read_u32_vec(&mut self,size:usize,capacity:usize)->Vec{let mut values=Vec::with_capacity(capacity.max(size));values.extend((0..size).map(|_|self.scan::()));values}#[inline]fn read_u64(&mut self)->Option{self.next_token()?.parse().ok()}#[inline]fn read_u128(&mut self)->Option{self.next_token()?.parse().ok()}#[inline]fn read_usize(&mut self)->Option{self.next_token()?.parse().ok()}#[inline]fn read_i8(&mut self)->Option{self.next_token()?.parse().ok()}#[inline]fn read_i16(&mut self)->Option{self.next_token()?.parse().ok()}#[inline]fn read_i32(&mut self)->Option{self.next_token()?.parse().ok()}#[inline]fn read_i64(&mut self)->Option{self.next_token()?.parse().ok()}#[inline]fn read_i128(&mut self)->Option{self.next_token()?.parse().ok()}#[inline]fn read_isize(&mut self)->Option{self.next_token()?.parse().ok()}#[doc=" Panics if reading fails."]#[inline]fn scan(&mut self)->T::Output{T::scan(self).expect("scan error")}#[doc=" Panics if reading fails."]#[inline]fn mscan(&mut self,marker:T)->T::Output{marker.mscan(self).expect("scan error")}fn scan_vec(&mut self,size:usize,capacity:usize)->Vec{if size==0{self.skip_whitespace();}T::scan_vec(self,size,capacity)}#[inline]fn iter(&mut self)->ScannerIter<'_,Self,T>{ScannerIter{inner:self,_marker:PhantomData}}}#[derive(Clone,Debug)]pub struct Scanner<'a,I:Iterator=std::str::SplitAsciiWhitespace<'a>>{iter:I}impl<'a>Scanner<'a>{pub fn new(s:&'a str)->Self{Self{iter:s.split_ascii_whitespace()}}}impl<'a,I:Iterator>Scanner<'a,I>{pub fn new_from_iter(iter:I)->Self{Self{iter}}}impl<'a,I:Iterator>ScanSource for Scanner<'a,I>{fn next_token(&mut self)->Option<&str>{self.iter.next()}}macro_rules!impl_scan{($($t:ty)*)=>{$(impl Scan for$t{type Output=Self;fn scan(iter:&mut I)->Option{iter.next_token()?.parse::<$t>().ok()}})*};}impl_scan!(char f32 f64 String);macro_rules!impl_integer_scan{($($t:ty=>$read:ident),*$(,)?)=>{$(impl Scan for$t{type Output=Self;#[inline]fn scan(source:&mut S)->Option{source.$read()}})*};}impl_integer_scan!(u8=>read_u8,u16=>read_u16,u64=>read_u64,u128=>read_u128,usize=>read_usize,i8=>read_i8,i16=>read_i16,i32=>read_i32,i64=>read_i64,i128=>read_i128,isize=>read_isize,);impl Scan for u32{type Output=Self;#[inline]fn scan(source:&mut S)->Option{source.read_u32()}fn scan_vec(source:&mut S,size:usize,capacity:usize)->Vec{source.read_u32_vec(size,capacity)}}macro_rules!impl_scan_tuple{(@impl$($T:ident)*)=>{impl<$($T:Scan),*>Scan for($($T,)*){type Output=($(<$T as Scan>::Output,)*);fn scan(_iter:&mut It)->Option{Some(($(<$T as Scan>::scan(_iter)?,)*))}}};(@inner$($T:ident)*,)=>{impl_scan_tuple!(@impl$($T)*);};(@inner$($T:ident)*,$U:ident$($Rest:ident)*)=>{impl_scan_tuple!(@impl$($T)*);impl_scan_tuple!(@inner$($T)*$U,$($Rest)*);};($($T:ident)*)=>{impl_scan_tuple!(@inner,$($T)*);};}impl_scan_tuple!(A B C D E F G H I J K);pub struct ScannerIter<'a,S,T>{inner:&'a mut S,_marker:PhantomDataT>}implIterator for ScannerIter<'_,S,T>{type Item=T::Output;#[inline]fn next(&mut self)->Option{T::scan(self.inner)}}#[doc=" scan a value with Scanner"]#[doc=""]#[doc=" - `scan_value!(scanner, ELEMENT)`"]#[doc=""]#[doc=" ELEMENT :="]#[doc=" - `$ty`: Scan"]#[doc=" - `&str`: borrowed token; the source stays borrowed until its last use"]#[doc=" - `@$expr`: MarkedScan"]#[doc=" - `$ty = $expr`: MarkedScan"]#[doc=" - `[ELEMENT; $expr]`: vector"]#[doc=" - `[$ty; $len; capacity $capacity]`: vector with reserved capacity"]#[doc=" - `[ELEMENT; const $expr]`: array"]#[doc=" - `[ELEMENT]`: iterator"]#[doc=" - `[ELEMENT; iter $expr]`: iterator of the specified length"]#[doc=" - `($(ELEMENT)*,)`: tuple"]#[macro_export]macro_rules!scan_value{(@repeat$scanner:expr,[$($t:tt)*]$len:expr)=>{{$crate::scan_value!(@iter$scanner,[$($t)*]$len).collect::>()}};(@repeat$scanner:expr,[$($t:tt)*])=>{{::std::iter::repeat_with(||$crate::scan_value!(@inner$scanner,[]$($t)*))}};(@iter$scanner:expr,[$($t:tt)*]$len:expr)=>{{let size=$len;if size==0{$scanner.skip_whitespace();}$crate::scan_value!(@repeat$scanner,[$($t)*]).take(size)}};(@array$scanner:expr,[$($t:tt)*]$len:expr)=>{{if$len==0{$scanner.skip_whitespace();}$crate::array![||$crate::scan_value!(@inner$scanner,[]$($t)*);$len]}};(@tuple$scanner:expr,[$([$($args:tt)*])*])=>{($($($args)*,)*)};(@sparen$scanner:expr,[]@$e:expr;$($t:tt)*)=>{$crate::scan_value!(@sparen$scanner,[@$e]$($t)*)};(@sparen$scanner:expr,[]($($tt:tt)*);$($t:tt)*)=>{$crate::scan_value!(@sparen$scanner,[($($tt)*)]$($t)*)};(@sparen$scanner:expr,[][$($tt:tt)*];$($t:tt)*)=>{$crate::scan_value!(@sparen$scanner,[[$($tt)*]]$($t)*)};(@sparen$scanner:expr,[]$ty:ty=$e:expr;$($t:tt)*)=>{$crate::scan_value!(@sparen$scanner,[$ty=$e]$($t)*)};(@sparen$scanner:expr,[]$ty:ty;$($t:tt)*)=>{$crate::scan_value!(@sparen$scanner,[$ty]$($t)*)};(@sparen$scanner:expr,[]$($args:tt)*)=>{$crate::scan_value!(@repeat$scanner,[$($args)*])};(@sparen$scanner:expr,[$($args:tt)+]const$len:expr)=>{$crate::scan_value!(@array$scanner,[$($args)+]$len)};(@sparen$scanner:expr,[$($args:tt)+]iter$len:expr)=>{$crate::scan_value!(@iter$scanner,[$($args)+]$len)};(@sparen$scanner:expr,[$($args:tt)+]$len:expr;capacity$capacity:expr)=>{{let size=$len;let capacity=$capacity;$scanner.scan_vec::<$($args)+>(size,capacity)}};(@sparen$scanner:expr,[$($args:tt)+]$len:expr)=>{$crate::scan_value!(@repeat$scanner,[$($args)+]$len)};(@$tag:ident$scanner:expr,[[$($args:tt)*]])=>{$($args)*};(@$tag:ident$scanner:expr,[$($args:tt)*]@$e:expr$(,$($t:tt)*)?)=>{$crate::scan_value!(@$tag$scanner,[$($args)*[$scanner.mscan($e)]]$(,$($t)*)?)};(@$tag:ident$scanner:expr,[$($args:tt)*]($($tuple:tt)*)$($t:tt)*)=>{$crate::scan_value!(@$tag$scanner,[$($args)*[$crate::scan_value!(@tuple$scanner,[]$($tuple)*)]]$($t)*)};(@$tag:ident$scanner:expr,[$($args:tt)*][$($tt:tt)*]$($t:tt)*)=>{$crate::scan_value!(@$tag$scanner,[$($args)*[$crate::scan_value!(@sparen$scanner,[]$($tt)*)]]$($t)*)};(@$tag:ident$scanner:expr,[$($args:tt)*]&str$(,$($t:tt)*)?)=>{$crate::scan_value!(@$tag$scanner,[$($args)*[$scanner.next_token().expect("scan error")]]$(,$($t)*)?)};(@$tag:ident$scanner:expr,[$($args:tt)*]$ty:ty=$e:expr$(,$($t:tt)*)?)=>{$crate::scan_value!(@$tag$scanner,[$($args)*[{let _tmp:$ty=$scanner.mscan($e);_tmp}]]$(,$($t)*)?)};(@$tag:ident$scanner:expr,[$($args:tt)*]$ty:ty$(,$($t:tt)*)?)=>{$crate::scan_value!(@$tag$scanner,[$($args)*[$scanner.scan::<$ty>()]]$(,$($t)*)?)};(@$tag:ident$scanner:expr,[$($args:tt)*],$($t:tt)*)=>{$crate::scan_value!(@$tag$scanner,[$($args)*]$($t)*)};(@$tag:ident$scanner:expr,[$($args:tt)*])=>{::std::compile_error!(::std::stringify!($($args)*))};(src=$src:expr,$($t:tt)*)=>{{let mut __scanner=Scanner::new($src);$crate::scan_value!(@inner __scanner,[]$($t)*)}};(iter=$iter:expr,$($t:tt)*)=>{{let mut __scanner=Scanner::new_from_iter($iter);$crate::scan_value!(@inner __scanner,[]$($t)*)}};($scanner:expr,$($t:tt)*)=>{$crate::scan_value!(@inner$scanner,[]$($t)*)}}#[doc=" scan and bind values with Scanner"]#[doc=""]#[doc=" - `scan!(scanner, $($pat $(: ELEMENT)?),*)`"]#[macro_export]macro_rules!scan{(@assert$p:pat)=>{};(@assert$($p:tt)*)=>{::std::compile_error!(::std::concat!("expected pattern, found `",::std::stringify!($($p)*),"`"));};(@pat$scanner:expr,[][])=>{};(@pat$scanner:expr,[][],$($t:tt)*)=>{$crate::scan!(@pat$scanner,[][]$($t)*)};(@pat$scanner:expr,[$($p:tt)*][]$x:ident$($t:tt)*)=>{$crate::scan!(@pat$scanner,[$($p)*$x][]$($t)*)};(@pat$scanner:expr,[$($p:tt)*][]::$($t:tt)*)=>{$crate::scan!(@pat$scanner,[$($p)*::][]$($t)*)};(@pat$scanner:expr,[$($p:tt)*][]&$($t:tt)*)=>{$crate::scan!(@pat$scanner,[$($p)*&][]$($t)*)};(@pat$scanner:expr,[$($p:tt)*][]($($x:tt)*)$($t:tt)*)=>{$crate::scan!(@pat$scanner,[$($p)*($($x)*)][]$($t)*)};(@pat$scanner:expr,[$($p:tt)*][][$($x:tt)*]$($t:tt)*)=>{$crate::scan!(@pat$scanner,[$($p)*[$($x)*]][]$($t)*)};(@pat$scanner:expr,[$($p:tt)*][]{$($x:tt)*}$($t:tt)*)=>{$crate::scan!(@pat$scanner,[$($p)*{$($x)*}][]$($t)*)};(@pat$scanner:expr,[$($p:tt)*][]:$($t:tt)*)=>{$crate::scan!(@ty$scanner,[$($p)*][]$($t)*)};(@pat$scanner:expr,[$($p:tt)*][]$($t:tt)*)=>{$crate::scan!(@let$scanner,[$($p)*][usize]$($t)*)};(@ty$scanner:expr,[$($p:tt)*][$($tt:tt)*]@$e:expr$(,$($t:tt)*)?)=>{$crate::scan!(@let$scanner,[$($p)*][$($tt)*@$e]$(,$($t)*)?)};(@ty$scanner:expr,[$($p:tt)*][$($tt:tt)*]($($x:tt)*)$($t:tt)*)=>{$crate::scan!(@let$scanner,[$($p)*][$($tt)*($($x)*)]$($t)*)};(@ty$scanner:expr,[$($p:tt)*][$($tt:tt)*][$($x:tt)*]$($t:tt)*)=>{$crate::scan!(@let$scanner,[$($p)*][$($tt)*[$($x)*]]$($t)*)};(@ty$scanner:expr,[$($p:tt)*][$($tt:tt)*]&str$(,$($t:tt)*)?)=>{$crate::scan!(@let$scanner,[$($p)*][$($tt)*&str]$(,$($t)*)?)};(@ty$scanner:expr,[$($p:tt)*][$($tt:tt)*]$ty:ty=$e:expr$(,$($t:tt)*)?)=>{$crate::scan!(@let$scanner,[$($p)*][$($tt)*$ty=$e]$(,$($t)*)?)};(@ty$scanner:expr,[$($p:tt)*][$($tt:tt)*]$ty:ty$(,$($t:tt)*)?)=>{$crate::scan!(@let$scanner,[$($p)*][$($tt)*$ty]$(,$($t)*)?)};(@let$scanner:expr,[$($p:tt)*][$($tt:tt)*]$($t:tt)*)=>{$crate::scan!{@assert$($p)*}let$($p)* =$crate::scan_value!($scanner,$($tt)*);$crate::scan!(@pat$scanner,[][]$($t)*)};(src=$src:expr,$($t:tt)*)=>{let mut __scanner=Scanner::new($src);$crate::scan!(@pat __scanner,[][]$($t)*)};(iter=$iter:expr,$($t:tt)*)=>{let mut __scanner=Scanner::new_from_iter($iter);$crate::scan!(@pat __scanner,[][]$($t)*)};($scanner:expr,$($t:tt)*)=>{$crate::scan!(@pat$scanner,[][]$($t)*)}}#[doc=" define enum scan rules"]#[doc=""]#[doc=" # Example"]#[doc=" ```rust"]#[doc=" # use competitive::{define_enum_scan, tools::{CharsWithBase, Scan, ScanSource, Scanner, Usize1}};"]#[doc=" define_enum_scan! {"]#[doc=" enum Query: u8 {"]#[doc=" 0 => Noop,"]#[doc=" 1 => Args { i: Usize1, s: char },"]#[doc=" 9 => Complex { n: usize, c: [(usize, Vec = CharsWithBase('a')); n] },"]#[doc=" }"]#[doc=" }"]#[doc=" ```"]#[macro_export]macro_rules!define_enum_scan{(@field_ty@repeat[$($t:tt)*]$($len:expr)?)=>{Vec<$crate::define_enum_scan!(@field_ty$($t)*)>};(@field_ty@array[$($t:tt)*]$len:expr)=>{[$crate::define_enum_scan!(@field_ty$($t)*);$len]};(@field_ty@tuple[$([$($args:tt)*])*])=>{($($($args)*,)*)};(@field_ty@sparen[]($($tt:tt)*);$($t:tt)*)=>{$crate::define_enum_scan!(@field_ty@sparen[($($tt)*)]$($t)*)};(@field_ty@sparen[][$($tt:tt)*];$($t:tt)*)=>{$crate::define_enum_scan!(@field_ty@sparen[[$($tt)*]]$($t)*)};(@field_ty@sparen[]$ty:ty=$e:expr;$($t:tt)*)=>{$crate::define_enum_scan!(@field_ty@sparen[$ty=$e]$($t)*)};(@field_ty@sparen[]$ty:ty;$($t:tt)*)=>{$crate::define_enum_scan!(@field_ty@sparen[$ty]$($t)*)};(@field_ty@sparen[]$($args:tt)*)=>{$crate::define_enum_scan!(@field_ty@repeat[$($args)*])};(@field_ty@sparen[$($args:tt)+]const$len:expr)=>{$crate::define_enum_scan!(@field_ty@array[$($args)+]$len)};(@field_ty@sparen[$($args:tt)+]$len:expr)=>{$crate::define_enum_scan!(@field_ty@repeat[$($args)+]$len)};(@field_ty@$tag:ident[$($args:tt)*]($($tuple:tt)*)$($t:tt)*)=>{$crate::define_enum_scan!(@field_ty@$tag[$($args)*[$crate::define_enum_scan!(@field_ty@tuple[]$($tuple)*)]]$($t)*)};(@field_ty@$tag:ident[$($args:tt)*][$($tt:tt)*]$($t:tt)*)=>{$crate::define_enum_scan!(@field_ty@$tag[$($args)*[$crate::define_enum_scan!(@field_ty@sparen[]$($tt)*)]]$($t)*)};(@field_ty@$tag:ident[$($args:tt)*]$ty:ty=$e:expr$(,$($t:tt)*)?)=>{$crate::define_enum_scan!(@field_ty@$tag[$($args)*[$ty]]$(,$($t)*)?)};(@field_ty@$tag:ident[$($args:tt)*]$ty:ty$(,$($t:tt)*)?)=>{$crate::define_enum_scan!(@field_ty@$tag[$($args)*[<$ty as Scan>::Output]]$(,$($t)*)?)};(@field_ty@$tag:ident[$($args:tt)*],$($t:tt)*)=>{$crate::define_enum_scan!(@field_ty@$tag[$($args)*]$($t)*)};(@field_ty@$tag:ident[[$($args:tt)*]])=>{$($args)*};(@field_ty@$tag:ident[$($args:tt)*])=>{::std::compile_error!(::std::stringify!($($args)*))};(@field_ty$($t:tt)*)=>{$crate::define_enum_scan!(@field_ty@inner[]$($t)*)};(@tag_expr raw,$iter:ident)=>{ScanSource::next_token($iter)?};(@tag_expr$d:ty,$iter:ident)=>{<$d as Scan>::scan($iter)?};(@variant([$($attr:tt)*]$vis:vis$T:ident$d:tt)[$($vars:tt)*])=>{$crate::define_enum_scan!{@def$($attr)*$vis enum$T:$d{$($vars)*}}};(@variant$ctx:tt[$($vars:tt)*]$p:pat=>$v:ident{$($fs:tt)*}$($rest:tt)*)=>{$crate::define_enum_scan!{@field$ctx[$($vars)*]$p=>$v[]$($fs)*;$($rest)*}};(@variant$ctx:tt[$($vars:tt)*]$p:pat=>$v:ident$($rest:tt)*)=>{$crate::define_enum_scan!{@variant$ctx[$($vars)*$p=>$v,]$($rest)*}};(@variant$ctx:tt[$($vars:tt)*],$($rest:tt)*)=>{$crate::define_enum_scan!{@variant$ctx[$($vars)*]$($rest)*}};(@endfield$ctx:tt[$($vars:tt)*]$p:pat=>$v:ident[$($fs:tt)*][$f:ident:$($spec:tt)*],$($rest:tt)*)=>{$crate::define_enum_scan!{@field$ctx[$($vars)*]$p=>$v[$($fs)*[$f:$($spec)*]]$($rest)*}};(@endfield$ctx:tt[$($vars:tt)*]$p:pat=>$v:ident[$($fs:tt)*][$f:ident:$($spec:tt)*];$($rest:tt)*)=>{$crate::define_enum_scan!{@variant$ctx[$($vars)*$p=>$v{$($fs)*[$f:$($spec)*]},]$($rest)*}};(@field$ctx:tt[$($vars:tt)*]$p:pat=>$v:ident[$($fs:tt)*];$($rest:tt)*)=>{$crate::define_enum_scan!{@variant$ctx[$($vars)*$p=>$v{$($fs)*},]$($rest)*}};(@field$ctx:tt[$($vars:tt)*]$p:pat=>$v:ident[$($fs:tt)*]$f:ident:($($tuple:tt)*)$sep:tt$($rest:tt)*)=>{$crate::define_enum_scan!{@endfield$ctx[$($vars)*]$p=>$v[$($fs)*][$f:($($tuple)*)]$sep$($rest)*}};(@field$ctx:tt[$($vars:tt)*]$p:pat=>$v:ident[$($fs:tt)*]$f:ident:[$($x:tt)*]$sep:tt$($rest:tt)*)=>{$crate::define_enum_scan!{@endfield$ctx[$($vars)*]$p=>$v[$($fs)*][$f:[$($x)*]]$sep$($rest)*}};(@field$ctx:tt[$($vars:tt)*]$p:pat=>$v:ident[$($fs:tt)*]$f:ident:$ty:ty=$e:expr,$($rest:tt)*)=>{$crate::define_enum_scan!{@endfield$ctx[$($vars)*]$p=>$v[$($fs)*][$f:$ty=$e],$($rest)*}};(@field$ctx:tt[$($vars:tt)*]$p:pat=>$v:ident[$($fs:tt)*]$f:ident:$ty:ty;$($rest:tt)*)=>{$crate::define_enum_scan!{@endfield$ctx[$($vars)*]$p=>$v[$($fs)*][$f:$ty];$($rest)*}};(@field$ctx:tt[$($vars:tt)*]$p:pat=>$v:ident[$($fs:tt)*]$f:ident:$ty:ty=$e:expr;$($rest:tt)*)=>{$crate::define_enum_scan!{@endfield$ctx[$($vars)*]$p=>$v[$($fs)*][$f:$ty=$e];$($rest)*}};(@field$ctx:tt[$($vars:tt)*]$p:pat=>$v:ident[$($fs:tt)*]$f:ident:$ty:ty,$($rest:tt)*)=>{$crate::define_enum_scan!{@endfield$ctx[$($vars)*]$p=>$v[$($fs)*][$f:$ty],$($rest)*}};(@def$(#[$attr:meta])*$vis:vis enum$T:ident:$d:tt{$($p:pat=>$v:ident$({$([$f:ident:$($spec:tt)*])*})?,)*})=>{$(#[$attr])*$vis enum$T{$($v$({$($f:$crate::define_enum_scan!(@field_ty$($spec)*)),*})?),*}impl Scan for$T{type Output=Self;fn scan(iter:&mut I)->Option{let tag=$crate::define_enum_scan!(@tag_expr$d,iter);match tag{$($p=>{$($(let$f=$crate::scan_value!((*iter),$($spec)*);)*)?Some($T::$v$({$($f),*})?)}),*_=>None,}}}};($(#[$attr:meta])*$vis:vis enum$T:ident:raw{$($body:tt)*})=>{$crate::define_enum_scan!{@variant([$(#[$attr])*]$vis$T raw)[]$($body)*}};($(#[$attr:meta])*$vis:vis enum$T:ident:$d:ty{$($body:tt)*})=>{$crate::define_enum_scan!{@variant([$(#[$attr])*]$vis$T$d)[]$($body)*}};}#[derive(Debug,Copy,Clone)]pub enum Usize1{}impl Scan for Usize1{type Output=usize;fn scan(iter:&mut I)->Option{::scan(iter)?.checked_sub(1)}}#[derive(Debug,Copy,Clone)]pub struct CharWithBase(pub char);impl MarkedScan for CharWithBase{type Output=usize;fn mscan(self,iter:&mut I)->Option{Some((::scan(iter)?as u8-self.0 as u8)as usize)}}#[derive(Debug,Copy,Clone)]pub enum Chars{}impl Scan for Chars{type Output=Vec;fn scan(iter:&mut I)->Option{Some(iter.next_token()?.chars().collect())}}#[derive(Debug,Copy,Clone)]pub struct CharsWithBase(pub char);impl MarkedScan for CharsWithBase{type Output=Vec;fn mscan(self,iter:&mut I)->Option{Some(iter.next_token()?.chars().map(|c|(c as u8-self.0 as u8)as usize).collect())}}#[derive(Debug,Copy,Clone)]pub enum Byte1{}impl Scan for Byte1{type Output=u8;fn scan(iter:&mut I)->Option{let bytes=iter.next_token()?.as_bytes();assert_eq!(bytes.len(),1);Some(bytes[0])}}#[derive(Debug,Copy,Clone)]pub struct ByteWithBase(pub u8);impl MarkedScan for ByteWithBase{type Output=usize;fn mscan(self,iter:&mut I)->Option{Some((::scan(iter)?as u8-self.0)as usize)}}#[derive(Debug,Copy,Clone)]pub enum Bytes{}impl Scan for Bytes{type Output=Vec;fn scan(iter:&mut I)->Option{Some(iter.next_token()?.bytes().collect())}}#[derive(Debug,Copy,Clone)]pub struct BytesWithBase(pub u8);impl MarkedScan for BytesWithBase{type Output=Vec;fn mscan(self,iter:&mut I)->Option{Some(iter.next_token()?.bytes().map(|c|(c-self.0)as usize).collect())}}#[derive(Debug,Copy,Clone)]pub struct Collect::Output>>where T:Scan,B:FromIterator<::Output>{size:usize,_marker:PhantomData(T,B)>}implCollectwhere T:Scan,B:FromIterator<::Output>{pub fn new(size:usize)->Self{Self{size,_marker:PhantomData}}}implMarkedScan for Collectwhere T:Scan,B:FromIterator<::Output>{type Output=B;fn mscan(self,iter:&mut I)->Option{if self.size==0{iter.skip_whitespace();}repeat_with(| |::scan(iter)).take(self.size).collect()}}#[derive(Debug,Copy,Clone)]pub struct SizedCollect::Output>>where T:Scan,B:FromIterator<::Output>{_marker:PhantomData(T,B)>}implScan for SizedCollectwhere T:Scan,B:FromIterator<::Output>{type Output=B;fn scan(iter:&mut I)->Option{let size=usize::scan(iter)?;if size==0{iter.skip_whitespace();}repeat_with(| |::scan(iter)).take(size).collect()}}#[derive(Debug,Copy,Clone)]pub struct Splittedwhere T:Scan{pat:P,_marker:PhantomDataT>}implSplittedwhere T:Scan{pub fn new(pat:P)->Self{Self{pat,_marker:PhantomData}}}implMarkedScan for Splittedwhere T:Scan{type Output=Vec<::Output>;fn mscan(self,iter:&mut I)->Option{let mut iter=Scanner::new_from_iter(iter.next_token()?.split(self.pat));Some(from_fn(| |::scan(&mut iter)).collect())}}implMarkedScan for Splittedwhere T:Scan{type Output=Vec<::Output>;fn mscan(self,iter:&mut I)->Option{let mut iter=Scanner::new_from_iter(iter.next_token()?.split(self.pat));Some(from_fn(| |::scan(&mut iter)).collect())}}implMarkedScan for F where F:Fn(&str)->Option{type Output=T;fn mscan(self,iter:&mut I)->Option{self(iter.next_token()?)}}} // codesnip-guard: zero_one #[cfg_attr(any(),rustfmt::skip)]pub use self::zero_one::{One,Zero};#[cfg_attr(any(),rustfmt::skip)]mod zero_one{pub trait Zero:Sized{fn zero()->Self;#[inline]fn is_zero(&self)->bool where Self:PartialEq{self==&Self::zero()}#[inline]fn set_zero(&mut self){*self=Self::zero();}}pub trait One:Sized{fn one()->Self;#[inline]fn is_one(&self)->bool where Self:PartialEq{self==&Self::one()}#[inline]fn set_one(&mut self){*self=Self::one();}}macro_rules!impl_zero_one{($({$(<$T:ident:$Bound:ident>)?$Trait:ident$method:ident$($t:ty)*,$e:expr})*)=>{$(impl_zero_one!(@impl[$(<$T:$Bound>)?]$Trait$method[$($t)*],$e);)*};(@impl[<$T:ident:$Bound:ident>]$Trait:ident$method:ident[$($t:ty)*],$e:expr)=>{$(impl<$T:$Bound>$Trait for$t{fn$method()->Self{$e}})*};(@impl[]$Trait:ident$method:ident[$($t:ty)*],$e:expr)=>{$(impl$Trait for$t{fn$method()->Self{$e}})*};}impl_zero_one!({Zero zero u8 u16 u32 u64 usize i8 i16 i32 i64 isize u128 i128,0}{Zero zero f32 f64,0.}{One one u8 u16 u32 u64 usize i8 i16 i32 i64 isize u128 i128,1}{One one f32 f64,1.}{Zero zero std::num::Wrapping,Self(T::zero())}{One one std::num::Wrapping,Self(T::one())});}