pub struct URational<T>where
T: Unsigned,{
pub num: T,
pub den: T,
}
Fields§
§num: T
§den: T
Implementations§
Source§impl<T> URational<T>where
T: Unsigned,
impl<T> URational<T>where
T: Unsigned,
Sourcepub fn new(num: T, den: T) -> Self
pub fn new(num: T, den: T) -> Self
Examples found in repository?
More examples
crates/competitive/src/num/urational.rs (line 78)
74 pub fn map<U>(self, mut f: impl FnMut(T) -> U) -> URational<U>
75 where
76 U: Unsigned,
77 {
78 URational::new(f(self.num), f(self.den))
79 }
80 pub fn map_unchecked<U>(self, mut f: impl FnMut(T) -> U) -> URational<U>
81 where
82 U: Unsigned,
83 {
84 URational::new_unchecked(f(self.num), f(self.den))
85 }
86 pub fn map_eval<U>(self, mut f: impl FnMut(T) -> U) -> <U as Div>::Output
87 where
88 U: Div,
89 {
90 f(self.num) / f(self.den)
91 }
92}
93
94impl<T> Bounded for URational<T>
95where
96 T: Unsigned,
97{
98 fn maximum() -> Self {
99 Self::new_unchecked(T::one(), T::zero())
100 }
101 fn minimum() -> Self {
102 Self::zero()
103 }
104}
105
106impl<T> Zero for URational<T>
107where
108 T: Unsigned,
109{
110 fn zero() -> Self {
111 Self::new_unchecked(T::zero(), T::one())
112 }
113}
114impl<T> One for URational<T>
115where
116 T: Unsigned,
117{
118 fn one() -> Self {
119 Self::new_unchecked(T::one(), T::one())
120 }
121}
122
123impl<T> Add for URational<T>
124where
125 T: Unsigned,
126{
127 type Output = Self;
128 fn add(self, rhs: Self) -> Self::Output {
129 Self::new(self.num * rhs.den + self.den * rhs.num, self.den * rhs.den)
130 }
131}
132impl<T> Sub for URational<T>
133where
134 T: Unsigned,
135{
136 type Output = Self;
137 fn sub(self, rhs: Self) -> Self::Output {
138 Self::new(self.num * rhs.den - self.den * rhs.num, self.den * rhs.den)
139 }
140}
141impl<T> Mul for URational<T>
142where
143 T: Unsigned,
144{
145 type Output = Self;
146 fn mul(self, rhs: Self) -> Self::Output {
147 Self::new(self.num * rhs.num, self.den * rhs.den)
148 }
149}
150impl<T> Div for URational<T>
151where
152 T: Unsigned,
153{
154 type Output = Self;
155 fn div(self, rhs: Self) -> Self::Output {
156 Self::new(self.num * rhs.den, self.den * rhs.num)
157 }
crates/library_checker/src/math/stern_brocot_tree.rs (line 18)
9pub fn stern_brocot_tree(reader: impl Read, mut writer: impl Write) {
10 let s = read_all_unchecked(reader);
11 let mut scanner = Scanner::new(&s);
12 scan!(scanner, t);
13 for _ in 0..t {
14 scan!(scanner, type_: String);
15 match type_.as_str() {
16 "ENCODE_PATH" => {
17 scan!(scanner, a: u32, b: u32);
18 let path = SbtPath::from(URational::new(a, b));
19 let len = if path.path.first() == Some(&0) {
20 path.path.len() - 1
21 } else {
22 path.path.len()
23 };
24 write!(writer, "{}", len).ok();
25 for (i, count) in path.into_iter().enumerate() {
26 if count == 0 {
27 continue;
28 }
29 if i % 2 == 0 {
30 write!(writer, " R {}", count).ok();
31 } else {
32 write!(writer, " L {}", count).ok();
33 }
34 }
35 writeln!(writer).ok();
36 }
37 "DECODE_PATH" => {
38 scan!(scanner, k, path: [(char, u32); k]);
39 let node: SbtNode<u32> = if path.first().is_some_and(|t| t.0 == 'L') {
40 [0].into_iter()
41 .chain(path.into_iter().map(|(_, c)| c))
42 .collect()
43 } else {
44 path.into_iter().map(|(_, c)| c).collect()
45 };
46 let val = node.eval();
47 writeln!(writer, "{} {}", val.num, val.den).ok();
48 }
49 "LCA" => {
50 scan!(scanner, [a, b, c, d]: [u32; const 4]);
51 let path1 = SbtPath::from(URational::new(a, b));
52 let path2 = SbtPath::from(URational::new(c, d));
53 let val = SbtNode::lca(path1, path2).eval();
54 writeln!(writer, "{} {}", val.num, val.den).ok();
55 }
56 "ANCESTOR" => {
57 scan!(scanner, [k, a, b]: [u32; const 3]);
58 let mut path = SbtPath::from(URational::new(a, b));
59 let depth = path.depth();
60 if k <= depth {
61 path.up(depth - k);
62 let val = path.eval();
63 writeln!(writer, "{} {}", val.num, val.den).ok();
64 } else {
65 writeln!(writer, "-1").ok();
66 }
67 }
68 "RANGE" => {
69 scan!(scanner, [a, b]: [u32; const 2]);
70 let node = SbtPath::from(URational::new(a, b)).to_node();
71 writeln!(
72 writer,
73 "{} {} {} {}",
74 node.l.num, node.l.den, node.r.num, node.r.den
75 )
76 .ok();
77 }
78 _ => unreachable!(),
79 }
80 }
81}
Sourcepub fn new_unchecked(num: T, den: T) -> Self
pub fn new_unchecked(num: T, den: T) -> Self
Examples found in repository?
crates/competitive/src/num/urational.rs (line 66)
64 pub fn new(num: T, den: T) -> Self {
65 let g = num.gcd(den);
66 Self::new_unchecked(num / g, den / g)
67 }
68 pub fn new_unchecked(num: T, den: T) -> Self {
69 Self { num, den }
70 }
71 pub fn eval(self) -> T {
72 self.num / self.den
73 }
74 pub fn map<U>(self, mut f: impl FnMut(T) -> U) -> URational<U>
75 where
76 U: Unsigned,
77 {
78 URational::new(f(self.num), f(self.den))
79 }
80 pub fn map_unchecked<U>(self, mut f: impl FnMut(T) -> U) -> URational<U>
81 where
82 U: Unsigned,
83 {
84 URational::new_unchecked(f(self.num), f(self.den))
85 }
86 pub fn map_eval<U>(self, mut f: impl FnMut(T) -> U) -> <U as Div>::Output
87 where
88 U: Div,
89 {
90 f(self.num) / f(self.den)
91 }
92}
93
94impl<T> Bounded for URational<T>
95where
96 T: Unsigned,
97{
98 fn maximum() -> Self {
99 Self::new_unchecked(T::one(), T::zero())
100 }
101 fn minimum() -> Self {
102 Self::zero()
103 }
104}
105
106impl<T> Zero for URational<T>
107where
108 T: Unsigned,
109{
110 fn zero() -> Self {
111 Self::new_unchecked(T::zero(), T::one())
112 }
113}
114impl<T> One for URational<T>
115where
116 T: Unsigned,
117{
118 fn one() -> Self {
119 Self::new_unchecked(T::one(), T::one())
120 }
More examples
pub fn eval(self) -> T
pub fn map<U>(self, f: impl FnMut(T) -> U) -> URational<U>where
U: Unsigned,
pub fn map_unchecked<U>(self, f: impl FnMut(T) -> U) -> URational<U>where
U: Unsigned,
pub fn map_eval<U>(self, f: impl FnMut(T) -> U) -> <U as Div>::Outputwhere
U: Div,
Trait Implementations§
Source§impl<T> AddAssign for URational<T>where
T: Unsigned,
impl<T> AddAssign for URational<T>where
T: Unsigned,
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
Performs the
+=
operation. Read moreSource§impl<T> Bounded for URational<T>where
T: Unsigned,
impl<T> Bounded for URational<T>where
T: Unsigned,
fn maximum() -> Self
fn minimum() -> Self
fn is_maximum(&self) -> bool
fn is_minimum(&self) -> bool
fn set_maximum(&mut self)
fn set_minimum(&mut self)
Source§impl<T> DivAssign for URational<T>where
T: Unsigned,
impl<T> DivAssign for URational<T>where
T: Unsigned,
Source§fn div_assign(&mut self, rhs: Self)
fn div_assign(&mut self, rhs: Self)
Performs the
/=
operation. Read moreSource§impl<T> MulAssign for URational<T>where
T: Unsigned,
impl<T> MulAssign for URational<T>where
T: Unsigned,
Source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
Performs the
*=
operation. Read moreSource§impl<T> Ord for URational<T>where
T: Unsigned,
impl<T> Ord for URational<T>where
T: Unsigned,
Source§impl<T> PartialOrd for URational<T>where
T: Unsigned,
impl<T> PartialOrd for URational<T>where
T: Unsigned,
Source§impl<T> SubAssign for URational<T>where
T: Unsigned,
impl<T> SubAssign for URational<T>where
T: Unsigned,
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
Performs the
-=
operation. Read moreimpl<T> Copy for URational<T>
impl<T> Eq for URational<T>where
T: Unsigned,
Auto Trait Implementations§
impl<T> Freeze for URational<T>where
T: Freeze,
impl<T> RefUnwindSafe for URational<T>where
T: RefUnwindSafe,
impl<T> Send for URational<T>where
T: Send,
impl<T> Sync for URational<T>where
T: Sync,
impl<T> Unpin for URational<T>where
T: Unpin,
impl<T> UnwindSafe for URational<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> AsTotalOrd for Twhere
T: PartialOrd,
impl<T> AsTotalOrd for Twhere
T: PartialOrd,
fn as_total_ord(&self) -> TotalOrd<&T>
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more